【问题标题】:Connection Refused when using Xamarin and SignalR使用 Xamarin 和 SignalR 时连接被拒绝
【发布时间】:2021-10-28 13:47:35
【问题描述】:

您好,我最近在使用 SignalR 和 Xamarin 时遇到了一些问题。我使用了一个 Asp.net 核心 Web 应用程序和一个使用 SignalR 客户端包的 xamarin 表单。每当我尝试连接时,我都会收到错误“连接被拒绝”,我已经将我的代码与其他做同样事情的教程进行了比较,我相信它们匹配但我仍然遇到问题。 这是我的代码:

Xamarin 客户端代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Neighbor_Mobile.SubPages
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class GroupPage : ContentPage
{
    HubConnection connection;
    public GroupPage()
    {
        InitializeComponent();

        AttemptConnect();
    }

    private async void AttemptConnect()
    {
        try
        {
            connection = new HubConnectionBuilder()
                .WithUrl("https://localhost:44353/chat")
                .Build();
            connection.On<string>("ReceiveMessage", (message) => Display.Text = message);
            await SignalRConnect();
        }
        catch (Exception e)
        {
            error.Text = $"error: {e}"; 
        }
    }

    private async Task SignalRConnect()
    {
        try
        {
            //the error occurs here and prints "Connection Refused" to a debug label i'm using
            await connection.StartAsync();
        }
        catch (Exception ex)
        {
            Display.Text = ex.Message;
            return;
        }
    }

    }
}

这是我在服务器上的启动页面代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FleetSocketServer.Hubs;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace FleetSocketServer
{
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSignalR();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHub<SocketGroupHub>("/chat");
        });
    }
}
}

我的 Hub 页面代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;

namespace FleetSocketServer.Hubs
{
public class SocketGroupHub :Hub
{
    public async Task SendMessage(string message)
    {
        await Clients.All.SendAsync("Recieve", message);
    }
}
} 

我的 launchSettings 我尝试在 WithURL 中更改 URl 时使用 iisSettigns 和 FleetSocketServer 对其进行调试,但仍然给我同样的错误

{
"iisSettings": {
"windowsAuthentication": false, 
"anonymousAuthentication": true, 
"iisExpress": {
  "applicationUrl": "http://localhost:35579",
  "sslPort": 44353
 }
 },
 "profiles": {
 "IIS Express": {
  "commandName": "IISExpress",
  "launchBrowser": true,
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
  },
  "FleetSocketServer": {
  "commandName": "Project",
  "launchBrowser": false,
  "applicationUrl": "https://localhost:5001;http://localhost:5050",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
 }
}
}

谢谢!

【问题讨论】:

  • 首先,不要在客户端使用“localhost”。使用服务器的 IP 或 FQDN。第二,确保您的服务器设置为接受远程连接。默认情况下,IIS Express 不会。

标签: c# asp.net-core xamarin.forms asp.net-core-signalr signalr-client


【解决方案1】:

好吧,我真的很愚蠢,原因是安卓模拟器使用 10.0.2.2 作为 localhost 而不是 127.0.0.1,原因很明显。

【讨论】:

    猜你喜欢
    • 2019-12-11
    • 1970-01-01
    • 2017-08-08
    • 2019-12-28
    • 2016-04-06
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多