使用Simple Injector将控制器注入SignalR Hub

时间:2016-07-25 11:31:36

标签: c# signalr simple-injector

我想使用Simple Injector将单个控制器注入我的集线器。

我已经尝试了以下内容,但我现在获得状态500作为回应。

中心代码:

public class EventDataHub : Hub
    {
        private static IEventDataController _dataController;

        public EventDataHub(IEventDataController dataController)
        {
            _dataController = dataController;
        }

        public void Subscribe(string signal)
        {
            _dataController.Subscribe(signal, Context.ConnectionId);
        }
    }

Startup.cs

public class Startup
    {
        public void Configuration(IAppBuilder app)
        {

            var container = new Container();

            var hybridLifestyle = Lifestyle.CreateHybrid(
                lifestyleSelector: () => HttpContext.Current != null,
                trueLifestyle: new WebRequestLifestyle(),
                falseLifestyle: new LifetimeScopeLifestyle());

            container.Register<IEventDataController, EventDataController>(Lifestyle.Singleton);
            container.Register<IHub, EventDataHub>(hybridLifestyle);

            container.Verify();

            var activator = new SimpleInjectorHubActivator(container);
            GlobalHost.DependencyResolver.Register(typeof(IHubActivator), () => activator);

            app.MapSignalR();
        }
    }

Hub激活器

public class SimpleInjectorHubActivator : IHubActivator
    {
        private readonly Container _container;

        public SimpleInjectorHubActivator(Container container)
        {
            _container = container;
        }

        public IHub Create(HubDescriptor descriptor)
        {
            return (IHub)_container.GetInstance(descriptor.HubType);
        }
    }

Subscribe方法似乎无法从客户端访问。中心构造函数已执行。

异常堆栈:

  

[MissingMethodException:没有为此对象定义无参数构造函数]
  System.RuntimeTypeHandle.CreateInstance(RuntimeType类型,布尔值   publicOnly,Boolean noCheck,Boolean&amp; canBeCached,   RuntimeMethodHandleInternal&安培; ctor,布尔&amp; bNeedSecurityCheck)+0
  System.RuntimeType.CreateInstanceSlow(Boolean publicOnly,Boolean   skipCheckThis,Boolean fillCache,StackCrawlMark&amp; stackMark)+113
  System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly,   Boolean skipCheckThis,Boolean fillCache,StackCrawlMark&amp; stackMark)   +206 System.Activator.CreateInstance(Type type,Boolean nonPublic)+83 System.Activator.CreateInstance(Type type)+11 Microsoft.AspNet.SignalR.Hubs.DefaultHubActivator.Create(HubDescriptor)   描述符)+84
  Microsoft.AspNet.SignalR.Hubs.DefaultHubManager.ResolveHub(字符串   hubName)+27
  Microsoft.AspNet.SignalR.Hubs.HubDispatcher.CreateHub(IRequest   请求,HubDescriptor描述符,String connectionId,   StateChangeTracker跟踪器,布尔throwIfFailedToCreate)+386
  Microsoft.AspNet.SignalR.Hubs.HubDispatcher.OnReceived(IRequest   request,String connectionId,String data)+400
  Microsoft.AspNet.SignalR&LT;&GT; c__DisplayClass64_1.b__5()   +34 Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethod(Func`1 func)+28
  System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)+92
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)+58
  Microsoft.AspNet.SignalR.Transports.d__40.MoveNext()   +742 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)+92
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)+58 Microsoft.Owin.Mapping.d__0.MoveNext()+385
  System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)+92
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)+58
  Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.d__5.MoveNext()   +187 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)+92
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)+58
  Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.d__2.MoveNext()   +185 Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult)   ar)+69
  Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult的   ar)+64
  System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()   +380 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+155

修改

将DI配置从Startup.cs移动到global.asax会引发此异常:

  

[ArgumentNullException:value不能为null。参数名称:   s] System.IO.StringReader..ctor(String s)+11377176
  Microsoft.AspNet.SignalR.Json.JsonSerializerExtensions.Parse(JsonSerializer   serializer,String json)+63
  Microsoft.AspNet.SignalR.Hubs.HubRequestParser.Parse(String data,   JsonSerializer序列化程序)+21
  Microsoft.AspNet.SignalR.Hubs.HubDispatcher.OnReceived(IRequest   request,String connectionId,String data)+40
  Microsoft.AspNet.SignalR&LT;&GT; c__DisplayClass64_1.b__5()   +34 Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethod(Func`1 func)+28
  System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)+92
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)+58
  Microsoft.AspNet.SignalR.Transports.d__40.MoveNext()   +742 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)+92
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)+58 Microsoft.Owin.Mapping.d__0.MoveNext()+385
  System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)+92
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)+58
  Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.d__5.MoveNext()   +187 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)+92
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)+58
  Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.d__2.MoveNext()   +185 Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult)   ar)+69
  Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult的   ar)+65
  System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()   +380 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+155

1 个答案:

答案 0 :(得分:1)

我终于找到了问题:

container.Verify()正在打破IHubActivator的注册。所以它必须在之后或从不被称为。

此外,我删除了IHub的容器注册,因为它现在没有用。 (我添加了它,因为container.Register<IEventDataController, EventDataController>(Lifestyle.Singleton)当时没有工作并且以某种方式修复了它)

所以我在Startup.cs中的最终代码如下所示:

public void Configuration(IAppBuilder app)
{
    var container = new Container();

    container.Register<IEventDataController, EventDataController>(Lifestyle.Singleton);

    var activator = new SimpleInjectorHubActivator(container);
    GlobalHost.DependencyResolver.Register(typeof(IHubActivator), () => activator);

    app.MapSignalR();
}