kestrel虚拟服务器,如何使Kestrel Web服务器监听非本地主机的请求?

小编典典

Kestrel服务器使用的默认配置文件是hosting.json。在不同的beta版本中,名称多次更改。如果您现在project.json使用以下"command"部分

"commands": {

"web": "Microsoft.AspNet.Server.Kestrel"

}

然后在从命令行启动服务器的过程中

dnx web

该文件hosting.json将被读取。文件

{

"server.urls": "http://0.0.0.0:5000"

}

将配置服务器在每个IP4地址上侦听5000。配置

{

"server.urls": "http://::5000;http://0.0.0.0:5000"

}

将通知在IP4和IP6地址上侦听5000。

可以通过使用ASPNET_ENV环境变量或--config

myconfig1.json(或config=myconfig1.json)的使用来指定备用配置文件。例如,您可以使用

SET ASPNET_ENV=Development

并创建hosting.Development.json具有特定配置的文件。或者,您可以project.json与

"commands": {

"web": "Microsoft.AspNet.Server.Kestrel"

"webProd": "Microsoft.AspNet.Server.Kestrel --config prod.json"

}

并按使用量启动服务器

dnx webProd

我还必须提醒您,可能需要您允许另外收听和注册(开始dnx web)。由于防火墙和侦听新的TCP /

HTTP端口的本地安全性,因此需要它。如下所示,应该为每个人(IPv4和IPv6)进行本地注册和侦听5000端口:

netsh http add iplisten ipaddress=0.0.0.0:5000

netsh http add iplisten ipaddress=::5000

netsh http add urlacl url=http://+:5000/ user=\Everyone

为了更加安全,您可以调整上述配置以授予最少的权限。

更新: 谢谢@BlaneBunderson。可以使用代替IP地址(例如http://*:5000)来监听来自任何接口的任何*

IP4和IP6地址。一个应该小心,不要使用这些

http://*:5000;http://::5000

http://::5000;http://*:5000

http://*:5000;http://0.0.0.0:5000

http://*:5000;http://0.0.0.0:5000

因为它将需要 两次 注册IP6地址::或IP4地址。0.0.0.0 __

对应于公告

从技术上讲,任何不是“ localhost”或有效IPv4或IPv6地址的主机名都将导致Kestrel绑定到所有网络接口。

我认为这种行为将来可能会改变。因此,我建议只使用*:5000,0.0.0.0:5000并::5000构成对任何IT地址的注册。

更新2: ASP.NET Core

RC2更改了加载默认值的行为(请参见声明)。必须更改Main才能从中加载设置hosting.json和命令行参数。以下是用法示例

public static void Main(string[] args)

{

var config = new ConfigurationBuilder()

.SetBasePath(Directory.GetCurrentDirectory())

.AddJsonFile("hosting.json", optional: true)

.AddEnvironmentVariables(prefix: "ASPNETCORE_")

.AddCommandLine(args)

.Build();

var host = new WebHostBuilder()

.UseUrls("http://*:1000", "https://*:1234", "http://0.0.0.0:5000")

.UseEnvironment("Development")

.UseConfiguration(config)

.UseKestrel()

.UseContentRoot(Directory.GetCurrentDirectory())

.UseIISIntegration()

.UseStartup()

.Build();

host.Run();

}

上面的代码使用了三个绑定:"http://*:1000","https://*:1234","http://0.0.0.0:5000"在默认情况下,而不是在默认情况下使用默认端口5000(准确地说的使用http://localhost:5000)。的呼叫.UseConfiguration(config)是在

之后

发出的.UseUrls。因此,hosting.json从命令行加载的配置或命令行将覆盖默认选项。如果有.SetBasePath(Directory.GetCurrentDirectory())一行删除行,hosting.json则将从将编译应用程序dll的目录下加载该目录(例如bin\Debug\netcoreapp1.0)。

一个人可以像这样使用执行

dotnet.exe run --server.urls=http://0.0.0.0:5000

覆盖默认设置(from UseUrls)和from "server.urls"属性的设置(hosting.json如果存在)。

以相同的方式,可以通过设置环境变量来覆盖ULR设置

set ASPNETCORE_SERVER.URLS=http://localhost:12541/

然后使用的应用程序的默认启动dotnet.exe run将http://localhost:12541/用于绑定。

您可以在此处找到HTTPS绑定用法的示例。

备注:

在ASP.NET的更高版本中,环境变量的名称从更改ASPNETCORE_SERVER.URLS为ASPNETCORE_URLS(请参阅此处的ASP.NET Core 3.1文档)。

2020-05-19

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Kestrel创建TCP服务器,您需要使用`Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets` NuGet包。以下是一个示例,演示如何使用Kestrel创建TCP服务器: ```csharp using System; using System.Net; using System.Net.Sockets; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets; class Program { static async Task Main(string[] args) { var host = new WebHostBuilder() .UseKestrel(options => { options.Listen(IPAddress.Any, 1234, listenOptions => { listenOptions.UseConnectionHandler<TcpConnectionHandler>(); }); }) .ConfigureServices(services => services.AddSingleton<TcpConnectionHandler>()) .Configure(app => app.UseRouting()) .Build(); await host.RunAsync(); } } class TcpConnectionHandler : ConnectionHandler { public override async Task OnConnectedAsync(ConnectionContext connection) { Console.WriteLine($"New client connected: {connection.ConnectionId}"); while (true) { var buffer = new byte[1024]; var result = await connection.Transport.Input.ReadAsync(buffer); var input = Encoding.UTF8.GetString(buffer.Slice(0, result.Buffer.Length)); Console.WriteLine($"Received from client {connection.ConnectionId}: {input}"); if (result.IsCompleted) { break; } } } } ``` 在此示例中,`Listen`方法用于配置Kestrel服务器以侦听TCP连接。使用`UseConnectionHandler`方法指定连接处理程序,该处理程序将处理所有传入的连接。在此示例中,我们使用`TcpConnectionHandler`类作为连接处理程序。在`TcpConnectionHandler`的`OnConnectedAsync`方法中,我们读取来自客户端的数据并打印到控制台上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值