简体   繁体   English

Xamarin.Forms 和 gRPC。 “无法连接到所有地址”错误消息

[英]Xamarin.Forms and gRPC. "failed to connect to all addresses" error message

I'm struggling to get a gRPC client built with Xamarin.Forms to communicate with a gRPC server running on my localhost.我正在努力获得一个使用 Xamarin.Forms 构建的 gRPC 客户端,以便与在我的本地主机上运行的 gRPC 服务器进行通信。 The server can be reached without issue with BloomRPC, however I can't seem to get it to work properly from my client.使用 BloomRPC 可以毫无问题地访问服务器,但是我似乎无法从我的客户端使其正常工作。 The server is listening on http://localhost:5000 and https://localhost:5001.服务器正在监听 http://localhost:5000 和 https://localhost:5001。

I initially thought that perhaps it was because it's running on localhost that my client is struggling, so I also tried using a RemoteURL courtesy of Conveyor by Keyoti, which is set to http://192.168.1.64:45455 and https://192.168.1.64:45456 , however I'm not quite sure how that works with my gRPC server as it is specifically listening on ports 5000 and 5001. Either way, trying to connect by either method results in an RPCException stating "failed to connect to all addresses".我最初认为可能是因为它在本地主机上运行我的客户正在苦苦挣扎,所以我还尝试使用由 Keyoti 提供的 Conveyor 提供的 RemoteURL,设置为http://192.168.1.64:45455Z5E056C500A1C4B6A7110B50D8087BADE5Z: // .1.64:45456 ,但是我不太确定它如何与我的 gRPC 服务器一起工作,因为它专门侦听端口 5000 和 5001。无论哪种方式,尝试通过任一方法连接都会导致 RPCException 声明“无法连接到所有地址”。

I'm using the Grpc.Core library, and Grpc.Core.Xamarin (version 2.41.1) with the below code.我正在使用带有以下代码的 Grpc.Core 库和 Grpc.Core.Xamarin(版本 2.41.1)。 I've also tried using version 1.20.1, but without success.我也尝试过使用 1.20.1 版本,但没有成功。 The below code tries to connect to the authentication service which simply returns a GUID.下面的代码尝试连接到仅返回 GUID 的身份验证服务。

public static async Task<Msg.SessionTokenResponse> test()
{
    // I've also tried, localhost:5000, localhost:5001 (with and without SSL credentials), 
    // 127.0.0.1:5001 (with and without SSL credentials), 0.0.0.0:5000, 0.0.0.0:5001 
    // (with and without SSL credentials), 192.168.1.64:45455, 192.168.1.64:45456 (with and 
    // without SSL credentials)
    var channel = new Channel("127.0.0.1:5000", ChannelCredentials.Insecure);

    var client = new AuthenticationService.AuthenticationServiceClient(channel);
    var request = new Msg.SessionTokenRequest();

    try
    {
        return await client.CreateSessionTokenAsync(request);
    }
    catch(Exception ex)
    {
        Console.WriteLine(ex.Message);
        throw ex;
    }
    finally
    {
        await channel.ShutdownAsync();
    }
}

It's also worth noting if I try to pass http:// or https:// at the start of the address I get a "DNS resolution failed for service" error message.还值得注意的是,如果我尝试在地址开头传递 http:// 或 https:// ,则会收到“DNS 解析服务失败”错误消息。 Upon reading up it looks like this is not meant to be provided as part of the address, but I may be misunderstanding the requirements.阅读后,看起来这并不意味着作为地址的一部分提供,但我可能误解了要求。 I have tried this with my firewall disabled, and have no proxy.我已在禁用防火墙的情况下尝试过此操作,并且没有代理。 I've also tried adding the following environment variables:我还尝试添加以下环境变量:

Environment.SetEnvironmentVariable("NO_PROXY", "127.0.0.1");
Environment.SetEnvironmentVariable("GRPC_DNS_RESOLVER", "native");
Environment.SetEnvironmentVariable("GRPC_VERBOSITY", "debug");
Environment.SetEnvironmentVariable("GRPC_TRACE", "all");

I can't see anything helpful in the logging.我在日志记录中看不到任何有用的信息。

Any and all help would be greatly appreciated.任何和所有的帮助将不胜感激。

After all the help from @Jason I was finally able to get this resolved.在@Jason 的所有帮助之后,我终于能够解决这个问题。

As he alluded to, the problem was because the code is running on the simulators, not my local machine (even if the simulators are on my local machine), and thus referring to localhost will simply try to access the simulator, not my PC.正如他所提到的,问题是因为代码运行在模拟器上,而不是我的本地机器上(即使模拟器在我的本地机器上),因此引用 localhost 只会尝试访问模拟器,而不是我的 PC。

To get around this without publishing my gRPC server to some other device, I simply needed to change the applicationUrl in my gRPC server's launchSettings.json file so that it would listen specifically to my internal IP address, and not localhost.为了在不将我的 gRPC 服务器发布到其他设备的情况下解决这个问题,我只需要在我的 gRPC 服务器的 launchSettings.json 文件中更改 applicationUrl,以便它专门监听我的内部 IP 地址,而不是 localhost。 My launchSettings.json file looks like below:我的 launchSettings.json 文件如下所示:

{
  "profiles": {
    "MyGRPCServer.API": {
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://192.168.1.64:45456;http://192.168.1.64:45455"
    }
  }
}

I then simply whitelisted ports 45455 and 45456 in my firewall and the simulators were able to access my gRPC server.然后,我简单地将防火墙中的端口 45455 和 45456 列入白名单,模拟器就能够访问我的 gRPC 服务器。

I also changed我也变了

var channel = new Channel("127.0.0.1:5000", ChannelCredentials.Insecure);

to

var channel = new Channel("192.168.1.64:45455", ChannelCredentials.Insecure);

Thanks again for all the help Jason, and hopefully this helps someone else.再次感谢 Jason 的所有帮助,希望这对其他人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM