本文介绍了如何在我的ASP.NET MVC应用程序5.2.3其他地方得到IAppBuilder的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要建立一个Owin中间件对象,但不是从启动
类中。我需要从我的code别处内部建立,所以我需要在应用程序的的AppBuilder
实例的引用。是否有一种方式来获得从其他地方?
I need to build an Owin middle-ware object but not from within the Startup
class. I need to build it from within anywhere else in my code, so I need a reference to the AppBuilder
instance of the application. Is there a way to get that from anywhere else?
推荐答案
您可以简单地注入的AppBuilder
本身 OwinContext
。但由于Owin方面只支持的IDisposable
对象,把它包在的IDisposable
对象,并对其进行注册。
You could simply inject AppBuilder
itself to OwinContext
. But since Owin context only supports IDisposable
object, wrap it in IDisposable
object and register it.
public class AppBuilderProvider : IDisposable
{
private IAppBuilder _app;
public AppBuilderProvider(IAppBuilder app)
{
_app = app;
}
public IAppBuilder Get() { return _app; }
public void Dispose(){}
}
public class Startup
{
// the startup method
public void Configure(IAppBuilder app)
{
app.CreatePerOwinContext(() => new AppBuilderProvider(app));
// another context registrations
}
}
所以在你到处code你有机会获得 IAppBuilder
对象。
public class FooController : Controller
{
public ActionResult BarAction()
{
var app = HttpContext.GetOwinContext().Get<AppBuilderProvider>().Get();
// rest of your code.
}
}
这篇关于如何在我的ASP.NET MVC应用程序5.2.3其他地方得到IAppBuilder的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!