cannot load SignalR hub script in Autofac

632 Views Asked by At

I'm trying to use SignalR with Autofac dependency in my application, i have successfully registered dependencies but on run time it is causing error that SignalR dyanmic generated file hubs is missing and without that i can't do anything. I've searched internet but couldn't find any solution. Please help.

Config Class:

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(IocConfig), "RegisterDependencies")]

namespace Rental.Bootstrapper
{
    public class IocConfig
    {
        public static void RegisterDependencies()
        {
            var builder = new ContainerBuilder();
            const string nameOrConnectionString = "name=DefaultConnection";
            builder.RegisterControllers(typeof(MvcApplication).Assembly);
            builder.RegisterApiControllers(typeof(WebApiConfig).Assembly);
            builder.RegisterHubs(typeof(RentalHub).Assembly);
            //builder.RegisterInstance(typeof(Startup));
            builder.RegisterModule<AutofacWebTypesModule>();
            builder.RegisterGeneric(typeof(EntityRepository<>)).As(typeof(IRepository<>)).InstancePerHttpRequest();
            builder.RegisterGeneric(typeof(Service<>)).As(typeof(IService<>)).InstancePerHttpRequest();
            builder.RegisterType(typeof(UnitOfWork)).As(typeof(IUnitOfWork)).InstancePerHttpRequest();
            builder.Register<IEntitiesContext>(b =>
            {
                var logger = b.Resolve<ILogger>();
                var context = new RentalContext(nameOrConnectionString, logger);
                return context;
            }).InstancePerHttpRequest();
            builder.Register(b => NLogLogger.Instance).SingleInstance();
            builder.RegisterModule(new IdentityModule());

            var container = builder.Build();

            DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(container));
            GlobalConfiguration.Configuration.DependencyResolver =
                 new AutofacWebApiDependencyResolver(container);

            var signalRDependencyResolver = new Autofac.Integration.SignalR.AutofacDependencyResolver(container);
            GlobalHost.DependencyResolver = signalRDependencyResolver;                                  
        }
    }
}
1

There are 1 best solutions below

6
On

I suspect this is not Autofac issue at all. I also assume that you use MVC4, which is using Owin by default. Owin has a startup class, where all middleware and extensions must be registered. Normally, this is a Startup.cs class that at its minimum, required for SignalR, contains this:

using Microsoft.AspNet.SignalR;
using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(Startup))]
namespace MyNamepace
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR(new HubConfiguration
            {
                EnableDetailedErrors = true
            });
        }
    }
}

If you look at the MapSignalR method, you will find that it registers the"/signalr" route and this is exactly what you are missing, if I understand your question correctly:

/// <summary>
/// Maps SignalR hubs to the app builder pipeline at "/signalr".
/// 
/// </summary>
/// <param name="builder">The app builder</param><param name="configuration">The <see cref="T:Microsoft.AspNet.SignalR.HubConfiguration"/> to use</param>
public static IAppBuilder MapSignalR(this IAppBuilder builder, HubConfiguration configuration)
{
  return OwinExtensions.MapSignalR(builder, "/signalr", configuration);
}

Autofac SignalR documentation has a section about Owin startup, I strongly suggest you look there. It has a complete code for the startup.cs file that includes Autofac container initialization, all registrations and Owin-related code for SignalR.