Use dependency injection in Signalr 2.0 on your own?

With SignalR 2.0 in a self-service application from these instructions , you have something like this:

class Startup
{
   public void Configuration(IAppBuilder app)
   {
       app.MapSignalR(new HubConfiguration { Resolver = ... });
   }
}
class Program
{
    static void Main(string[] args)
    {
        using (WebApp.Start("http://localhost:8080")) // constructs Startup instance internally
        {
            Console.WriteLine("Server running on {0}", url);
            Console.ReadLine();
        }
    }
}

You will notice that an instance of the launch class is created with some backstage magic. I can’t figure out how to fill in the dependencies on it. Is there a way to override the construction of the Startup class so that I can embed dependencies in it?

+4
source share
3 answers

Instead of replacing the IAppActivator, you can simply register the arguments of the Startup constructor with the Katana ServiceProvider.

IAppActivator , Startup. , WebApp.Start, ServiceProvider:

public class MyService : IMyService
{
    private readonly IMyOtherService _myOtherService;

    // Services will be recursively resolved by Katana ServiceProvider
    public MyService(IMyOtherService myOtherService)
    {
        _myOtherService = myOtherService;
    }

    // Implementation
}

public class Startup
{
   private readonly IMyService _myService;

   // Startup must have exactly one constructor.
   public Startup(IMyService myService)
   {
       _myService = myService
   }

   public void Configuration(IAppBuilder app)
   {
       app.MapSignalR(new HubConfiguration { Resolver = ... });
   }
}

using System;
using Microsoft.Owin.Hosting;
using Microsoft.Owin.Hosting.Services;
using Microsoft.Owin.Hosting.Starter;

public class Program
{
    static void Main(string[] args)
    {
        var url = "http://localhost:8080";

        var services = (ServiceProvider)ServicesFactory.Create();
        var options = new StartOptions(url);

        services.Add<IMyOtherService, MyOtherService>();
        services.Add<IMyService, MyService>();

        var starter = services.GetService<IHostingStarter>();

        using (starter.Start(options)) // constructs Startup instance internally
        {
            Console.WriteLine("Server running on {0}", url);
            Console.ReadLine();
        }
    }
}

WebApp.Start Program.Main, , IHostingStarter.Start, : http://katanaproject.codeplex.com/SourceControl/changeset/view/c726b87e90c05677a256ca1821bac481f402d6bd#src/Microsoft.Owin.Hosting/WebApp.cs

ServiceProvider.Add, : http://msdn.microsoft.com/en-us/library/microsoft.owin.hosting.services.serviceprovider(v=vs.111).aspx

, Katana IAppActivator StartOptions.Settings, .

, , Startup , .

+2
class Startup
{
   private readonly IDependencyResolver _resolver;

   public Startup(IDependencyResolver resolver)
   {
        _resolver = resolver;
   }

   public void Configuration(IAppBuilder app)
   {
       app.MapSignalR(new HubConfiguration { Resolver = _resolver; });
   }
}

class Program
{
    static void Main(string[] args)
    {
        Startup startup = new Statrtup(new MyResolver());
        using (WebApp.Start("http://localhost:8080", startup.Configuration))
        {
            Console.WriteLine("Server running on {0}", url);
            Console.ReadLine();
        }
    }
}
0

Source: https://habr.com/ru/post/1523943/


All Articles