4

我对 autofac 和 singalR 库都是全新的,所以请放轻松!我在引导程序中有以下代码,它可以在没有signalR的情况下自行工作。

var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces();

builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerHttpRequest();
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerHttpRequest();

builder.RegisterAssemblyTypes(typeof(adminRepository).Assembly).Where(t => t.Name.EndsWith("Repository")).AsImplementedInterfaces().InstancePerHttpRequest();

builder.RegisterAssemblyTypes(typeof(adminService).Assembly).Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces().InstancePerHttpRequest();

builder.RegisterFilterProvider();
IContainer container = builder.Build();

DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(container));

上面的代码工作得很好,但是在用下面的代码注册我的集线器之后,它就不起作用了。

builder.RegisterType<Chat>().InstancePerLifetimeScope();

builder.RegisterFilterProvider();
IContainer container = builder.Build();

DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(container));

SignalR.IDependencyResolver resolver = new SignalR.Autofac.AutofacDependencyResolver(container);
GlobalHost.DependencyResolver = resolver;
RouteTable.Routes.MapHubs(resolver);

我正在使用 SignalR 依赖解析器从这个(https://github.com/pszmyd/SignalR.Autofac)绑定到 Autofac。

我有一个像这样的简单集线器,

public class Chat : Hub
{
    private readonly IadminService adminService;
    public Chat(IadminService adminService)
    {
        this.adminService = adminService;
    }

    public void Send(string message)
    {
        Clients.addMessage(message);
    }
}

这是我尝试使用 DI 连接集线器时遇到的错误。

“从请求实例的范围内看不到标签匹配‘httpRequest’的范围。”

无论我做什么,我似乎都无法让它工作,如果有人能告诉我上面的代码有什么问题,我将不胜感激。

非常感谢狮子座

4

2 回答 2

3

对于托管在 IIS 中的 ASP.NET 应用程序,请将其添加到 Application_Start:

var container = AutofacConfig.BuildContainer();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
var signalRDependencyResolver = new SignalRAutofacDependencyResolver(container);
// old SignalR 1.0 way - routes.MapHubs(signalRDependencyResolver);
RouteTable.Routes.MapHubs(new HubConfiguration { Resolver = signalRDependencyResolver });

您可以在此处找到 SignalRAutofacDependencyResolver 。

注入 ISecurity 服务的示例集线器:

public class ExampleHub : Hub
{
    private static int _count = 0;

    private readonly ISecurity _security;
    public ExampleHub(ISecurity security)
    {
        _security = security;
    }

    public void GetCount()
    {
        _count++;
        Clients.All.SetCount(_count);         
    }
}

这也适用于 SignalR self host release。只需使用这样的启动类:

// These are static variables in Program.cs - Probably a better way to do this
_container = AutofacConfig.BuildContainer();
_webServer = WebApp.Start<WebServerStartup>("http://localhost:8080");

public class WebServerStartup
{
    private readonly SignalRAutofacDependencyResolver _signalRDependencyResolver;

    public WebServerStartup()
    {
        _signalRDependencyResolver = new SignalRAutofacDependencyResolver(_container); 
    }

    public void Configuration(IAppBuilder app)
    {
        app.MapSignalR(new HubConfiguration { Resolver = _signalRDependencyResolver });
    }
}
于 2013-09-13T15:24:18.500 回答
1

设置 DependencyResolver 后,您是否仍需要将其传递给 MapHubs,那就试试这个:

GlobalHost.DependencyResolver = resolver;

RouteTable.Routes.MapHubs();

我知道 SignalR 但从未使用过 Autofac 但可能值得查看这些答案以了解有关可能的 Autofac 问题的详细信息:

如何解决 Autofac InstancePerHttpRequest

Autofac、ASP.NET MVC 3 httpRequest 范围和 AutoMapper:没有标签匹配“httpRequest”的范围可见

于 2012-09-25T13:31:12.983 回答