将参数传递给OWIN主机

Leo*_*Leo 10 c# owin

我使用OWIN自我托管ASP.NET Web API和SignalR.我使用以下代码启动服务器(在控制台应用程序上):

using (WebApplication.Start<Startup>(url))
{
    Console.WriteLine("Running...");
    Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)

这很好用.但现在我需要将一个参数(一个对象)传递给Startup该类.如何才能做到这一点?

Leo*_*Leo 10

WebApplication.Start方法有一个接受IServiceProvideras参数的重载,因此可以注入我想要的数据.

IServiceProvider serviceProvider = DefaultServices.Create(defaultServiceProvider =>
{
    defaultServiceProvider.AddInstance<IMyInterface>(myInstance);
});

using (WebApplication.Start<Startup>(serviceProvider, url)){ ... }
Run Code Online (Sandbox Code Playgroud)

现在,在我的Startup课上我只需要创建一个接收IMyInterface的构造函数:

public Startup(IMyInterface myInstance)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)