我在当前的Asp项目中使用autofac,一切正常,直到我决定在signalR Hub中使用依赖注入
这是我的启动课
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();
builder.RegisterType<DbFactory>().As<IDbFactory>().InstancePerLifetimeScope();
//builder.RegisterHubs(Assembly.GetExecutingAssembly());
builder.RegisterType<DiscussionHub>();
// Repositories
builder.RegisterAssemblyTypes(typeof(LanguagesRepository).Assembly)
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces().InstancePerRequest();
// Services
builder.RegisterAssemblyTypes(typeof(LanguageService).Assembly)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces().InstancePerRequest();
IContainer container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
var config = new HubConfiguration
{
Resolver = new Autofac.Integration.SignalR.AutofacDependencyResolver(container)
};
app.UseAutofacMiddleware(container);
AutoMapperConfiguration.Configure();
app.MapSignalR("/signalr",config);
}
}
这是我的中心
public class DiscussionHub : Hub
{
private readonly IDiscussionService _discussionService;
public DiscussionHub(IDiscussionService discussionService)
{
_discussionService = discussionService;
}}
错误是我的Hub上没有无参数构造函数?有什么建议吗?!
答案 0 :(得分:2)
您应该注册您的中心ExternallyOwned
它应该自己管理lifetimescope。这意味着autofac不会处理它们。
其次,所有内容都将从您的集线器中的根容器中解析出来。这意味着Per Dependency
或Per LifeTimeScope
将与您的中心(永远与应用程序)共存。因此,您应该管理集线器的生命周期。
即使我们在您的中心管理生活时间,也不会支持Per Request
。因此,当我们创建新的lifetimescope时,我们将使用AutofacWebRequest
标记创建它。这样,我们就可以解析您的Per Request
实例。但请注意,此实例将与普通请求lifetimescope中的其他实例完全不同。
你的中心应该是这样的:
public class DiscussionHub : Hub
{
private readonly ILifetimeScope _hubLifetimeScope;
private readonly IDiscussionService _discussionService;
public MyHub(ILifetimeScope lifetimeScope)
{
// Create a lifetime scope for the hub.
_hubLifetimeScope = lifetimeScope.BeginLifetimeScope("AutofacWebRequest");
// Resolve dependencies from the hub lifetime scope.
_discussionService = _hubLifetimeScope.Resolve<IDiscussionService>();
}
protected override void Dispose(bool disposing)
{
// Dipose the hub lifetime scope when the hub is disposed.
if (disposing && _hubLifetimeScope != null)
{
_hubLifetimeScope.Dispose();
}
base.Dispose(disposing);
}
}
您的注册应该是这样的:
.
.
builder.RegisterType<DiscussionHub>().ExternallyOwned();
var container = builder.Build();
GlobalHost.DependencyResolver = new Autofac.Integration.SignalR.AutofacDependencyResolver(container);
.
.
Owin整合:
public void Configuration(IAppBuilder app)
{
var builder = new ContainerBuilder();
// STANDARD SIGNALR SETUP:
// Get your HubConfiguration. In OWIN, you'll create one
// rather than using GlobalHost.
var config = new HubConfiguration();
// Register your SignalR hubs.
builder.RegisterHubs(Assembly.GetExecutingAssembly());
// Set the dependency resolver to be Autofac.
var container = builder.Build();
config.Resolver = new AutofacDependencyResolver(container);
// OWIN SIGNALR SETUP:
// Register the Autofac middleware FIRST, then the standard SignalR middleware.
app.UseAutofacMiddleware(container);
app.MapSignalR("/signalr", config);
}
查看更多detail。