asp.net-mvc – 在SignalR Core中更改全局设置配置
发布时间:2020-12-16 07:02:21 所属栏目:asp.Net 来源:网络整理
导读:我在 ASP.Net Core使用 SignalR Core . 我想覆盖signalR的GlobalHost设置. 我到了this– protected void Application_Start(object sender,EventArgs e){ // Make long polling connections wait a maximum of 110 seconds for a // response. When that tim
我在
ASP.Net Core使用
SignalR Core.
我想覆盖signalR的GlobalHost设置. 我到了this– protected void Application_Start(object sender,EventArgs e) { // Make long polling connections wait a maximum of 110 seconds for a // response. When that time expires,trigger a timeout command and // make the client reconnect. GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(110); // Wait a maximum of 30 seconds after a transport connection is lost // before raising the Disconnected event to terminate the SignalR connection. GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(30); // For transports other than long polling,send a keepalive packet every // 10 seconds. // This value must be no more than 1/3 of the DisconnectTimeout value. GlobalHost.Configuration.KeepAlive = TimeSpan.FromSeconds(10); RouteTable.Routes.MapHubs(); } 但我不能用我的应用程序配置它. 它位于ASP.Net Core v1.0上. 我的Startup.cs就像这样 – using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Cors.Infrastructure; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace McpSmyrilLine { public class Startup { public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json",optional: true,reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json",optional: true) .AddEnvironmentVariables(); if (env.IsDevelopment()) { // This will push telemetry data through Application Insights pipeline faster,allowing you to view results immediately. builder.AddApplicationInsightsSettings(developerMode: true); } Configuration = builder.Build(); } public IConfigurationRoot Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddApplicationInsightsTelemetry(Configuration); //Add DB Context var connectionStringBuilder = new Microsoft.Data.Sqlite.SqliteConnectionStringBuilder { DataSource = "mcp.db" }; var connectionString = connectionStringBuilder.ToString(); ///////////////Add Cors var corsBuilder = new CorsPolicyBuilder(); corsBuilder.AllowAnyHeader(); corsBuilder.AllowAnyMethod(); corsBuilder.AllowAnyOrigin(); corsBuilder.AllowCredentials(); services.AddCors(options => { options.AddPolicy("AllowAll",corsBuilder.Build()); }); ///////////////End Cors services.AddDbContext<McpDbContext>(options => options.UseSqlite(connectionString)); services.AddMvc(); services.AddSignalR(options => { options.Hubs.EnableDetailedErrors = true; }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app,IHostingEnvironment env,ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); //Configure Cors app.UseCors("AllowAll"); app.UseSignalR(); app.UseApplicationInsightsRequestTelemetry(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); //Adding Seeder Data AddTestData(app.ApplicationServices.GetService<McpDbContext>()); } else { app.UseExceptionHandler("/Home/Error"); } app.UseApplicationInsightsExceptionTelemetry(); app.UseStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: "default",template: "{controller=Home}/{action=Index}/{id?}"); }); } private static void AddTestData(McpDbContext context) { //var testUser1 = new DbModels.Try.User //{ // Id = "abc123",// FirstName = "Luke",// LastName = "Skywalker" //}; //context.Users.Add(testUser1); //var testPost1 = new DbModels.Try.Post //{ // Id = "def234",// UserId = testUser1.Id,// Content = "What a piece of junk!" //}; //context.Posts.Add(testPost1); //context.SaveChanges(); } } } 有人可以帮忙吗? 解决方法
SignalROptions有一个Transports属性
您可以像这样设置SignalR中间件: services.AddSignalR(options => { options.Hubs.EnableDetailedErrors = true; var transports = options.Transports; transports.DisconnectTimeout = TimeSpan.FromSeconds(30); transports.KeepAlive = TimeSpan.FromSeconds(10); transports.TransportConnectTimeout = TimeSpan.FromSeconds(110); }); 更新alpha2-final 可以通过MapHub配置传输选项: app.UseSignalR(configure => { configure.MapHub<Hub>("hub",options => { options.Transports = TransportType.All; options.LongPolling.PollTimeout = TimeSpan.FromSeconds(10); options.WebSockets.CloseTimeout = TimeSpan.FromSeconds(10); }); }) 在客户端: let logger: ILogger; let transportType: TransportType; const hubConnetion = new HubConnection( new HttpConnection( url,{ transport: transportType,logging: logger })); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 我可以在DefaultModelBinder上禁用DataAnnotati
- asp.net-mvc – 表单帖子永远不会执行操作,并且在MVC 6 RC2
- asp.net – 如何获取当前的月份和年份
- asp.net-mvc – telerik-grid onRowSelect如何获取id?
- asp.net-mvc – Elmah.Mvc 2.0自定义身份验证
- asp.net-mvc-3 – 如何将模型绑定中使用的所有ValueProvide
- 自KB4338419以来,无法再从Classic ASP创建C#COM对象
- 非锁定进程中的ASP.NET会话状态存储
- 手动搭建自己的nuget服务器及使用
- asp.net – CompositeControl中的DropDownList是否应该记住
推荐文章
站长推荐
- 使用ASP.NET将用户订阅到MailChimp
- asp.net – 调用en外部javascript文件的函数
- asp.net-core-mvc – 如何创建从HttpContext.Tra
- 数组 – ASP MVC控制器动作可以接收一个byte []吗
- asp.net – 如何捕获异常形式ObjectDataSource.U
- ASP.NET Web.config转换将无法在安装程序中使用
- asp.net-mvc – 上传文件并验证文件扩展名和文件
- 澄清ASP.NET生命周期事件的序列
- asp.net listview Container.DataItemIndex
- asp.net-mvc – ASP.NET MVC中依赖于语言的路由
热点阅读