当前位置:网站首页>. Net core 3.0 grpc custom service

. Net core 3.0 grpc custom service

2022-06-09 04:48:00 Wind god Shura envoy

Server side

newly build gRPC project
 Insert picture description here
Right click Protos Folder –> Create a new one .proto, Select the protocol buffer file , Used to describe gRPC Messages and services for
 Insert picture description here
Name at will , It's called UpImg Well
 Insert picture description here

Then put the package name , The service name , Modify the parameter and return value names
 Insert picture description here
Set the default method name SayHello And parameters and return values are also modified
 Insert picture description here
Change to our own defined parameters and return values
 Insert picture description here
Create a new one UpImgService, Implement what we just defined above gRPC The service description UpImg.proto

Note that the new service cannot be found by direct implementation
 Insert picture description here
Because it's not there yet obj\Debug\netcoreapp3.0 The following generates the corresponding class

You need to double-click our project to edit csproj file , Put the new UpImg.proto Add to the configuration
 Insert picture description here
Add it to the configuration and save it in obj\Debug\netcoreapp3.0 The corresponding class can be generated in
 Insert picture description here
Then continue to implement UpImg.proto, The implementation is very simple. Just pay attention to the parameters and return values
 Insert picture description here

client

The client needs to add a package :Grpc.Net.Client、Google.Protobuf、Grpc.Tools

  <ItemGroup>
    <PackageReference Include="Google.Protobuf" Version="3.11.2" />
    <PackageReference Include="Grpc.Net.Client" Version="2.26.0" />
    <PackageReference Include="Grpc.Tools" Version="2.26.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

hold UpImg.proto Copy to... In the client Proto( Here we use direct copy )
 Insert picture description here
Also add configuration on the client , Adding configuration and client will automatically generate corresponding code
 Insert picture description here

And then call it :

public async void UpLoad()
{
    
    // Settings allow unsafe HTTP2 Support 
    AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
    var channel = GrpcChannel.ForAddress("http://localhost:50001");
    var client = new UpImg.UpImgClient(channel);
    var reply = await client.DoUpLoadAsync(new UpImgRequest {
     Img = "hello girl" });
    Console.WriteLine(reply.Message);
}

gRPC The call was actively rejected by the target computer , Unable to connect .
Modify port settings , May be https Caused by the
 Insert picture description here

Code :

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
    
            webBuilder.ConfigureKestrel(options => {
    

                options.ListenLocalhost(8099,a=>a.Protocols= Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http2);
            });

            webBuilder.UseStartup<Startup>();
        });

There is also the possibility of error Grpc.Core.RpcException:“Status(StatusCode=Unimplemented, Detail="Service is unimplemented.")”
Think you haven't started the service you called yet
 Insert picture description here

原网站

版权声明
本文为[Wind god Shura envoy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203021506048012.html