Last active
September 14, 2015 09:51
-
-
Save tillig/12d6d9acfcde8bd55609 to your computer and use it in GitHub Desktop.
Intercepting SignalR Hubs with Autofac
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Linq; | |
using Castle.DynamicProxy; | |
namespace SignalInterceptor | |
{ | |
public class CallLogger : IInterceptor | |
{ | |
public void Intercept(IInvocation invocation) | |
{ | |
Trace.WriteLine( | |
String.Format("Calling method {0} with parameters {1}... ", | |
invocation.Method.Name, | |
String.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray()))); | |
invocation.Proceed(); | |
Trace.WriteLine(String.Format("Done: result was {0}.", invocation.ReturnValue)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNet.SignalR; | |
namespace SignalInterceptor.Hubs | |
{ | |
public class ChatHub : Hub | |
{ | |
// IMPORTANT: The method must be virtual or it won't get intercepted! | |
public virtual void Send(string name, string message) | |
{ | |
Clients.All.addNewMessageToPage(name, message); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<packages> | |
<package id="Antlr" version="3.5.0.2" targetFramework="net45" /> | |
<!-- Don't forget the SignalR AND the DynamicProxy2 integration packages for Autofac --> | |
<package id="Autofac" version="3.5.2" targetFramework="net45" /> | |
<package id="Autofac.Extras.DynamicProxy2" version="3.0.5" targetFramework="net45" /> | |
<package id="Autofac.Mvc5" version="3.3.4" targetFramework="net45" /> | |
<package id="Autofac.Mvc5.Owin" version="3.1.0" targetFramework="net45" /> | |
<package id="Autofac.Owin" version="3.1.0" targetFramework="net45" /> | |
<package id="Autofac.SignalR2" version="3.1.0" targetFramework="net45" /> | |
<package id="bootstrap" version="3.3.4" targetFramework="net45" /> | |
<package id="Castle.Core" version="3.3.3" targetFramework="net45" /> | |
<package id="jQuery" version="2.1.4" targetFramework="net45" /> | |
<package id="jQuery.Validation" version="1.13.1" targetFramework="net45" /> | |
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net45" /> | |
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" /> | |
<package id="Microsoft.AspNet.SignalR" version="2.2.0" targetFramework="net45" /> | |
<package id="Microsoft.AspNet.SignalR.Core" version="2.2.0" targetFramework="net45" /> | |
<package id="Microsoft.AspNet.SignalR.JS" version="2.2.0" targetFramework="net45" /> | |
<package id="Microsoft.AspNet.SignalR.SystemWeb" version="2.2.0" targetFramework="net45" /> | |
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net45" /> | |
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net45" /> | |
<package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net45" /> | |
<package id="Microsoft.Owin" version="3.0.1" targetFramework="net45" /> | |
<package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net45" /> | |
<package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net45" /> | |
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" /> | |
<package id="Modernizr" version="2.8.3" targetFramework="net45" /> | |
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" /> | |
<package id="Owin" version="1.0" targetFramework="net45" /> | |
<package id="Respond" version="1.4.2" targetFramework="net45" /> | |
<package id="WebGrease" version="1.6.0" targetFramework="net45" /> | |
</packages> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
using System.Web.Optimization; | |
using System.Web.Routing; | |
using Autofac; | |
using Autofac.Extras.DynamicProxy2; | |
using Autofac.Integration.Mvc; | |
using Autofac.Integration.SignalR; | |
using Microsoft.AspNet.SignalR; | |
using Microsoft.Owin; | |
using Owin; | |
[assembly: OwinStartup(typeof(SignalInterceptor.Startup))] | |
namespace SignalInterceptor | |
{ | |
public class Startup | |
{ | |
public void Configuration(IAppBuilder app) | |
{ | |
// Autofac registrations | |
var builder = new ContainerBuilder(); | |
builder.RegisterType<CallLogger>(); | |
builder.RegisterHubs(typeof(Startup).Assembly).EnableClassInterceptors().InterceptedBy(typeof(CallLogger)); | |
builder.RegisterControllers(typeof(Startup).Assembly); | |
var container = builder.Build(); | |
// MVC setup | |
AreaRegistration.RegisterAllAreas(); | |
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); | |
RouteConfig.RegisterRoutes(RouteTable.Routes); | |
BundleConfig.RegisterBundles(BundleTable.Bundles); | |
DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(container)); | |
// SignalR setup | |
var config = new HubConfiguration(); | |
config.Resolver = new Autofac.Integration.SignalR.AutofacDependencyResolver(container); | |
// OWIN middleware | |
app.UseAutofacMiddleware(container); | |
app.UseAutofacMvc(); | |
app.MapSignalR("/signalr", config); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment