如何解决在Windows Forms应用程序中托管gRPC服务
我想创建一个gRPC服务,但是我需要在Winform .net应用程序中托管。 关于Hosting ASP.NET Core API in a Windows Forms Application 我希望有人向我解释,在该示例中,我需要安装什么以及应该更改什么,以便以Windows的形式托管grpc服务...
解决方法
您可以按照相同的步骤进行操作,但还要执行以下几个附加步骤:
- 安装软件包Microsoft.AspNetCore.Grpc.HttpApi,这会将gRPC端点映射到经典HTTP。这不是自动的,您需要在Startup.cs中指定服务,如下所示:
def get_weather(lat,lon):
import json
import requests
r = requests.post('https://api.openweathermap.org/data/2.5/onecall?lat={}&lon={}&exclude=hourly,daily&appid=92d93ccc6ac5587d35d3ccc4479083a1'.format(lat,lon))
data = r.json()
dt = data["dt"]
temp = data["temp"]
weather = data["weather"]
humidity = data["humidity"]
wind_speed = data["wind_speed"]
print(r["dt"])
print(r["temp"])
print(r["weather"])
print(r["humidity"])
print(r["wind_speed"])
get_weather(33,44)
- 在原型中,您需要指示HTTP路径,如下所示:
app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<MygRPCService>();
});
- 添加到您的Startup.cs ConfigureService方法中
rpc Get(GetRequest) returns (GetReply) {
option (google.api.http) = {
get: '/my-endpoint'
body: '*'
};
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。