受欢迎的博客标签

ASP.NET Core SignalR(5):ASP.NET Core 5.x Web API+SignalR(微信小程序实时聊天集线)stock

Published

SignalR: UseSignalR and UseConnections methods removed In ASP.NET Core 5.0

In ASP.NET Core 3.0, SignalR adopted endpoint routing. As part of that change, the UseSignalR, UseConnections, and some related methods were marked as obsolete.

In ASP.NET Core 5.0, those obsolete methods were removed. For the full list of methods, see Affected APIs.

see:https://docs.microsoft.com/en-us/dotnet/core/compatibility/aspnet-core/5.0/signalr-usesignalr-useconnections-removed

see Affected APIs:https://docs.microsoft.com/en-us/dotnet/core/compatibility/aspnet-core/5.0/signalr-usesignalr-useconnections-removed#affected-apis

 

https://github.com/dotnet/aspnetcore/tree/main/src/SignalR/samples/SignalRSamples

Prerequisites

server configuration 

OS: Windows server R2 2012

nginx:1.14.0

Application :Dot net

Framework : Asp . net core 5.x

.Net Sdks:NET Core SDK 5.x or later

 

Table of content

Create a web API project.

Create a SignalR hub

Configure the project to use SignalR.

Add code that sends messages from any client to all connected clients

 

Step 1:Create a web API project.

Step 2:Configure the project to use SignalR

nuget:Mrcrosoft.AspNetCore.SignalR

Install-Package Microsoft.AspNetCore.SignalR
	<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />

Add SignalR  for Web API

  public void ConfigureServices(IServiceCollection services)
        {
            //old
           // services.AddControllers();
            services.AddControllers()
             .AddNewtonsoftJson();

            services.AddCors(options =>
            {
                options.AddPolicy("any",
                    policy => policy.AllowAnyOrigin()
                                    .AllowAnyHeader()
                                    .AllowAnyMethod()
                                    .WithOrigins("http://www.iaspnetcore.com", "https://iaspnetcore.com")
                                    .AllowCredentials());
            });

           

            //Add SignalR  for Web API
            services.AddSignalR()
            .AddMessagePackProtocol();
        }

 

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseAuthorization();

            //跨域支持
            app.UseCors("any");

            

            app.UseEndpoints(endpoints =>
            {

                endpoints.MapControllers();

                //微信小程序实时聊天集线器
                endpoints.MapHub<ChatHub>("/chat");

                //微信小程序行情回放集线器
                endpoints.MapHub<StockTickHub>("/stocktick");

                //微信小程序行情回放集线器,送分钟线和每笔成交明细供显示
                endpoints.MapHub<StockReportTickReplayHub>("/StockReportTickReplay");
               

               // endpoints.MapConnectionHandler<MessagesConnectionHandler>("/chat");
            });
        }