Signalr无法与Autofac和WebAPI + MVC + OWIN一起使用

问题描述 投票:0回答:1

[我们正在尝试使用Autofac进行依赖注入来启动Signalr Hub,但尚无结果。MVC和Web Apis与DI配合正常。

这里是配置文件。

Startup.cs

[assembly: OwinStartupAttribute(typeof(PNAME.WebUI.Startup))]
namespace PNAME.WebUI
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var config = new HttpConfiguration();

            // Register Routes
            WebApiConfig.Register(config);

            // Register dependencies
            //ConfigureDependencies(app, config);
            var container = DependencyConfiguration.Configure(app);
            SignalRConfiguration.Configure(app, container);
            MvcConfiguration.Configure(app, container);
            WebApiConfiguration.Configure(app, container, config);
            // Configure Authentication middleware
            ConfigureAuth(app, config);

            // configure Web API 
            app.UseWebApi(config);
        }
    }
}

DependencyConfiguration.cs

  public static class DependencyConfiguration
    {
        public static IContainer Configure(IAppBuilder app)
        {
            var builder = new ContainerBuilder();


            //Register any other components required by your code....
            builder.RegisterType<MainContext>().As<IApplicationDbContext>();

            builder.RegisterType<OneSignalNotificationService>().As<IPushNotificationService>();

            builder.RegisterType<LogService>().As<ILogger>();

            //Register SignalR Hub
            builder.RegisterHubs(Assembly.GetExecutingAssembly());
            //builder.RegisterType<ChatHub>().ExternallyOwned();

            //Register MVC Controllers
            builder.RegisterControllers(Assembly.GetExecutingAssembly());

            //Register WebApi Controllers
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            builder.RegisterAssemblyModules(Assembly.GetExecutingAssembly());

            var container = builder.Build();
            app.UseAutofacMiddleware(container);
            return container;
        }
    }

WebApiConfiguration.cs

public class WebApiConfiguration
    {
        public static void Configure(IAppBuilder app, IContainer container, HttpConfiguration config)
        {
            // Set the dependency resolver for Web API.
            var webApiResolver = new AutofacWebApiDependencyResolver(container);
            config.DependencyResolver = webApiResolver;

            //Configure Action Injection
            //config.InjectInterfacesIntoActions();

            app.UseAutofacWebApi(config);
        }
    }

SignalRConfiguration.cs

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

            //app.MapSignalR("/signalr", config);

            app.Map("/signalr", map =>
            {
                map.UseAutofacMiddleware(container);

                var hubConfiguration = new HubConfiguration
                {
                    Resolver = new AutofacDependencyResolver(container),
                    EnableDetailedErrors = true
                };

                map.RunSignalR(hubConfiguration);
            });
        }
    }

MvcConfiguration.cs

public class MvcConfiguration
    {
        public static void Configure(IAppBuilder app, IContainer container)
        {
            var mvcResolver = new Autofac.Integration.Mvc.AutofacDependencyResolver(container);
            DependencyResolver.SetResolver(mvcResolver);

            app.UseAutofacMvc();
        }
    }

关于ChatHub,它的以类库的形式位于[另一个项目中,作为类库

ChatHub.cs

namespace

即所有配置。

正如我在MVC和API中所说的,控制器与DI唯一的信号发送器未从客户端连接,并因以下错误而失败。

namespace PNAME.BotServices.NotificationCenter.SignalR
{
    [HubName("ChatHub")]
    public class ChatHub : Hub
    {
        private ILogger _logger;
        //private IChatMessageServices _chatMessageServices;
        private readonly ILifetimeScope _hubLifetimeScope;

        public ChatHub(ILifetimeScope lifetimeScope)
        {
            // Create a lifetime scope for the hub.
            _hubLifetimeScope = lifetimeScope.BeginLifetimeScope("AutofacWebRequest");

            // Resolve dependencies from the hub lifetime scope.
            _logger = _hubLifetimeScope.Resolve<ILogger>();
           // _chatMessageServices = _hubLifetimeScope.Resolve<IChatMessageServices>();

        }

        public bool SendMessage(INotificationMessage notificationMessage)
        {
           //Some code here
        }
        public override Task OnConnected()
        {
            //Some code here
        }
        public override Task OnReconnected()
        {
             //Some code here
        }
        public async Task UpdateStatus(int messageId, int ServerId, StatusType status)
        {
             //Some code here
        }

        protected override void Dispose(bool disposing)
        {
            // Dispose the hub lifetime scope when the hub is disposed.
            if (disposing && _hubLifetimeScope != null)
            {
                _hubLifetimeScope.Dispose();
            }

            base.Dispose(disposing);
        }

    }
}

不知道我在此配置中错过了什么。

asp.net-web-api model-view-controller signalr owin autofac
1个回答
0
投票

问题出在将集线器名称更改为“ MyChatHub”之后,集线器名称出现了问题,在依赖项注入中一切正常]]

© www.soinside.com 2019 - 2024. All rights reserved.