Autofac将数据注入OWIN Startup类 [英] Autofac Injection of data into OWIN Startup class

查看:324
本文介绍了Autofac将数据注入OWIN Startup类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在四处寻找SO,但是找不到我的特定问题的答案:

I've been looking around on SO for a while and can't find the answer to my particular question:

如何使用Autofac(通过构造函数或属性)将数据注入OWIN Startup类?

How do you use Autofac to inject data (via constructor or property) into the OWIN Startup class?

我在Windows服务中使用Autofac,该服务还处理后端服务等的创建,因此我在其中进行所有配置读取和容器构建.

I'm using Autofac in a Windows service which also handles creation of back-end services etc. so I'm doing all the configuration reading and container building in there.

我希望能够注册Startup类,以便可以为CORS注入允许的源,但是即使在我使用如下属性注册对象时也是如此:

I want to be able to register the Startup class so I can inject allowed origins for CORS but even when I register the object with a property like so:

var builder = new ContainerBuilder();
// This will actually be read from config.
var origins = new[]
              {
                  "http://localhost",
                  "http://localhost:8082",
              };
builder.Register(c => new Startup(origins));
var container = builder.Build();

在运行时实例化Startup类时,调用堆栈显示它来自外部代码,而我的Autofac生成器未将该属性压入.

At runtime when the Startup class is instantiatedthe callstack shows it comes from external code and my Autofac builder didn't push the property in.

理想情况下,我想将Autofac注册保存在一个位置(Windows服务类),然后将所需的数据注入到Startup中,这样我就可以在下面做类似的事情(其中allowedOrigins被设置为属性或通过构造函数注入)

I'd ideally like to keep my Autofac registrations in a single place (the Windows service class) and just inject the required data to Startup so I can just do something like this below (where allowedOrigins is set as a property or injected via the constructor)

 public void Configuration(IAppBuilder app)
 {
     var configuration = new HttpConfiguration();
     ...
     var origins = string.Join(",", allowedOrigins);
     var cors = new EnableCorsAttribute(origins, "*", "*") { SupportsCredentials = true };
     configuration.EnableCors(cors);
     ....
 }

任何想法都将不胜感激. 谢谢

Any ideas would be much appreciated. Thanks

peteski

更新

我应该补充一点,在尝试注册并构建Autofac之后,我通过执行以下操作启动了OWIN自托管应用程序:

I should add that after attempting Autofac registration and building, I was kicking off the OWIN self host app by doing:

var webApp = WebApp.Start<Startup>(baseAddress);

在与朋友的交谈中,他们建议创建Startup对象并将其传递给WebApp:

From a conversation with a friend they suggested creating the Startup object and passing it to the WebApp:

var startup = new Startup(origins);
var webApp = WebApp.Start(startup, baseAddress);

在这里,我将IEnumerable<string>的起源添加到Startup类的ctor中.这确实有效!但是感觉就像使用Autofac来处理启动类所需的东西一样.

Here, I'd add the IEnumerable<string> origins to the ctor for the Startup class. This does actually work! But feels like it goes around using Autofac to handle giving the Startup class what it needs.

推荐答案

现成的Autofac不支持您要的内容.

Out of the box Autofac doesn't support what you're looking for.

在IIS或其他非自托管环境中,没有用于构建容器然后注入到启动类中的钩子.有点像鸡/蛋的问题-启动类是您开始构建容器的钩子.

In an IIS or other non-self-hosted environment, there's no hook for building a container and then injecting into the startup class. It's sort of a chicken/egg problem - the startup class is your hook to start building the container.

在自托管环境中,您提到的建议-创建启动对象并将其传递给对WebApp.Start的调用-是您的最佳选择.

In a self-hosted environment, the suggestion you mentioned - creating the startup object and passing that into the call to WebApp.Start - is your best bet.

基本上,在某些时候,您将到达应用程序的入口点(通常在OWIN中是启动类),这就是您必须构建容器并成为解决和启动事情的引导者的时候向上. Autofac无法为您提供帮助-这取决于您使用的框架(例如MVC,OWIN,Web API等).

Basically, at some point you're going to hit the entry point for your application (usually in OWIN that's the startup class) and that's the point at which you have to build your container and be the bootstrapper for resolving and starting things up. Autofac can't help you with that - that's up to the framework(s) you're using (e.g., MVC, OWIN, Web API, etc.).

这篇关于Autofac将数据注入OWIN Startup类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆