前言
之前大概画过一个关于webim项目的架构设计,架构肯定还有很多需要改进地方,最近这段时间就大概把这个进行了代码实现,分享、记录一下,也请各位大佬多多指点
代码实现
- 创建.Net Core Web应用项目
- 通过NuGet安装、引用Microsoft.AspNetCore.SignalR.Core、Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson
- 修改Startup.cs,添加SignalR
-
public void ConfigureServices(IServiceCollection services) { services.AddSignalR().AddNewtonsoftJsonProtocol(options => { //忽略循环引用 options.PayloadSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; //不使用驼峰样式的key options.PayloadSerializerSettings.ContractResolver = new DefaultContractResolver(); }); }
2.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); // 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.UseHttpsRedirection(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); endpoints.MapHub<ChatHub>("/api/chatHub/signalr"); }); }
3.使用endpoints.MapHub指定signlar的路由
-