.net5 grpc服务在windows上的架设方法
.net 5 编写的grpc服务,在windows2016 IIS10上没有架设成功,试过了http https均不可行,可能是没有SSL证书的原因,自签名证书也试过了,都无法连接,具体错误信息如下
http
Grpc.Core.RpcException: Status(StatusCode="Internal", Detail="Error starting gRPC call. HttpRequestException: An error occurred while sending the request. IOException: The request was aborted. Http2ConnectionException: The HTTP/2 server sent invalid data on the connection. HTTP/2 error code 'PROTOCOL_ERROR' (0x1).", DebugException="System.Net.Http.HttpRequestException: An error occurred while sending the request.
https
Grpc.Core.RpcException: Status(StatusCode="Cancelled", Detail="No grpc-status found on response.")
官方给出的文档似乎也不能解决
https://docs.microsoft.com/zh-cn/iis/get-started/whats-new-in-iis-10/http2-on-iis
至少我也没有成功
后来想到将grpc服务作成windows服务,搞定
首先添加 Microsoft.AspNetCore.Hosting.WindowsServices 的包引用
在 Program.cs 的 CreateHostBuilder
中:
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseWindowsService() .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
然后发布项目
以管理员身份打开命令提示符
接下来,我们可以使用标准Windows服务命令将exe作为服务安装。因此,将命令提示符移动到输出文件夹(可能沿着myproject\bin\Release\netcoreapp3.1\publish)。并运行类似的内容以作为服务安装:
sc create ASPNETCoreWindowsService binPath=ASPNETCoreWindowsService.exe
当然我们也可以停止和删除该服务
sc.exe stop ASPNETCoreWindowsService
sc.exe delete ASPNETCoreWindowsService
然后在windows服务中,找到服务名为 ASPNETCoreWindowsService 的服务,启动,并设为自动启动
访问http://ip:5000,即可
具体可参照
https://cloud.tencent.com/developer/article/1571026
此处不再重复粘贴
服务的端口号,可在appsettings.json的 "Urls"节点中配置,例如我的配置为
少了一层IIS反向代理,按理说效率应该更高吧