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

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

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

 

 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="Protos\greet.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": "*"
}

 

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

上一篇:html的固定定位,锚点设置,水平居中,垂直居中


下一篇:JS实现~~斐波那数列