[英]Simple Injector registration problem in SignalR
I set DI in my Controller as shown below and tied to register IHubContext as it seen on我如下所示在我的控制器中设置了 DI 并绑定到注册 IHubContext,就像它在上面看到的那样
Controller:控制器:
public class DemoController : Controller
{
private IHubContext<DemoHub> context;
public DemoController(IHubContext<DemoHub> context)
{
this.context = context;
}
}
Global.asax:全球.asax:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
BundleConfig.RegisterBundles(BundleTable.Bundles);
var container = new Container();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
container.Register<IHubContext, IHubContext>(Lifestyle.Scoped);
// or
container.Register<IHubContext>(Lifestyle.Scoped);
// code omitted
}
But when I debug my app, encounter " System.ArgumentException: 'The given type IHubContext is not a concrete type. Please use one of the other overloads to register this type. Parameter name: TImplementation' " error.但是,当我调试我的应用程序时,遇到“ System.ArgumentException: 'The given type IHubContext is not a specific type. Please use other重载之一来注册此类型。Parameter name: TImplementation' “错误。 So, how can I register IHubContext properly?那么,如何正确注册 IHubContext 呢?
Since ASP.NET MVC
doesn't have built in dependency injection for SignalR
hub context you have to obtain a context instance using GlobalHost.ConnectionManager
.因为ASP.NET MVC
没有内置的依赖注入SignalR
枢纽环境下,你必须获得使用上下文实例GlobalHost.ConnectionManager
。 With this you can register a dependency with your container that creates IHubContext
instance.有了这个,您可以向创建IHubContext
实例的容器注册依赖IHubContext
。 Considering you have typed hub考虑到您输入了 hub
public class DemoHub : Hub<ITypedClient>
{
}
and interface和界面
public interface ITypedClient
{
void Test();
}
register dependency as the following注册依赖如下
container.Register<IHubContext<ITypedClient>>(() =>
{
return GlobalHost.ConnectionManager.GetHubContext<DemoHub, ITypedClient>();
}, Lifestyle.Scoped);
And the controller should look like控制器应该看起来像
public class DemoController : Controller
{
private IHubContext<ITypedClient> context;
public DemoController(IHubContext<ITypedClient> context)
{
this.context = context;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.