I have an existing MVC 4 application that uses Autofac and I have other assemblies that have autofac Modules in. Without SignalR/autofac integration, my application works perfectly as far as IOC is concerned. I have added in SignalR hubs which I am trying to inject dependencies into via the constructor. I have followed the documentation and implemented the code example from.
http://autofac.readthedocs.org/en/latest/integration/signalr.html
Which results in this following class:
public class SignalRConfig
{
public void Configuration(IAppBuilder app)
{
var builder = new ContainerBuilder();
var config = new HubConfiguration();
Assembly thisAssembly = typeof(SignalRConfig).Assembly;
builder.RegisterHubs(thisAssembly);
var container = builder.Build();
config.Resolver = new AutofacDependencyResolver(container);
app.UseAutofacMiddleware(container);
app.MapSignalR("/signalr", config);
}
}
This is hooked up with this line which is added to my AssemblyInfo.cs
[assembly: OwinStartup(typeof(SignalRConfig))]
But its not working at runtime as my hub does not have a parameterless constructor and it cannot resolve the dependency. My assumption is that I am creating a new ContainerBuilder (as per docs) and that this instance has nothing registered on it. I actually want the reference to the ContainerBuilder that is passed around my modules that knows about all my registered types. I just dont know how to do it. The existing IOC code runs from the application_start on the global.asax and the SignalRConfig runs at the same time. I dont want to hold the ContainerBuilder in a singleton as it feels dirty but I cant find any other solution.
Here is a section of my nuget package config showing the version numbers etc
<package id="Autofac" version="3.5.2" targetFramework="net451" />
<package id="Autofac.Mvc4" version="3.1.0" targetFramework="net40" />
<package id="Autofac.Owin" version="3.1.0" targetFramework="net451" />
<package id="Autofac.SignalR" version="3.0.1" targetFramework="net451" />
<package id="Autofac.WebApi" version="3.1.0" targetFramework="net40" />
<package id="Microsoft.AspNet.Mvc" version="4.0.30506.0" targetFramework="net40" />
<package id="Microsoft.AspNet.Razor" version="2.0.30506.0" targetFramework="net40" />
<package id="Microsoft.AspNet.SignalR" version="2.2.0" targetFramework="net451" />
<package id="Microsoft.AspNet.SignalR.Core" version="2.2.0" targetFramework="net451" />
<package id="Microsoft.AspNet.SignalR.JS" version="2.2.0" targetFramework="net451" />
<package id="Microsoft.AspNet.SignalR.SystemWeb" version="2.2.0" targetFramework="net451" />
<package id="Microsoft.AspNet.WebApi" version="4.0.20710.0" targetFramework="net40" />
<package id="Microsoft.AspNet.WebApi.Client" version="4.0.30506.0" targetFramework="net40" />
<package id="Microsoft.AspNet.WebApi.Core" version="4.0.30506.0" targetFramework="net40" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="4.0.30506.0" targetFramework="net40" />
<package id="Microsoft.AspNet.WebPages" version="2.0.30506.0" targetFramework="net40" />
<package id="Microsoft.Owin" version="3.0.0" targetFramework="net451" />
<package id="Microsoft.Owin.Host.SystemWeb" version="2.1.0" targetFramework="net451" />
<package id="Microsoft.Owin.Security" version="3.0.0" targetFramework="net451" />
<package id="Owin" version="1.0" targetFramework="net451" />