Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SignalR integration #33

Closed
sergey-buturlakin opened this issue May 4, 2016 · 1 comment
Closed

SignalR integration #33

sergey-buturlakin opened this issue May 4, 2016 · 1 comment

Comments

@sergey-buturlakin
Copy link

Could you add an integration package for SignalR using the following source code?

file SimpleInjectorSignalRDependencyResolver.cs

using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.AspNet.SignalR;


namespace SimpleInjector.Integration.SignalR
{
    public class SimpleInjectorSignalRDependencyResolver : DefaultDependencyResolver
    {
        public SimpleInjectorSignalRDependencyResolver(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        public override object GetService(Type serviceType)
        {
            return _serviceProvider.GetService(serviceType) ?? base.GetService(serviceType);
        }

        public override IEnumerable<object> GetServices(Type serviceType)
        {
            var @this = (IEnumerable<object>) _serviceProvider.GetService(typeof (IEnumerable<>).MakeGenericType(serviceType));

            var @base = base.GetServices(serviceType);

            return @this == null ? @base : @base == null ? @this : @this.Concat(@base);
        }

        private readonly IServiceProvider _serviceProvider;
    }
}

file SimpleInjectorHubDispatcher.cs

using System;
using System.Threading.Tasks;

using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;


namespace SimpleInjector.Integration.SignalR
{
    public class SimpleInjectorHubDispatcher : HubDispatcher
    {
        public SimpleInjectorHubDispatcher(Container container, HubConfiguration configuration) : base(configuration)
        {
            _container = container;
        }

        protected override Task OnConnected(IRequest request, string connectionId)
        {
            return Invoke(() => base.OnConnected(request, connectionId));
        }

        protected override Task OnReceived(IRequest request, string connectionId, string data)
        {
            return Invoke(() => base.OnReceived(request, connectionId, data));
        }

        protected override Task OnDisconnected(IRequest request, string connectionId, bool stopCalled)
        {
            return Invoke(() => base.OnDisconnected(request, connectionId, stopCalled));
        }

        protected override Task OnReconnected(IRequest request, string connectionId)
        {
            return Invoke(() => base.OnReconnected(request, connectionId));
        }

        private async Task Invoke(Func<Task> method)
        {
            using (_container.BeginExecutionContextScope())
                await method();
        }

        private readonly Container _container;
    }
}

Registration sample:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var container = new Container();

        container.Options.DefaultScopedLifestyle = new ExecutionContextScopeLifestyle();

        container.Register<DbContext, MyDbContext>(Lifestyle.Scoped);
        container.Register<ISampleRepository, SampleRepository>(Lifestyle.Scoped);

        // if you want to use the same container in WebApi don't forget to add
        app.Use(async (context, next) => {
            using (container.BeginExecutionContextScope())
                await next();
        });

        // ... configure web api 

        var config = new HubConfiguration
        {
            Resolver = new SimpleInjectorSignalRDependencyResolver(container)
        }

        // ... configure the rest of SignalR

        // important to pass SimpleInjectorHubDispatcher
        app.MapSignalR<SimpleInjectorHubDispatcher>("/signalr", config);
    }
}

P.S. I tried to make a PR but experienced problems with the project solution in my dev environment.

@sergey-buturlakin
Copy link
Author

Sorry, wrong repo :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant