Autofacを使用してSignalRIHubContextをサービスレイヤーに挿入します

Nov 21 2020

.NETCoreではなくFramework4.72を実行しているアプリで、SignalRIHubContextをWebAPI2.xサービスに挿入しようとしています。私のソリューションは、Web、サービス、データの3つのプロジェクトに分かれています。SignalRハブはWebレイヤーにあります。サービスレイヤーで実行されるバックグラウンドコードがあり、完了したら、ハブを介してメッセージを送信するために必要です。このバックグラウンドタスクは、コントローラーによって開始されません。

私のGlobal.asaxはかなり標準的です:

 protected void Application_Start()
{
    GlobalConfiguration.Configure(WebApiConfig.Register);

    // Set JSON serializer to use camelCase
    var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
    json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

    DIConfig.Setup();

    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

    var logConfigFilePath = Server.MapPath("~/log4net.config");
    log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(logConfigFilePath));
}

私のDIConfigには次のものが含まれています。

internal static void Setup()
{
    var config = System.Web.Http.GlobalConfiguration.Configuration;
    var builder = new ContainerBuilder();

    builder.Register(c => new ShopAPDbContext()).AsImplementedInterfaces().InstancePerBackgroundJob().InstancePerLifetimeScope();
    builder.RegisterType<ShopAPRepository>().As<IShopAPRepository>().InstancePerBackgroundJob().InstancePerLifetimeScope();
    builder.RegisterType<ShopAPService>().As<IShopAPService>().InstancePerBackgroundJob().InstancePerLifetimeScope();

    builder.AddAutoMapper(typeof(InvoiceMappingProfile).Assembly);
    builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
    var container = builder.Build();

    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

    Hangfire.GlobalConfiguration.Configuration.UseAutofacActivator(container);
}

そして私のStartup.cs:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var container = DependencyConfiguration.Configure(app);
        SignalRConfiguration.Configure(app, container);
        HangFireDashboardConfig.Configure(app);
    }
}

public static class DependencyConfiguration
{
    public static IContainer Configure(IAppBuilder app)
    {
        var builder = new ContainerBuilder();
        builder.RegisterHubs(typeof(SignalRConfiguration).Assembly);
        var container = builder.Build();
        app.UseAutofacMiddleware(container);
        return container;
    }
}

public static class SignalRConfiguration
{
    public static void Configure(IAppBuilder app, IContainer container)
    {
        HubConfiguration config = new HubConfiguration();
        config.Resolver = new AutofacDependencyResolver(container);

        app.Map("/messages", map =>
        {
            map.UseCors(CorsOptions.AllowAll);
            var hubConfiguration = new HubConfiguration
            {
                EnableDetailedErrors = true,
                EnableJavaScriptProxies = false
            };
            map.RunSignalR(hubConfiguration);
        });
    }
}

私のサービスレイヤーのコンストラクターは次のようになります。

public ShopAPService()
{
    _shopAPRepository = new ShopAPRepository();
    _mapper = new Mapper((IConfigurationProvider)typeof(InvoiceMappingProfile).Assembly);
    _hubContext = null;  // what here?
}

public ShopAPService(IShopAPRepository shopAPRepository, IMapper mapper, IHubContext hubContext)
{
    _shopAPRepository = shopAPRepository;
    _mapper = mapper;
    _hubContext = hubContext;
}

IHubContextのインスタンスをサービスに渡す必要があることはわかっていますが、これまでのところ成功していません。私が知っているように、最初のコンストラクターは、コントローラー以外からサービスが呼び出されたときに使用されるものですか?


最初の改訂

わかりました。すべてを1つのコンテナに入れる必要があることを理解しています。フィードバックに基づいて、それらのリンクを見て、コンテナーを作成して渡します。これが私の改訂されたスタートアップです:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        //var config = System.Web.Http.GlobalConfiguration.Configuration;
        var container = GetDependencyContainer();

        RegisterWebApi(app, container);
        RegisterSignalR(app, container);

        GlobalHost.DependencyResolver = new AutofacDependencyResolver(container);
        HubConfiguration config = new HubConfiguration();
        config.Resolver = new AutofacDependencyResolver(container);

        //config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        Hangfire.GlobalConfiguration.Configuration.UseAutofacActivator(container);

        HangFireDashboardConfig.Configure(app);
    }

    private IContainer GetDependencyContainer()
    {
        return AutofacConfig.RegisterModules();
    }

    private void RegisterWebApi(IAppBuilder app, IContainer container)
    {
        var configuration = new HttpConfiguration
        {
            DependencyResolver = new AutofacWebApiDependencyResolver(container)
        };

        WebApiConfig.Register(configuration);

        app.UseAutofacMiddleware(container);
        app.UseAutofacWebApi(configuration);
        app.UseWebApi(configuration);
    }

    private void RegisterSignalR(IAppBuilder app, IContainer container)
    {
        var configuration = new HubConfiguration
        {
            Resolver = new AutofacDependencyResolver(container)
        };

        app.MapSignalR(configuration);
    }
}

そして、私のAutofacConfigはコンテナを構築し、登録のほとんどを行います。

internal class AutofacConfig
{
    public static IContainer RegisterModules()
    {
        var builder = new ContainerBuilder();

        builder.Register(c => new ShopAPDbContext()).AsImplementedInterfaces().InstancePerBackgroundJob().InstancePerLifetimeScope();
        builder.RegisterType<ShopAPRepository>().As<IShopAPRepository>().InstancePerBackgroundJob().InstancePerLifetimeScope();
        builder.RegisterType<ShopAPService>().As<IShopAPService>().InstancePerBackgroundJob().InstancePerLifetimeScope();

        builder.RegisterAutoMapper(typeof(InvoiceMappingProfile).Assembly);
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

        // Register Autofac resolver into container to be set into HubConfiguration later
        builder.RegisterType<AutofacDependencyResolver>().As<IDependencyResolver>().SingleInstance();

        // Register ConnectionManager as IConnectionManager so that you can get hub context via IConnectionManager injected to your service
        builder.RegisterType<ConnectionManager>().As<IConnectionManager>().SingleInstance();

        builder.RegisterHubs(Assembly.GetExecutingAssembly());

        var container = builder.Build();

        return container;
    }
}

しかし、それでもサービス内のハブへの参照を取得できません(APIとは別のプロジェクトにありますが、同じソリューションにあります。

私のサービスメソッドはHangFireから呼び出され、ハブへの参照がありません。パラメータなしのコンストラクタでどのように参照できますか?

public partial class ShopAPService : IShopAPService
{
    private static readonly ILog _log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    readonly IShopAPRepository _shopAPRepository;
    readonly IMapper _mapper;
    private IHubContext _hubContext;

    public ShopAPService()
    {
        _shopAPRepository = new ShopAPRepository();
        _mapper = new Mapper((IConfigurationProvider)typeof(InvoiceMappingProfile).Assembly);
        _hubContext = connectionManager.GetHubContext<MessageHub>();
    }

    public ShopAPService(IShopAPRepository shopAPRepository, IMapper mapper, IHubContext hubContext)
    {
        _shopAPRepository = shopAPRepository;
        _mapper = mapper;
        _hubContext = hubContext;
    }
}

回答

2 AlexanderI. Dec 01 2020 at 21:49

IHubContext直接解決することはできません。ただし、このインターフェイスの一般的な実装は解決できます。詳細はこことここで説明されています。

OWIN(Stratup.cs)の非常に単純な実装を作成するだけです。Autofac.SignalRナゲットパッケージをインストールし、登録にメソッドRegisterHubsを使用し、マッピングにメソッドMapSignalRを使用しました。これは標準的なアプローチであり、型付きハブの実装を解決するのは大変です。

ただし、コンテキストをより正確に解決したい場合は、AutofacDependencyResolverConnectionManagerの2つの登録を追加する必要があります(詳細については、こちらをご覧ください)。

完全なサンプルを確認してください:

using Autofac;
using Autofac.Integration.SignalR;
using Autofac.Integration.WebApi;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin;
using Owin;
using System.Reflection;
using System.Web.Http;

[assembly: OwinStartup(typeof(Startup))]
namespace Sample
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var container = GetDependencyContainer();

            RegisterWebApi(app, container);
            RegisterSignalR(app, container);
        }

        private IContainer GetDependencyContainer()
        {
            var builder = new ContainerBuilder();

            AutofacConfig.RegisterModules(builder);
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            builder.RegisterHubs(Assembly.GetExecutingAssembly());

            // Register Autofac resolver into container to be set into HubConfiguration later
            builder.RegisterType<AutofacDependencyResolver>().As<IDependencyResolver>().SingleInstance();
            // Register ConnectionManager as IConnectionManager so that you can get hub context via IConnectionManager injected to your service
            builder.RegisterType<ConnectionManager>().As<IConnectionManager>().SingleInstance();

            var container = builder.Build();

            return container;
        }

        private void RegisterWebApi(IAppBuilder app, IContainer container)
        {
            var configuration = new HttpConfiguration
            {
                DependencyResolver = new AutofacWebApiDependencyResolver(container)
            };

            WebApiConfig.Register(configuration);

            app.UseAutofacMiddleware(container);
            app.UseAutofacWebApi(configuration);
            app.UseWebApi(configuration);
        }

        private void RegisterSignalR(IAppBuilder app, IContainer container)
        {
            var configuration = new HubConfiguration
            {
                Resolver = new AutofacDependencyResolver(container)
            };

            app.MapSignalR(configuration);
        }
    }
}

私のハブは標準的でシンプルです:

public class MaintenanceHub : Hub
{
    public MaintenanceHub(IMaintenanceLogProvider maintenanceLogProvider)
    {
        maintenanceLogProvider.TaskProgressStatusEvent += (s, e) => GetTaskLogStatus(e);
    }

    public void GetTaskLogStatus(LongTaskProgressStatus taskProgressStatus)
    {
        Clients.All.getTaskLogStatus(taskProgressStatus);
    }
}

登録後、ハブインジェクトコンテキストを使用できます。それのようなもの(利用可能な2つのオプション、あなたが望むものは1つだけ使用できます):

public AccountController(IConnectionManager connectionManager, MaintenanceHub maintenanceHub)
{
    var context = connectionManager.GetHubContext<MaintenanceHub>();
    var hub = maintenanceHub;
}