Asp Net Core SignalR hub in another project

I have created an application using Asp Net Core and I am using SignalR to push data to client applications.

Below given is the code of startup class-

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

Right now I have my SignalR Hub class in the same project and it is working fine. When I move SignalR hub class in a different project than SignalR hub stopped working, But as per the project requirement I want SignalR Hub in a separate project. Please suggest how it can be done.

Below given is SignalR hub code-

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

Please remember that this application is being build using Asp Net Core so avoid answers with older .net framework. SignalR implementation is different in older framework versions (using Owin Communication listener).

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

上一篇: 如果集线器在不同的项目SignalR中,如何连接集线器?

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