使用对 SO 上其他问题的回答,我设法使用 Ninject 使依赖注入与 SignalR 一起工作。但它在我的一段代码中被我忽略了一段时间,几周后我回来看它 - 它似乎已经停止工作,我似乎无法弄清楚为什么。
我正在适当地创建我的 Ninject 内核:
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
Kernel = kernel;
return kernel;
}
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
kernel.Bind<ISessionWrapper>().To<SessionWrapper>();
kernel.Bind<IPayboardEntities>().To<PayboardEntities>().InRequestScope();
kernel.Bind<IAuthenticationService>().To<AuthenticationService>().InRequestScope();
kernel.Bind<IAuthorizationService>().To<AuthorizationService>().InRequestScope();
kernel.Bind<ITokenService>().To<TokenService>().InRequestScope();
kernel.Bind<fsIntegrations.RealtimeService>().To<fsIntegrations.RealtimeService>().InRequestScope();
kernel.Bind<IDataServiceFactory>()
.To<fsIntegrations.DataServiceFactory>()
.InSingletonScope()
.WithConstructorArgument("kernel", kernel);
kernel.Bind<IntegrationHub>().To<IntegrationHub>();
kernel.Bind<CustomerHub>().To<CustomerHub>();
}
我正在覆盖 Global.asax.cs 中的 SignalR DependencyResolver:
protected void Application_Start()
{
XmlConfigurator.Configure();
// To support Ninject with SignalR
// See http://stackoverflow.com/questions/14706530/ninject-dependency-injection-for-signalr
GlobalHost.DependencyResolver = new SignalRNinjectDependencyResolver(NinjectWebCommon.Kernel);
// Register the default hubs route: ~/signalr
RouteTable.Routes.MapHubs();
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
BootstrapBundleConfig.RegisterBundles(BundleTable.Bundles);
SimpleSocialAuthBundleConfig.RegisterBundles(BundleTable.Bundles);
}
但是当 SignalR 尝试创建我的任何集线器(我正在使用构造函数注入)时,它会引发此异常:
System.MissingMethodException: No parameterless constructor defined for this object.
这曾经有效,而且我认为我没有改变任何会破坏它的东西 - 尽管显然有些东西做了。
关于我做错了什么的任何建议?