我对 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’的范围。”
无论我做什么,我似乎都无法让它工作,如果有人能告诉我上面的代码有什么问题,我将不胜感激。
非常感谢狮子座