【问题标题】:use Kestrel in .NET Core worker project在 .NET Core 辅助项目中使用 Kestrel
【发布时间】:2020-12-25 17:15:30
【问题描述】:

我使用 Visual Studio 提供的模板创建了一个新的 .NET Core 辅助项目。我想监听传入的 TCP 消息和 HTTP 请求。我正在关注 David Fowler's "Multi-protocol Server with ASP.NET Core and Kestrel" repository 如何设置 Kestrel。

我所要做的就是安装 Microsoft.AspNetCore.Hosting 包以访问UseKestrel 方法。

Program.cs 文件中我目前正在这样做

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureServices((hostContext, services) =>
            {
                services.AddHostedService<Worker>();
            });
}

很遗憾,我无法将 UseKestrel 附加到 ConfigureServices 方法中。我认为这是因为我使用的是IHostBuilder 接口而不是IWebHostBuilder 接口。

这个项目不应该是一个 Web API 项目,它应该是一个 Worker 项目。

任何想法如何为此配置 Kestrel?


我尝试将代码更改为示例存储库中的代码

using System.Net;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace Service
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .ConfigureServices(services =>
                {
                    // ...
                })
                .UseKestrel(options =>
                {
                    // ...
                });
    }
}

这样做时它仍然无法解决 WebHost 并出现这些错误

  • 没有给出与“ConnectionBuilderExtensions.Run(IConnectionBuilder, Func)”所需的形式参数“中间件”相对应的参数

  • 当前上下文中不存在名称“WebHost”

我认为这是因为工作项目不使用 Web SDK。

【问题讨论】:

  • 据我所知 kestrel 是 webhostbuilder 的扩展。如果您需要在控制台应用程序中使用 Web 服务器,请在您的 program.cs 中使用 webhostbuilder
  • 嗯,我试过了,但没用……我更新了我的问题

标签: c# .net-core kestrel


【解决方案1】:

您使用 IHostbuilder 启用 HTTP 工作负载,您需要在您的 Host.CreateDefaultBuilder 中添加 .ConfigureWebHostDefaults。由于 Kestrel 是网络服务器,您只能从网络主机配置它。

在你的 program.cs 文件中

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                   webBuilder.UseStartup<Startup>();
                    webBuilder.UseKestrel(options =>
                    {
                        // TCP 8007
                        options.ListenLocalhost(8007, builder =>
                        {
                            builder.UseConnectionHandler<MyEchoConnectionHandler>();
                        });

                        // HTTP 5000
                        options.ListenLocalhost(5000);

                        // HTTPS 5001
                        options.ListenLocalhost(5001, builder =>
                        {
                            builder.UseHttps();
                        });
                    });
                });

或者

public static IHostBuilder CreateHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseKestrel(options =>
            {
                // TCP 8007
                options.ListenLocalhost(8007, builder =>
                {
                    builder.UseConnectionHandler<MyEchoConnectionHandler>();
                });

                // HTTP 5000
                options.ListenLocalhost(5000);

                // HTTPS 5001
                options.ListenLocalhost(5001, builder =>
                {
                    builder.UseHttps();
                });
            });

由于您使用 Web 服务器启用 HTTP 工作负载,因此您的项目必须是 web 类型。仅当项目为 web 类型时才会启用 WebHost。因此,您需要更改您的 SDK 以在您的 csproj 文件中使用 web。

<Project Sdk="Microsoft.NET.Sdk.Web">

参考:

【讨论】:

    【解决方案2】:

    NET 5.0 如下

    1. 将框架引用添加到 ASP.NET:

      <Project Sdk="Microsoft.NET.Sdk.Worker">
          <ItemGroup>
              <FrameworkReference Include="Microsoft.AspNetCore.App" />
          </ItemGroup>
      </Project>
      
    2. program.cs上添加网页配置

      public static IHostBuilder CreateHostBuilder(string[] args) =>
          Host.CreateDefaultBuilder(args)
              .UseWindowsService()
              .ConfigureAppConfiguration((hostingContext, config) =>
              {
                  config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false);
                  config.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true);
      
                  Configuration = config.Build();
               })
      
               //Worker
               .ConfigureServices((hostContext, services) =>
               {
                   services.AddHostedService<ServiceDemo>();
               })
      
               // web site
               .ConfigureWebHostDefaults(webBuilder =>
               {
                   webBuilder.UseStartup<Startup>();
                   webBuilder.UseKestrel(options =>
                   {
                       // HTTP 5000
                       options.ListenLocalhost(58370);
      
                       // HTTPS 5001
                       options.ListenLocalhost(44360, builder =>
                       {
                           builder.UseHttps();
                       });
                   });
               });
      
    3. 配置Startup

      public class Startup
      {
          public IConfiguration Configuration { get; }
      
          public Startup(IConfiguration configuration)
          {
              Configuration = configuration;
          }
      
          public void ConfigureServices(IServiceCollection services)
          {
          }
      
          public void Configure(IApplicationBuilder app)
          {
              var serverAddressesFeature = app.ServerFeatures.Get<IServerAddressesFeature>();
      
              app.UseStaticFiles();
              app.Run(async (context) =>
              {
                  context.Response.ContentType = "text/html";
                  await context.Response
                      .WriteAsync("<!DOCTYPE html><html lang=\"en\"><head>" +
                                  "<title></title></head><body><p>Hosted by Kestrel</p>");
      
                  if (serverAddressesFeature != null)
                  {
                      await context.Response
                          .WriteAsync("<p>Listening on the following addresses: " +
                                      string.Join(", ", serverAddressesFeature.Addresses) +
                                      "</p>");
                  }
      
                  await context.Response.WriteAsync("<p>Request URL: " +
                                                    $"{context.Request.GetDisplayUrl()}<p>");
              });
          }
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 2019-10-02
      • 2021-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多