我正在尝试在我的应用程序中使用具有Autofac依赖性的SignalR,我已经成功注册了依赖项,但是在运行时它导致SignalR dyanmic生成的文件集线器丢失的错误,没有我无法做任何事情。我搜索过互联网但找不到任何解决方案。请帮忙。
配置类:
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(IocConfig), "RegisterDependencies")]
namespace Rental.Bootstrapper
{
public class IocConfig
{
public static void RegisterDependencies()
{
var builder = new ContainerBuilder();
const string nameOrConnectionString = "name=DefaultConnection";
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterApiControllers(typeof(WebApiConfig).Assembly);
builder.RegisterHubs(typeof(RentalHub).Assembly);
//builder.RegisterInstance(typeof(Startup));
builder.RegisterModule<AutofacWebTypesModule>();
builder.RegisterGeneric(typeof(EntityRepository<>)).As(typeof(IRepository<>)).InstancePerHttpRequest();
builder.RegisterGeneric(typeof(Service<>)).As(typeof(IService<>)).InstancePerHttpRequest();
builder.RegisterType(typeof(UnitOfWork)).As(typeof(IUnitOfWork)).InstancePerHttpRequest();
builder.Register<IEntitiesContext>(b =>
{
var logger = b.Resolve<ILogger>();
var context = new RentalContext(nameOrConnectionString, logger);
return context;
}).InstancePerHttpRequest();
builder.Register(b => NLogLogger.Instance).SingleInstance();
builder.RegisterModule(new IdentityModule());
var container = builder.Build();
DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver =
new AutofacWebApiDependencyResolver(container);
var signalRDependencyResolver = new Autofac.Integration.SignalR.AutofacDependencyResolver(container);
GlobalHost.DependencyResolver = signalRDependencyResolver;
}
}
}
答案 0 :(得分:0)
我怀疑这根本不是Autofac问题。我还假设您使用MVC4,默认情况下使用Owin。 Owin有一个启动类,必须注册所有中间件和扩展。通常,这是一个Startup.cs类,它至少是SignalR所需的类,包含:
using Microsoft.AspNet.SignalR;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(Startup))]
namespace MyNamepace
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR(new HubConfiguration
{
EnableDetailedErrors = true
});
}
}
}
如果你看一下MapSignalR方法,你会发现它注册了“/ signalr”路由,如果我理解你的问题,这正是你所缺少的:
/// <summary>
/// Maps SignalR hubs to the app builder pipeline at "/signalr".
///
/// </summary>
/// <param name="builder">The app builder</param><param name="configuration">The <see cref="T:Microsoft.AspNet.SignalR.HubConfiguration"/> to use</param>
public static IAppBuilder MapSignalR(this IAppBuilder builder, HubConfiguration configuration)
{
return OwinExtensions.MapSignalR(builder, "/signalr", configuration);
}
Autofac SignalR文档 http://jsfiddle.net/dreamweiver/bp0x1yc4/11/,我强烈建议您查看。它有一个完整的startup.cs文件代码,包括Autofac容器初始化,所有注册和SignalR的Owin相关代码。