Get Your Web API Playing Nicely With SignalR on OWIN with Autofac
Niche, right? Yes. But this has caused me a day of headaches so posting here to save anyone else from the pain.
WebAPI
Say we already have Autofac set-up with your WebAPI. So we have something like this:
This is fine.
If we then follow the Autofac docs, we’ll end up with something pretty similar for SignalR:
and then the Hub configuration:
Run this and (if you just have a default WebAPI set-up) we’ll probably get a 404 for our SignalR hub. So we need to get WebAPI to ignore anything relevant to SignalR.
Ignore Routes
Before we map our main routes, we can tell the router to ignore anything under the SignalR path:
Here, we’re just telling the WebAPI router to ignore anything that starts with ‘signalr‘. Now our hub paths should return properly.
But what about the dependency config?
Path Matching Config
You might have noticed that we have two calls to app.UseAutofacMiddleware(container) – which one do we use?
Rather than configuring everything directly onto the app object, we can use path matching to only inject the required Autofac dependency settings for SignalR.
Using this syntax, we can set up our code with a scoped middleware call:
(this replaces app.MapSignalR from above)
Note that we’re setting the SignalR path explicitly as /signalr. We could change this if we wanted, but we’d need to also update our ignored routes above to match.
With that done, we can inject whatever we need from our container. The Autofac docs suggest injecting an ILifetimeScope into the Hub constructor so child objects can be disposed of properly (we do not want a memory leak) so it’s worth reading that if you haven’t already.