SignalR 中的简单注入器注册问题
Simple Injector registration problem in SignalR
我在我的控制器中设置了 DI,如下所示,并绑定到注册 IHubContext,如它在
上所见
控制器:
public class DemoController : Controller
{
private IHubContext<DemoHub> context;
public DemoController(IHubContext<DemoHub> context)
{
this.context = context;
}
}
Global.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
}
但是当我调试我的应用程序时,遇到“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'”错误。那么,如何正确注册 IHubContext 呢?
由于 ASP.NET MVC
没有为 SignalR
中心上下文内置依赖注入,您必须使用 GlobalHost.ConnectionManager
获取上下文实例。有了这个,您可以向创建 IHubContext
实例的容器注册依赖项。考虑到您输入了 hub
public class DemoHub : Hub<ITypedClient>
{
}
和界面
public interface ITypedClient
{
void Test();
}
注册依赖如下
container.Register<IHubContext<ITypedClient>>(() =>
{
return GlobalHost.ConnectionManager.GetHubContext<DemoHub, ITypedClient>();
}, Lifestyle.Scoped);
控制器应该看起来像
public class DemoController : Controller
{
private IHubContext<ITypedClient> context;
public DemoController(IHubContext<ITypedClient> context)
{
this.context = context;
}
}
我在我的控制器中设置了 DI,如下所示,并绑定到注册 IHubContext,如它在
上所见控制器:
public class DemoController : Controller
{
private IHubContext<DemoHub> context;
public DemoController(IHubContext<DemoHub> context)
{
this.context = context;
}
}
Global.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
}
但是当我调试我的应用程序时,遇到“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'”错误。那么,如何正确注册 IHubContext 呢?
由于 ASP.NET MVC
没有为 SignalR
中心上下文内置依赖注入,您必须使用 GlobalHost.ConnectionManager
获取上下文实例。有了这个,您可以向创建 IHubContext
实例的容器注册依赖项。考虑到您输入了 hub
public class DemoHub : Hub<ITypedClient>
{
}
和界面
public interface ITypedClient
{
void Test();
}
注册依赖如下
container.Register<IHubContext<ITypedClient>>(() =>
{
return GlobalHost.ConnectionManager.GetHubContext<DemoHub, ITypedClient>();
}, Lifestyle.Scoped);
控制器应该看起来像
public class DemoController : Controller
{
private IHubContext<ITypedClient> context;
public DemoController(IHubContext<ITypedClient> context)
{
this.context = context;
}
}