• Asp.Net Core Web MVC调用Grpc服务器


    1、新建Asp.net Core Web Mvc项目

     2、Nuget包下载安装

    Grpc.Net.Client
    Google.ProtoBuf
    Grpc.Tools

    3、新建Protos文件夹,复制之前文章Grpc服务器的greet.proto文件

    syntax = "proto3";
    
    option csharp_namespace = "MyGrpcWeb";
    
    package MyGrpc;
    
    // The greeting service definition.
    service TestGrpc {
      // Sends a greeting
      rpc TestSay (TestRequest) returns (TestReply);
    
      rpc StreamingFromServer(ExampleRequest) returns (stream ExampleResponse);
    
      rpc StreamingFromClient(stream ExampleRequest) returns (ExampleResponse);
    
      rpc StreamingBothWays(stream ExampleRequest) returns (stream ExampleResponse);
    }
    
    // The request message containing the user's name.
    message TestRequest {
      string name = 1;
    }
    
    // The response message containing the greetings.
    message TestReply {
      string message = 1;
    }
    
    message ExampleRequest{
    int32 pageIndex=1;
    int32 pageSize=2;
    bool isDescending=3;
    }
    
    message ExampleResponse{
    string name=1;
    string sex=2;
    }

    修改WebApplication1.csproj 文件,添加以下内容

    <ItemGroup>
        <Protobuf Include="Protosgreet.proto" GrpcServices="Client" />
      </ItemGroup>

    4、修改HomeController.cs

      public class HomeController : Controller
        {
            private readonly ILogger<HomeController> _logger;
            private readonly TestGrpc.TestGrpcClient _testGrpcClient;
            private readonly GrpcChannel _channel;
    
            public HomeController(ILogger<HomeController> logger)
            {
                _logger = logger;
                _channel = GrpcChannel.ForAddress("http://localhost:5000");
                _testGrpcClient = new TestGrpc.TestGrpcClient(_channel);
            }
    
            public async Task<IActionResult> IndexAsync()
            {
                var ret = await _testGrpcClient.TestSayAsync(new TestRequest { Name = "MyAspNetCoreMvc" });
                string msg = ret.Message;
                ViewData["GrpcMsg"] = msg;
                return View();
            }
    
            public IActionResult Privacy()
            {
                return View();
            }
    
            [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
            public IActionResult Error()
            {
                return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
            }
        }

    5、修改Index.cshtml

    @{
        ViewData["Title"] = "Home Page";
    }
    
    <div class="text-center">
        <h1 class="display-4">Welcome</h1>
        <h2>发送消息的用户:@ViewData["GrpcMsg"] </h2>
        <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
    </div>

    6、修改launchSettings.json

    {
     
      "profiles": {
            "WebApplication1": {
          "commandName": "Project",
          "dotnetRunMessages": "true",
          "launchBrowser": true,
          "applicationUrl": "http://localhost:5002",
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }

     或者修改appsettings.json文件

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "Urls": "http://localhost:5002",
      "AllowedHosts": "*"
    }
  • 相关阅读:
    电商工具 谷歌插件 版本 2021-03-04
    PowerDesigner 自定义脚本
    MapReduce案例之寻找共同好友
    Hadoop之MapReduce开发总结
    python之文件操作
    python字典、集合
    python元组
    python列表练习
    python之列表
    python之编码解码、字符串常用方法
  • 原文地址:https://www.cnblogs.com/lhwpc/p/15165861.html
Copyright © 2020-2023  润新知