使用Autofac将SignalR IHubContext注入到服务层

4
在运行Framework 4.72的应用程序中(不是.NET Core),我正在尝试将SignalR IHubContext注入到Web API 2.x服务中。 我将我的解决方案分为三个项目:web,service,data。 SignalR hub位于web层中。 我有在service层中运行的后台代码,当完成后,我需要通过hub发送消息。 这个后台任务不是由控制器启动的。
我的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的实例传递到服务中,但是到目前为止我还没有成功。据我所知,当服务从控制器以外的任何地方调用时,第一个构造函数就会被使用?


第一次修改

好的,我明白所有内容都应该放在一个容器中。根据反馈和查看那些链接,我创建了一个容器并将其传递。以下是我的修改后的启动代码:

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;
    }
}

但是,我仍然无法在我的服务中获取到 Hub 的引用(该服务位于与 API 不同的项目中,但在相同的解决方案中)。

solutioin screenshot

我的服务方法是从HangFire调用的,与Hub没有关联。如何在我的无参数构造函数中引用它?

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;
    }
}
1个回答

2
您不能直接解析。但是您可以解析此接口的通用实现。更多详细信息请参见这里这里
我创建了一个非常简单的实现(Stratup.cs)。我安装了Autofac.SignalR包并使用RegisterHubs方法进行注册,使用MapSignalR方法进行映射。这是标准方法,并且足以解析类型化的Hub实现。
但是,如果您希望更正确地解决上下文,则需要添加两个注册: AutofacDependencyResolver ConnectionManager (有关更多信息,请参见此处)。
请查看完整示例:
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);
        }
    }
}

我的 Hub 是标准且简单的:

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

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

注册后,您可以使用Hub注入上下文。类似于以下内容(提供2种选项,您可以根据需要仅使用其中一种):

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

enter image description here


AutofacConfig在哪里? - Connie DeCinko
@alexandar 我该如何将 hub 注入到另一个类中的服务中? - Connie DeCinko
@ConnieDeCinko 请查看 StartUp 类。其中包含 AutofacDependencyResolverConnectionManager 的注册。您可以将 IConnectionManager 注入到您的服务类中。您可以从 IConnectionManager 中提取 Hub 上下文,例如:var context = connectionManager.GetHubContext<MaintenanceHub>(); - Alexander I.
@ConnieDeCinko,我看到你已经添加了注册功能,这是正确的。但是你尝试注入HubContext是错误的。你需要在ShopAPService构造函数中注入IConnectionManager。另外,为什么你要使用无参构造函数?你需要为构造函数提供所有必要的参数,Autofac会自动为你注入接口。 - Alexander I.
@ConnieDeCinko 我理解你的困惑。不确定将中心移动到服务项目是否是一个好的架构思路。对我来说,当你的API和SignalR项目引用服务项目(就像你现在实现的那样)时,这是一个好的架构。我认为你可以尝试实现事件或委托方法。 - Alexander I.
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接