我如何才能将依赖项注入signalR核心的集线器构造函数中?

时间:2019-03-21 10:05:59

标签: c# asp.net-core signalr-hub asp.net-core-signalr

我正在asp.net core 2.2下的asp.net信号内核中工作,我想在集线器构造函数中注入依赖项,我该怎么做? 我使它通用,以便在构造函数上传递参数。

我的中心:

 public class IntegratedHUB : Hub<IIntegratedHubClient>
    {
        private readonly AuthorizeConnection _authorizeConnection = new AuthorizeConnection();
        private readonly ISignalRIdentity _signalRIdentity;
        private readonly IVideoService _videoService;
        public IntegratedHUB(ISignalRIdentity signalrIdentity, IVideoService videoService)
        {
            _signalRIdentity = signalrIdentity;
            _videoService = videoService;
        }
}

“我的创业”课程

 // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var _unitofWork = new UnitOfWork(new DbFactory());
            var _SignalrIdentity = new SignalRIdentity(_unitofWork, new UnitOfWork(new DbFactory()));
            var _videoService = new VideoService(_unitofWork, new UnitOfWork(new DbFactory()));
            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy",
                    builder => builder.SetIsOriginAllowed((host) => true)/*WithOrigins("https://localhost:44381")*/
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials());
            });
            services.AddTransient<IntegratedHUB, IntegratedHUB>(); // how  i change it?
            services.AddSignalR();
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseCors("CorsPolicy");
            app.UseSignalR(routes =>
            {
                routes.MapHub<IntegratedHUB>("/integratedHUB");
            });
            app.UseHttpsRedirection();
            app.UseMvc();
        }

我如何在集线器构造函数中注入依赖项?

0 个答案:

没有答案