从IIS内部的.NET Core 2.1 Web API项目启动SignalR

问题描述

我使用Visual Studio 2017构建了一个新项目:“ ASP.NET Core Web应用程序”,然后构建了“ API”项目(ASP.NET Core 2.1),并按照本指南的说明添加了SignalR:{{ 3}}

我可以通过在控制台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","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无法启动。 / p>

编辑:

这是我在IIS Express中启动Visual Studio项目时在控制台上显示的错误,该错误与如果在不启动Visual Studio 2017上的调试的情况下启动客户端而得到的错误相同,因为当该项目是作为控制台应用程序启动的,我以为我缺少一些配置。

https://code-maze.com/netcore-signalr-angular/

要启动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>();
    }
}

是否可以通过SignalR方法启动Main

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)