我有SignalR的所有Nuget Bits,我正在尝试使用我自己的clientID以及SignalR附带的依赖注入容器,用于我所有其他存储库等。现在奇怪的是这个连接到集线器的jQuery失败了:
debugger;
// Proxy created on the fly
var chat = $.connection.chat;
基本上,聊天对象变得不确定,好像无法解析SignalR。一旦我尝试用下面的代码覆盖SignalR的默认解析器,就会发生这种情况。
我在这里缺少什么?
我遇到的另一个问题是我不确定我的UserClientIDfactory是否实现了IConnectionIdFactory 正在努力。
这是我的Global.asax中的MVC3代码
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
RegisterServices(kernel);
return kernel;
}
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<UserIdClientIdFactory>()
.To<UserIdClientIdFactory>()
.InRequestScope();
//Rest of the other stuff to inject
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
//ninject calls to create the kernal etc
IKernel kernel = CreateKernel();
//TO DO using signal IR resolver
var resolver = new NinjectDependencyResolver(kernel);
SignalR.Hosting.AspNet.AspNetHost.SetResolver(resolver);
}
最后,这是我的自定义clientIDfactory的代码
public class UserIdClientIdFactory : IConnectionIdFactory
{
#region IConnectionIdFactory Members
string IConnectionIdFactory.CreateConnectionId(SignalR.Hosting.IRequest request)
{
// get and return the UserId here, in my app it is stored
// in a custom IIdentity object, but you get the idea
return HttpContext.Current.User.Identity.Name != null ?
//TO DO change to get profileID from Appfabric or the database and log user infor
HttpContext.Current.User.Identity.Name.ToString() :
Guid.NewGuid().ToString();
}
#endregion
}
答案 0 :(得分:1)
当我读到你的问题时,你会问如何使用相同的DI容器在ASP.NET MVC和SignalR中进行适当的依赖注入(因此只需要在一个地方声明绑定)。如果理解正确,我曾写过一篇关于此事的博文:http://lcdev.dk/2012/02/14/using-signalr-ninject-with-asp-net-mvc3-and-the-ninject-mvc3-nuget-package/
在博客文章中,我假设您使用的是ASP.NET MVC3以及Ninject.MVC3和SignalR.Ninject Nuget软件包。
但是,如果不是这种情况,我会对您的代码发表评论。对我而言,用于进行绑定的内核(在RegisterServices
中)似乎不是您实际向SignalR注册的内核。如果是这种情况,那么当然SignalR将不知道您的预期绑定,并且可能因使用未实例化的对象引用而引发异常 - &gt;然后可以解释为什么你不能再连接到你的SignalR集线器了。
答案 1 :(得分:0)
http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/
这是gloabal.asax中的工作代码我还删除了NinJect mvc3添加到应用程序启动文件夹的引导程序,因为它在上面的帖子中是如何工作的
public class MvcApplication : NinjectHttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.IgnoreRoute("{*allaxd}", new { allaxd = @".*\.axd(/.*)?" }); //added for mango chat
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
public override void Init()
{
this.AuthenticateRequest += new EventHandler(MvcApplication_AuthenticateRequest);
this.PostAuthenticateRequest += new EventHandler(MvcApplication_PostAuthenticateRequest);
base.Init();
}
#region "Ninject stuff for dependancy Injection
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
protected override IKernel CreateKernel()
{
var kernel = new StandardKernel();
// kernel.Load(Assembly.GetExecutingAssembly());
RegisterServices(kernel);
return kernel;
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<UserIdClientIdFactory>()
.To<UserIdClientIdFactory>()
.InRequestScope();
SignalR.Hosting.AspNet.AspNetHost.DependencyResolver.Register(typeof(IConnectionIdFactory), () => new UserIdClientIdFactory());
}
#endregion
protected override void OnApplicationStarted()
{
base.OnApplicationStarted();
//for project awesome
ModelMetadataProviders.Current = new AwesomeModelMetadataProvider();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}