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的主要内容,如果未能解决你的问题,请参考以下文章

csharp C#中的Topshelf使用

csharp C#中的Topshelf使用

csharp Topshelf与Windsor的基本用法

csharp 示例Topshelf服务,演示如何使用Fooidity Switchyard切换功能

OWIN 自主机中等效的会话变量

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