2

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.
Steven
  • 166,672
  • 24
  • 332
  • 435
user3547774
  • 1,621
  • 3
  • 20
  • 46

1 Answers1

2

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
        }
    }
}
VineetYadav
  • 783
  • 1
  • 5
  • 10