有人可以告诉我 OWIN 启动类的确切作用吗?基本上我正在寻找什么:
- 它的目的是什么
- 何时调用,仅调用一次或每次请求
- 这是配置我的依赖注入库的好地方吗?
Owin 被设计为可插拔设计。您可以从配置中更改/替换一组服务。例如在以下配置中,我有
- 启用 webapi
- 启用信号器
- 为 Signalr 启用基于属性的路由
- 配置默认依赖解析器
- 用自定义记录器替换了记录服务
这样,就可以配置完整的配置了。它只会在启动时调用一次。您可以在此处设置/使用依赖解析器并对其进行配置,但这主要取决于您的整体设计。
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
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)