I have an ASP.NET MVC5 application with SignalR and Ninject for dependency injection. I have a custom authorize attribute for both the web request and the signalr hub request.

I made the following class for the web:

public class MyWebAuthorizeAttribute : AuthorizeAttribute // (System.Web.Mvc.AuthorizeAttribute)
{
    [Inject]
    public IMyService MyService { get; set; } 

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        var id = int.Parse((httpContext.Handler as MvcHandler).RequestContext.RouteData.Values["id"].ToString());
        if (!MyService.Exists(id)) 
            return false;
        return base.AuthorizeCore(httpContext);
    }
}

And the following for SignalR:

public class MySignalRAuthorizeAttribute : AuthorizeAttribute // (Microsoft.AspNet.SignalR.AuthorizeAttribute)
{
    [Inject]
    public IMyService MyService { get; set; } 

    public override bool AuthorizeHubConnection(HubDescriptor hubDescriptor, IRequest request)
    {
        var id = int.Parse(request.QueryString["id"]);
        if (!MyService.Exists(id))
            return false;
        return base.AuthorizeHubConnection(hubDescriptor, request);
    }
}

Ninject bindings in the Startup class:

var kernel = new StandardKernel();
var resolver = new NinjectSignalRDependencyResolver(kernel);
kernel.Bind<IMyService>().To<MyService>().InSingletonScope();
var config = new HubConfiguration();
config.Resolver = resolver;
app.MapSignalR(config);

The problem is that the service is not being injected in MySignalRAuthorizeAttribute, though in MyWebAuthorizeAttribute the service is properly being injected.

Any idea what I am missing to get this working?

Related posts

Recent Viewed