zoukankan      html  css  js  c++  java
  • WebApi集成Grpc客户端

    1.依赖包

    • Grpc.Net.Client
    • Google.Protobuf
    • Grpc.Tools

    2.把相应的protos文件copy到项目文件目录下,重新编译项目,IDE会自动给你在项目文件中添加你的proto文件引用,如果没有可以自己手动添加。

        <ItemGroup>
            <Protobuf Include="Protos*.proto" GrpcServices="Client" Link="Protos\%(RecursiveDir)%(Filename)%(Extension)" />
            <Protobuf Update="ProtosTest.proto">
                <Link>ProtosTest.proto</Link>
            </Protobuf>
        </ItemGroup>
    

    4.通过ioc的模式来调用grpc服务,配置Startup.ConfigureServices

                AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
                services.AddGrpcClient<Test.TestClient>(options => {
                    options.Address = new Uri("http://localhost:5000");
                });
    

    5.直接注入客户端后调用。

        public class DemoService : IDemo
        {
            private readonly Test.TestClient _TestClient;
            public DemoService(Test.TestClient TestClient)
            {
                _TestClient = TestClient
            }
    
            public void Test()
            {
                //调用
                TestClient.PrintAsync(new str 
                {
                    str = "hello"
                });
            }
        }
    
  • 相关阅读:
    大端小端
    浅谈协程
    boost总结之any
    boost总结之variant
    STL总结之functor
    zabbix设置多个收件人
    zabbix设置发送消息的时间
    zabbix利用微信报警
    Windos无法验证文件数组签名
    zabbix基础安装
  • 原文地址:https://www.cnblogs.com/linhuiy/p/13940466.html
Copyright © 2011-2022 走看看