zoukankan      html  css  js  c++  java
  • 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": "*"
    }
  • 相关阅读:
    CachedRowSet使用
    mybatis There is no getter for property named 'xx' in 'class java.lang.String
    基于tcpdump的Android智能移动终端数据包捕获完整解决方案
    analytics详解
    android开发图片分辨率
    缩放图片,解决bitmap 内存溢出out of memory的问题
    使用windowAnimations定义Activity及Dialog的进入退出效果
    读取本地已有的.db数据库
    MyBatis 问题列表
    cxf 相关问题
  • 原文地址:https://www.cnblogs.com/lhwpc/p/15165861.html
Copyright © 2011-2022 走看看