【发布时间】:2023-04-03 16:11:01
【问题描述】:
我使用 Visual Studio 2017 构建了一个新项目:“ASP.NET Core Web 应用程序”,然后是“API”项目(ASP.NET Core 2.1),我添加了SignalR,如本指南所示:https://code-maze.com/netcore-signalr-angular/
我可以通过在launchSettings.json 中评论 IIS Express 配置文件来使用控制台启动项目,如下所示:
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:64155",
"sslPort": 0
}
},
"profiles": {
/*"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": false,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},*/
"WebApplication2": {
"commandName": "Project",
"launchBrowser": false,
"applicationUrl": "http://localhost:5001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
在这种情况下,我的 (Ionic 4) 客户端成功连接:
[2020-08-22T10:41:21.539Z] Information: WebSocket connected to ws://localhost:5001/testHub?id=iUNCgO8mFOt7MjdQB-Cn_Q.
但是,如果我开始在 IIS Express 中进行调试(没有使用 IISExpress 配置文件,我还尝试为 launchBrowser 属性使用不同的值),API 正在工作,但似乎 SignalR 没有启动。
编辑:
这是我在 IIS Express 中启动 Visual Studio 项目时在控制台上显示的错误,如果我启动客户端而不在 Visual Studio 2017 上启动调试,我会得到相同的错误,因为它在项目作为控制台应用程序启动,我以为我缺少一些配置。
什么是启动SignalR 保持 IIS Express 默认启动方法的正确配置?我希望最后能够发布解决方案并将其部署到 IIS。从 IIS 启动站点是否足以设置 SignalR 配置?
另外,最后是否可以在给定的日志文件中记录 SignalR 消息(处理事件)?
这是我的Startup.cs 文件:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using WebApplication2.SignalR;
namespace WebApplication2
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = 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)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSignalR();
}
// 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.UseCors(options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials());
app.UseMvc();
app.UseSignalR(routes =>
{
routes.MapHub<TestHub>("/testHub");
});
}
}
}
这是Program.cs:
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
namespace WebApplication2
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
是否可以从Main 方法启动SignalR?
【问题讨论】:
-
“signalr 没有启动”是什么意思?当您尝试连接时会发生什么?一个错误,如果有,是哪个?
-
@CamiloTerevinto,我用客户端错误更新了问题
-
@Ansharja 看起来您的客户端应用程序在与 API url 不同的 url 上运行。检查此处提供的解决方案:stackoverflow.com/questions/54762611/…
-
@MohsinMehmood:谢谢,这不起作用,但我找到了另一个解决方案的答案,一旦服务器在 IIS 配置文件上运行,我调用了错误的 URL,请检查我的编辑
标签: c# asp.net-core iis asp.net-core-signalr