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

时间:2014-01-27 18:47:13

标签: c# asp.net dependency-injection signalr self-hosting

在自托管应用程序中使用SignalR 2.0,来自these instructions,你有类似的东西:

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();
        }
    }
}

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

3 个答案:

答案 0 :(得分: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 = ... });
   }
}

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();
        }
    }
}

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

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

这应该比使用StartOptions替换Katana的IAppActivator要简单得多。如我在之前的回答中建议的那样。

我将离开之前的答案,因为它更详细地解释了如何构建Startup类以及如何使用Settings字典替换默认服务实现。

答案 1 :(得分:2)

在此处查看依赖项注入信息:http://www.asp.net/signalr/overview/signalr-20/extensibility/dependency-injection

应该有你需要知道的一切:)

希望这有帮助!

答案 2 :(得分:0)

class Startup
{
   private readonly IDependencyResolver _resolver;

   public Startup(IDependencyResolver resolver)
   {
        _resolver = resolver;
   }

   public void Configuration(IAppBuilder app)
   {
       app.MapSignalR(new HubConfiguration { Resolver = _resolver; });
   }
}

class Program
{
    static void Main(string[] args)
    {
        Startup startup = new Statrtup(new MyResolver());
        using (WebApp.Start("http://localhost:8080", startup.Configuration))
        {
            Console.WriteLine("Server running on {0}", url);
            Console.ReadLine();
        }
    }
}