从 IIS 内的 .NET Core 2.1 Web API 项目启动 SignalR
Launch SignalR from .NET Core 2.1 Web API project inside IIS
我用 Visual Studio 2017 构建了一个新项目:“ASP.NET 核心 Web 应用程序”,然后是“API”项目(ASP.NET 核心 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
?
您是否尝试过将 cors 添加到您的 startup.cs 文件中?
我将解决方案发布为编辑,但可能不容易看到。
我在这里找到了我的问题的正确答案::在移动到 IIS Express 配置文件后,我不得不将客户端调用的 url 从 http://localhost:5001/testHub
更改为 http://localhost:64155/testHub
.
我用 Visual Studio 2017 构建了一个新项目:“ASP.NET 核心 Web 应用程序”,然后是“API”项目(ASP.NET 核心 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
?
您是否尝试过将 cors 添加到您的 startup.cs 文件中?
我将解决方案发布为编辑,但可能不容易看到。
我在这里找到了我的问题的正确答案::在移动到 IIS Express 配置文件后,我不得不将客户端调用的 url 从 http://localhost:5001/testHub
更改为 http://localhost:64155/testHub
.