SignalR issues with SignalR.Ninject and overiding the IConnectionIdFactory

StackOverflow https://stackoverflow.com/questions/9524482

  •  15-11-2019
  •  | 
  •  

Pregunta

I have all the Nuget Bits for SignalR , I am trying to use my own clientIDs as well as the dependency Injection container that comes with SignalR for all my other repositories and such. Now the strange thing is this jQuery to connect to the hub fails on:

 debugger;
        // Proxy created on the fly
        var chat = $.connection.chat;

Basically, the chat object becomes undefined as if SignalR cannot be resolved. This started happening once I tried to overide the default resolver for SignalR with the code below.

What am I missing here?

Another issue I am having is I am not sure if my UserClientIDfactory which implements IConnectionIdFactory is working either.

Here is the MVC3 code in my Global.asax

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);
    }

Finally, here is the code for my custom 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
} 
¿Fue útil?

Solución 2

ok thanks for the your post man, made me do some more digging , I read the rest of the post you linked about how to use Ninject with MVC3 which lead me to realize that I had ninject but not the Nuget Bits for Ninject Mvc3 , I added that and alos modifed my global.asax using the following post

http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/

here is the working code in gloabal.asax I also removed the bootstrapper that NinJect mvc3 added to the application start folder since that is how it works in the above post

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); 

      }













    }

Otros consejos

As I read your question you ask how to do proper dependency injection in ASP.NET MVC and SignalR using the same DI container (and hence only need to declare bindings in one place). If this is correct understood, I once wrote a blog post regarding this: http://lcdev.dk/2012/02/14/using-signalr-ninject-with-asp-net-mvc3-and-the-ninject-mvc3-nuget-package/

In the blog post I assume that you are using ASP.NET MVC3 as well as the Ninject.MVC3 and the SignalR.Ninject Nuget packages.

However, if this is not the case I do have a comment to your code. To me it seems like that the kernel used to make your bindings (in RegisterServices) is not the kernel you actually register with SignalR. And if this is the case, then of course SignalR won't know about your intended bindings and might throw an exception as result of your use of an un-instantiated object reference -> which then might explain why you no longer can connect to your SignalR hub.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top