Pass a parameter to OWIN host

前端 未结 2 838
遥遥无期
遥遥无期 2021-02-20 08:04

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(url))
         


        
2条回答
  •  攒了一身酷
    2021-02-20 08:37

    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(myInstance);
    });
    
    using (WebApplication.Start(serviceProvider, url)){ ... }
    

    Now, on my Startup class I only need to create a constructor that receives the IMyInterface:

    public Startup(IMyInterface myInstance)
    {
        ...
    }
    

提交回复
热议问题