将参数传递给OWIN主机 [英] Pass a parameter to OWIN host
本文介绍了将参数传递给OWIN主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用OWIN自托管ASP.NET Web API和SignalR.我使用以下代码启动服务器(在控制台应用程序上):
I'm self-hosting ASP.NET Web API and SignalR using OWIN. I start the server (on a console app) with this code:
using (WebApplication.Start<Startup>(url))
{
Console.WriteLine("Running...");
Console.ReadLine();
}
这很好.但是现在我需要将参数(对象)传递给Startup
类.该怎么办?
This works fine. But now I need to pass a parameter (an object) to the Startup
class. How can this be done?
推荐答案
WebApplication.Start
方法具有一个接受IServiceProvider
作为参数的重载,因此可以注入我想要的数据.
The WebApplication.Start
method has an overload that accepts a IServiceProvider
as parameter, so it is possible to inject the data I want.
IServiceProvider serviceProvider = DefaultServices.Create(defaultServiceProvider =>
{
defaultServiceProvider.AddInstance<IMyInterface>(myInstance);
});
using (WebApplication.Start<Startup>(serviceProvider, url)){ ... }
现在,在我的Startup
类上,我只需要创建一个接收IMyInterface的构造函数:
Now, on my Startup
class I only need to create a constructor that receives the IMyInterface:
public Startup(IMyInterface myInstance)
{
...
}
这篇关于将参数传递给OWIN主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文