asp.net-mvc – 在MVC 5应用程序中使用autofac in为SignalR Hub
我正在尝试将SignalR 2引入现有项目,其中使用autofac执行所有依赖项注入,并且所有依赖项配置都在Global.asax中执行.我找到了使用带有autofac的SignalR及其
accompanying documentation的Autofac.SignalR软件包.
我按照提供的文档中的示例进行操作,并遵循使用RegisterHubs函数的建议,而不是定义我的各个集线器依赖项. 不幸的是,当我尝试从lifetimeScope解析依赖项时,我的Hub类会出现以下运行时错误 Autofac.Core.DependencyResolutionException was unhandled by user code HResult=-2146233088 Message=No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which instance was requested. This generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime,never from the container itself. 我无法让DependencyResolver.Current或ILifeTimeScopeProvider为我工作. 我的依赖配置如下 var builder = new ContainerBuilder(); .RegisterControllers(typeof (MvcApplication).Assembly); .RegisterHubs(Assembly.GetExecutingAssembly()); ... var container = builder.Build(); // Set dependency resolver for MVC DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); // Set dependency resolver for Web API GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container); // Set the dependency resolver for SignalR DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); var signalRDependencyResolver = new Autofac.Integration.SignalR.AutofacDependencyResolver(container); GlobalHost.DependencyResolver = signalRDependencyResolver; 我还根据示例设置了我的集线器类: public class BaseHub : Hub { protected readonly ILifetimeScope _hubLifetimeScope; private static IUserSignalRConnectionRepository _userSignalRConnectionRepository; public BaseHub(ILifetimeScope lifetimeScope) : base() { _hubLifetimeScope = lifetimeScope.BeginLifetimeScope(); _userSignalRConnectionRepository = _hubLifetimeScope.Resolve<IUserSignalRConnectionRepository>(); } 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); } } 例外情况发生在该行的cub类中 _userSignalRConnectionRepository = _hubLifetimeScope.Resolve<IUserSignalRConnectionRepository>(); 解决方法
您应该包含注册IUserSignalRConnectionRepository的代码.
错误消息似乎表明此依赖项是使用InstancePerHttpRequest()注册的,相当于InstancePerMatchingLifetimeScope(“AutofacWebRequest”).在MVC请求的情况下,此范围是自动创建的,但不是针对SignalR请求(这可能是一件好事,因为它们可以无限期地持续). 您可以通过调用lifetimeScope.BeginLifetimeScope(“AutofacWebRequest”);来解决此问题.而不是lifetimeScope.BeginLifetimeScope();在你的Hub构造函数中. 或者,您可以使用InstancePerDependency()(默认值)或SingleInstance()而不是InstancePerHttpRequest()来注册IUserSignalRConnectionRepository. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 我有多个用户,我可以锁定网页,以便一次只有一个
- asp.net-mvc – 为新的网上商店项目选择什么(在ASP.NET MVC
- asp.net-mvc – ASP.Net MVC:发送JSON到控制器
- 如何在ASP.Net项目中包含jQuery?
- asp.net-mvc – 对Mono XSP托管的Web应用程序的第一次浏览器
- ASP.NET中的会话修复
- ASP.NET自定义控件 – 什么是最好的方式来包括嵌入式CSS引用
- asp.net-mvc – 如何只允许mvc4 razor中的文本框中的数字
- asp.net-mvc – 绑定排除Asp.net MVC不适用于LINQ实体
- asp.net – Web部署项目的更好替代品
- ASP.NET Core 运行原理剖析
- asp.net-mvc – 将ASP.NET应用程序部署到多个EC2
- 启用禁用按钮asp .net – 使用javascript
- asp.net – Request.Browser.Platform不返回iPad
- Autofac和ASP.NET Web API ApiController
- asp.net-mvc – ASP.NET MVC4 CustomErrors Defa
- asp.net-mvc – 带有lambda表达式的ASP.net MVC
- asp.net-mvc – MVC服务层 – 每个控制器或其他设
- asp.net – 在.NET 4下编译时出现“具有相同密钥
- asp.net-mvc – asp.net mvc在控制器构建期间的任