csharp Topshelf + OWIN自主机+ ASP.NET WebAPI + Ninject

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Topshelf + OWIN自主机+ ASP.NET WebAPI + Ninject相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Web.Http;
using System.Web.Http.Tracing;
using System.Reflection;
using Owin;
using Microsoft.Owin.Hosting;
using Topshelf;
using Topshelf.ServiceConfigurators;
using Ninject;
using Ninject.Web.Common.OwinHost;
using Ninject.Web.WebApi.OwinHost;

namespace ConsoleApplication1
{
    class Program
    {
        static int Main(string[] args)
        {
            var exitCode = HostFactory.Run(c =>
            {
                c.Service<MyService>(service =>
                {
                    ServiceConfigurator<MyService> s = service;
                    s.ConstructUsing(() => new MyService());
                    s.WhenStarted(a => a.Start());
                    s.WhenStopped(a => a.Stop());
                });
            });
            return (int)exitCode;
        }
    }

    public class MyService
    {
        private IDisposable app;
        public void Start()
        {
            app = WebApp.Start("http://localhost:5000/");
        }
        public void Stop()
        {
            app.Dispose();
        }
    }

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var config = new HttpConfiguration();
            config.MapHttpAttributeRoutes();
            config.Formatters.Remove(config.Formatters.XmlFormatter);
            config.Formatters.Add(config.Formatters.JsonFormatter);
            config.EnableSystemDiagnosticsTracing();
            app.UseNinjectMiddleware(CreateKernel).UseNinjectWebApi(config);
        }

        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            //kernel.Load(Assembly.GetExecutingAssembly());
            DependencyInjection(kernel);
            return kernel;
        }

        private static void DependencyInjection(IKernel kernel)
        {
            kernel.Bind<ISampleDependency>().To<SampleDependency1>();
        }
    }

    public class SampleController : ApiController
    {
        private ISampleDependency _dependency;
        public SampleController(ISampleDependency dependency)
        {
            _dependency = dependency;
        }
        [Route("")]
        [HttpGet]
        public IHttpActionResult GetRoot()
        {
            return Ok<string>("This is root.");
        }
        [Route("{id:int}")]
        [HttpGet]
        public IHttpActionResult GetSquare(int id)
        {
            int value;
            try
            {
                value = _dependency.Square(id);
            }
            catch
            {
                return NotFound();
            }
            return Ok<int>(value);
        }
        [Route("{id}")]
        [HttpGet]
        public IHttpActionResult GetString(string id)
        {
            return Ok<string>(string.Format("get string: {0}", id));
        }
    }

    public interface ISampleDependency
    {
        int Square(int id);
    }

    public class SampleDependency1 : ISampleDependency
    {
        public int Square(int id)
        {
            return id * id;
        }
    }

    public class SampleDependency2 : ISampleDependency
    {
        public int Square(int id)
        {
            return id * id * id;
        }
    }
}

以上是关于csharp Topshelf + OWIN自主机+ ASP.NET WebAPI + Ninject的主要内容,如果未能解决你的问题,请参考以下文章

在自主机模式下获取当前的 owin 上下文

具有 OWIN 自主机和 Windows 身份验证的 Web Api

在使用 Owin 自托管的 Web API 中获取远程主机 IP

Owin 自主机 - 无法侦听前缀“http://localhost:12345/”,因为它与机器上的现有注册冲突

OWIN 自托管静态网站

如何在本地服务结构集群中部署和访问来宾可执行文件(ASP.NET OWIN 自托管 webapi 应用程序)