在Signalr 2.0自主机中使用依赖注入?

Bra*_*non 5 c# asp.net dependency-injection self-hosting signalr

在自托管应用程序中使用SignalR 2.0,根据这些说明,您可以这样:

class Startup
{
   public void Configuration(IAppBuilder app)
   {
       app.MapSignalR(new HubConfiguration { Resolver = ... });
   }
}
class Program
{
    static void Main(string[] args)
    {
        using (WebApp.Start("http://localhost:8080")) // constructs Startup instance internally
        {
            Console.WriteLine("Server running on {0}", url);
            Console.ReadLine();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

您会注意到Startup类实例是使用一些幕后魔术创建的.我无法弄清楚如何填写依赖关系.有没有办法覆盖Startup类的构造,以便我可以注入依赖项?

hal*_*r73 3

您可以简单地向 Katana 的 ServiceProvider 注册 Startup 的构造函数参数,而不用替换 IAppActivator。

默认的 IAppActivator 将为您解析与 Startup 构造函数的参数类型匹配的任何服务。唯一的缺点是您不能使用 WebApp.Start,因为它不会公开 ServiceProvider:

public class MyService : IMyService
{
    private readonly IMyOtherService _myOtherService;

    // Services will be recursively resolved by Katana's ServiceProvider
    public MyService(IMyOtherService myOtherService)
    {
        _myOtherService = myOtherService;
    }

    // Implementation
}

public class Startup
{
   private readonly IMyService _myService;

   // Startup must have exactly one constructor.
   public Startup(IMyService myService)
   {
       _myService = myService
   }

   public void Configuration(IAppBuilder app)
   {
       app.MapSignalR(new HubConfiguration { Resolver = ... });
   }
}
Run Code Online (Sandbox Code Playgroud)
using System;
using Microsoft.Owin.Hosting;
using Microsoft.Owin.Hosting.Services;
using Microsoft.Owin.Hosting.Starter;

public class Program
{
    static void Main(string[] args)
    {
        var url = "http://localhost:8080";

        var services = (ServiceProvider)ServicesFactory.Create();
        var options = new StartOptions(url);

        services.Add<IMyOtherService, MyOtherService>();
        services.Add<IMyService, MyService>();

        var starter = services.GetService<IHostingStarter>();

        using (starter.Start(options)) // constructs Startup instance internally
        {
            Console.WriteLine("Server running on {0}", url);
            Console.ReadLine();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我将 WebApp.Start 的默认实现复制到 Program.Main 中,但不是立即调用 IHostingStarter.Start,而是首先添加自定义服务:http://katanaproject.codeplex.com/SourceControl/changeset/view/c726b87e90c05677a256ca1821bac481f402d6bd#src/Microsoft .Owin.Hosting/WebApp.cs

ServiceProvider 还有很多其他重载。如果需要,请添加:http://msdn.microsoft.com/en-us/library/microsoft.owin.hosting.services.serviceprovider (v=vs.111).aspx

这应该比我在之前的答案中建议的使用 StartOptions.Settings 替换 Katana 的 IAppActivator 简单得多。

不过,我将保留之前的答案,因为它确实更详细地解释了 Startup 类的构造方式以及如何使用 Settings 字典替换默认服务实现。

  • 这实在是太复杂了。将 Startup 类中的内容移至 lambda 函数中,如下所示:`WebApp.Start(baseAddress, app =&gt; { app.MapSignalR(new HubConfiguration { Resolver = ... }); }`。然后您不需要将东西注入到启动类中,您可以将它们注入到名为 WebApp.Start 的类中,并通过闭包引用它们。 (2认同)