gpt4 book ai didi

c# - IApplicationBuilder 不包含定义 MapSignalR()。 app.MapSignalR() 不适用于 ASP.NET CORE

转载 作者:行者123 更新时间:2023-12-02 01:20:12 28 4
gpt4 key购买 nike

我想将 SignalR 合并到我的项目中,但我不能在 Startup 类中添加行 app.MapSignalR() 时出现错误。这是 StartUp 类:

 public class Startup
{
public Startup(IConfiguration configuration{...}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{...}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{


if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}


app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();

app.UseAuthentication();

app.MapSignalR(); // ERROR - IApplicaionBuilder dos not contain a definition MapSignalR() and the best extension method overload ' OwinExtensios.MapSignalR(IAppBuilder)' requires a receiver of type 'IAppBuilder'


app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}

我已经添加了“使用 Owin;”但它仍然不起作用。我该怎么办?

最佳答案

像这样使用。这是针对 .net core 3.0+ 的

public void ConfigureServices(IServiceCollection services)
{
services.AddSignalR();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/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.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ChatHub>("/chatHub");
});
}

了解更多:https://learn.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-3.1&tabs=visual-studio

如果.net core 2.2则使用下面

app.UseSignalR(routes =>
{
routes.MapHub<ChatHub>("/chatHub");
});

而不是

app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ChatHub>("/chatHub");
});

关于c# - IApplicationBuilder 不包含定义 MapSignalR()。 app.MapSignalR() 不适用于 ASP.NET CORE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59566761/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com