OWIN启动类 [英] OWIN Startup Class
本文介绍了OWIN启动类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以告诉我OWIN启动类的确切角色吗?基本上我在找什么:
Can someone please tell me the exact role of the OWIN startup class. Basically what I am looking for :
- 它的用途是什么
- 何时调用,仅一次或每个请求
- 这是配置依赖注入库的好地方.
推荐答案
Owin被设计为可插入的deisgn.您可以通过配置更改/替换一组服务.例如,在以下配置中,我有
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
- 启用了webapi
- 已启用信号器
- 为Signalr启用了基于属性的路由
- 配置的默认依赖项解析器
- 使用自定义日志记录器替换日志记录服务
因此,通过这种方式,您可以配置完整的配置. 在启动时将仅被调用一次.您可以在此处设置/使用依赖关系解析器并对其进行配置,但这主要取决于您的总体设计.
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
}
}
}
这篇关于OWIN启动类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文