【发布时间】:2021-09-21 11:41:14
【问题描述】:
我用控制台客户端测试了 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();
}
}
【问题讨论】:
-
请注意 Grpc.Core 仅在实验上支持统一(=未官方支持)。此外,我们计划在未来弃用 Grpc.Core:grpc.io/blog/grpc-csharp-future
标签: c# asp.net asp.net-core unity3d grpc