另一个项目中的Asp Net核心SignalR集线器

我使用Asp Net Core创建了一个应用程序,我使用SignalR将数据推送到客户端应用程序。

下面给出的是启动类的代码 -

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Swashbuckle.AspNetCore.Swagger;

namespace SignalRPushMessageApplication
{
    /// <summary>
    /// Startp class
    /// </summary>
    public class Startup
    {
        /// <summary>
        /// Startup method
        /// </summary>
        /// <param name="env"></param>
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", false, true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        /// <summary>
        /// Configuration
        /// </summary>
        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        /// <summary>
        /// ConfigureServices
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddMvc();
            // Add SignalR
            services.AddSignalR(options =>
            {
                options.Hubs.EnableDetailedErrors = true;
            });
        }

        /// <summary>
        /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        /// <param name="loggerFactory"></param>
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseMvc();
            app.UseSignalR();
        }
    }
}

现在我在同一个项目中使用了SignalR Hub类,它工作正常。 当我在不同于SignalR hub的项目中移动SignalR集线器类时,但根据项目要求,我希望SignalR Hub处于单独的项目中。 请建议如何完成。

下面给出的是SignalR hub代码 -

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;

namespace SignalRPushMessageService.Classes
{

    /// <summary>
    /// This is SignalR hub class where all the methods of hub class will be defined.
    /// </summary>
    public class SignalRHub : Hub
    {

        /// <summary>
        /// This method will send response back to SignalR client using its unique Id
        /// </summary>
        /// <param name="uniqueId">Unique id of the SignalR client</param>
        /// <param name="notificationMessage">message to be sent to SignalR client</param>
        /// <returns></returns>
        public Task SendResponse(string uniqueId, string message)
        {
            Clients.Group(uniqueId).SendResponse(message);
        }
    }
}

请记住,此应用程序正在使用Asp Net Core构建,因此请避免使用较旧的.net框架提供答案。 SignalR的实现在旧的框架版本中是不同的(使用Owin Communication监听器)。

链接地址: http://www.djcxy.com/p/96185.html

上一篇: Asp Net Core SignalR hub in another project

下一篇: SignalR hub and Identity.Claims