当前位置: 移动技术网 > IT编程>开发语言>.net > 记录一次Mac VSCode运行Grpc模板项目

记录一次Mac VSCode运行Grpc模板项目

2019年11月30日  | 移动技术网IT编程  | 我要评论

通信工程就业前景,乃组词,防震减灾手抄报

1、使用dotnet new grpc -o grpcgreeter && cd grpcgreeter && code . ,进入项目文件中,使用code .使用vscode打开。

  [注]你可能会遇到'code' command not found?

  解决办法:1)首次使用打开vscode -> command+shift+p -> 输入shell -> 提示shell command:install 'code' in path -> 点击安装

       2)额外知识点:mac在命令行中可以使用open .打开文件夹

2、项目文件打开了,这个时候我们使用dotnet run运行项目。

  [注]你可能会遇到

  1)https certificate not found

  解决办法:一般的解决办法是直接按照提示运行dotnet dev-certs https --trust就可以了,但是我遇到了一个很奇葩的事情

  2)cannot create developer certificate on mac

  解决办法:我重启了下电脑shutdown -r now,然后运行dotnet dev-certs https --trust,输入验证密码,然后ok了       

  3)无法绑定到 ipv4 环回接口上的 https://localhost:5001:在 macos 上不支持 http/2,因为缺少 alpn 支持。 "。

  解决办法:无法在macos启动asp.net core grpc应用

 1 public static ihostbuilder createhostbuilder(string[] args) =>
 2   host.createdefaultbuilder(args)
 3    .configurewebhostdefaults(webbuilder =>
 4    {
 5        webbuilder.configurekestrel(options =>
 6        {
 7            options.listenlocalhost(5000, o => o.protocols = httpprotocols.http2);
 8        });
 9        webbuilder.usestartup<startup>();
10     });

3、创建客户端项目dotnet new console -o grpcgreeterclient,并引入以下三个包:

  dotnet add grpcgreeterclient.csproj package grpc.net.client

  dotnet add grpcgreeterclient.csproj package google.protobuf

  dotnet add grpcgreeterclient.csproj package grpc.tools

4、将服务端的protos/greet.proto拷贝到客户端protos/greet.proto下,并在grpcgreeterclient.csproj项目文件中添加元素项组

<itemgroup>
  <protobuf include="protos\greet.proto" grpcservices="client" />
</itemgroup>

5、在客户端程序中

using system;
using system.net.http;
using system.threading.tasks;
using grpcgreeter;
using grpc.net.client;

namespace grpcgreeterclient
{
    class program
    {
        static async task main(string[] args)
        {
            var channel = grpcchannel.foraddress("http://localhost:5000");
            var client =  new greeter.greeterclient(channel);
            var reply = await client.sayhelloasync(
                              new hellorequest { name = "greeterclient" });
            console.writeline("greeting: " + reply.message);
        }
    }
}

6、运行客户端程序,发现报错

于是我们想到可能是我们为了解决http2问题引起的,如何解决呢?

  解决办法:允许客户端进行不安全连接,添加下行代码

appcontext.setswitch("system.net.http.socketshttphandler.http2unencryptedsupport", true);

7、客户-服务端正常通信

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网