gRpc Unity3D 客户端和 ASP.Net 核心服务器中的异常 "failed to connect to all addresses"

Exception "failed to connect to all addresses" in gRpc Unity3D client and ASP.Net core server

我用控制台客户端测试了 grcp asp.net 服务器,它可以正常工作。当然第一个问题是认证,但在客户端安装认证后它开始工作。

现在我正在使用 Unity3D 版本 2021.1.2f1 的新客户端测试同一台服务器。我像在教程中一样将项目设置为 .NET 4.x。安装插件 grcp 以实现统一。但是出了点问题。我觉得认证可能有问题,但是有没有办法为统一客户端安装认证? Unity 不支持 http2,所以我可能需要在服务器和客户端中配置它,或者它会自动配置?可能是其他方面的问题,但我无法想象是什么问题。

下面出现异常。

RpcException: Status(StatusCode="Unavailable", Detail="failed to connect to all addresses", DebugException="Grpc.Core.Internal.CoreErrorDetailException: {"created":"@1626079380.128000000","description":"Failed to pick subchannel","file":"..\..\..\src\core\ext\filters\client_channel\client_channel.cc","file_line":3009,"referenced_errors":[{"created":"@1626079380.128000000","description":"failed to connect to all addresses","file":"..\..\..\src\core\ext\filters\client_channel\lb_policy\pick_first\pick_first.cc","file_line":398,"grpc_status":14}]}")
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () (at <695d1cc93cca45069c528c15c9fdd749>:0)
GameManager+<Start>d__1.MoveNext () (at Assets/GameManager.cs:29)
--- End of stack trace from previous location where exception was thrown ---
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) (at <695d1cc93cca45069c528c15c9fdd749>:0)
UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at <9244fb8344e84288877b43b45b29c242>:0)
UnityEngine.UnitySynchronizationContext:ExecuteTasks()

原型文件

syntax = "proto3";

option csharp_namespace = "GameNetRpc";

package game;

service GameNet {
  rpc SayHello (HelloRequest) returns (HelloReply);
}

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}

启动设置

{
  "profiles": {
    "AspGrpcServer": {
      "commandName": "Project",
      "dotnetRunMessages": "true",
      "launchBrowser": false,
      "applicationUrl": "http://localhost:5000;https://localhost:5001",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

客户代码

using GameNetRpc;
using Grpc.Core;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    private Channel _channel;
    private async void Start()
    {
        _channel = new Channel("localhost", 5001, ChannelCredentials.Insecure);
        var client = new GameNet.GameNetClient(_channel);
        var reply = await client.SayHelloAsync(new HelloRequest { Name = "Client unity" });
        Debug.Log("Response from server: " + reply.Message);
    }

    private void OnDestroy()
    {
        _channel.ShutdownAsync().Wait();
    }
}

我开始使用 MagicOnion 并找到错误的解决方案 here https://github.com/Cysharp/MagicOnion/#support-for-unity-client