OWIN Startup Class

╄→尐↘猪︶ㄣ 提交于 2019-12-11 22:13:24

问题


Can someone please tell me the exact role of the OWIN startup class. Basically what I am looking for :

  • Whats its purpose
  • When is it called, it is just once or per request
  • Is this a good place to configure my Dependency Injection library.

回答1:


Owin is designed to be pluggable deisgn. there are a set of services which you can change/replace from the configuration. For example in the following configuration, i have

  • enabled webapi
  • Enabled Signalr
  • Enabled Attribute Based routing for Signalr
  • Configured default Dependency Resolver
  • Replaced Logging service with custom logger

So , this way, you can configure the complete configuration. It will be called only once at startup. You can set/use dependency resolver and configure it here, but that depends mainly on your overall design.

public class OwinStartup
{
    //initialize owin startup class and do other stuff
    public void Configuration(IAppBuilder app)
    {
        app.UseWelcomePage("/");
        //var container = new UnityContainer();

        HttpConfiguration httpConfiguration = new HttpConfiguration();
        httpConfiguration.Routes.MapHttpRoute(
            name: "MyDefaultApi",
            routeTemplate: "api/v2/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
            );

        //Maps the attribute based routing
        httpConfiguration.MapHttpAttributeRoutes();

        //Set the unity container as the default dependency resolver
        httpConfiguration.DependencyResolver = new UnityWebApiDependencyResolver(new UnityContainer());

        //replace (or add) the exception logger service to custom Logging service
        httpConfiguration.Services.Replace(typeof(IExceptionLogger), new ExLogger());
        app.UseWebApi(httpConfiguration);

        //Add Signalr Layer
        app.MapSignalR(new HubConfiguration
        {
            EnableJSONP = true,
            EnableDetailedErrors = true
        });
    }

    public class ExLogger : ExceptionLogger
    {
        public override void Log(ExceptionLoggerContext context)
        {
            base.Log(context);
            //do something
        }
    }
}


来源:https://stackoverflow.com/questions/24930185/owin-startup-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!