1

Hello I've been having some trouble with SignalR and Xamarin recently. I use a Asp.net core web app and a xamarin forms using the SignalR Client package. Whenever I try to connect I keep getting the error "Connection Refused", I've compared my code with other tutorials doing the same thing and I believe they match but I still get the issue. Here is my code:

Xamarin client side code

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;
        }
    }

    }
}

Here is my Startup page code on the server:

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");
        });
    }
}
}

My Hub page code:

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);
    }
}
} 

My launchSettings I've tried debugging it with iisSettigns and FleetSocketServer while changing the URl in WithURL but still gives me the same error

{
"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"
  }
 }
}
}

Thank you!

  • 1
    first, do not use "localhost" in the client. Either use the IP or FQDN of the server. 2nd, be sure that your server is setup to accept remote connections. By default IIS Express does not. – Jason Aug 29 '21 at 13:40

1 Answers1

0

Okay I was really dumb the reasoning was because the andriod emulator uses 10.0.2.2 as localhost rather than 127.0.0.1 for obvious reasons.