4

My Xamarin Forms Android application will not connect to my SignalR server.

I've got a website in IIS which hosts a simple SignalR chat hub which I can connect to with a Javascript client, a Console app, and a WPF app, but this Xamarin Forms Android app is not connecting. The Xamarin Forms projects have the Microsoft.AspNetCore.SignalR.Client v3.1.4 package added.

This is how I'm building the HubConnection:

hubConnection = new HubConnectionBuilder().WithUrl(@"http://Acme.RealTimeService.com/messages").Build();

The offending code:

await hubConnection.StartAsync();

The offensive error:

System.IO.IOException: Unable to read data from the transport connection: Connection reset by peer.

The bindings on the web site are set to:

127.0.0.1 Acme.RealTimeService.com

And an entry in the C:\Windows\System32\drivers\etc\hosts file was added:

127.0.0.1   Acme.RealTimeService.com

This is the Startup.cs code in the server piece which is an AspNetCore project targeting .Net Core 3.1. The Microsoft.AspNetCore.SignalR v1.1.0 package was added to the project.

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

    app.UseStaticFiles();
    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapRazorPages(); //Routes for pages
        endpoints.MapControllers(); //Routes for my API controllers

        endpoints.MapHub<MessageHub>("/messages");

        endpoints.MapControllerRoute("default", "{controller=Announcement}/{action=Index}");
    });
}

Some things I've tried:

I've tried using the actual IP address of my development PC.

I've tried skipping negotiation and going straight to WebSockets:

hubConnection = new HubConnectionBuilder().WithUrl($"http://Acme.RealTimeService.com/messages", options =>
{
   options.SkipNegotiation = true;
   options.Transports = HttpTransportType.WebSockets;
}).Build();

I pasted the url into the browser:

Acme.RealTimeService.com/messages

and I got this response, which I believe shows that the hub is up:

Connection ID required

I added the Xam.Plugin.Connectivity package by James Montemangno to the client to check connectivity, and connect comes back as true. So I can get to IIS I'm assuming.

        if (CrossConnectivity.Current.IsConnected)
        {
            try
            {
                bool connect = await CrossConnectivity.Current.IsRemoteReachable("Acme.RealTimeService.com", 80, 5000);
            }
            catch (Exception ex)
            {
                return;
            }
        }

I tried every TLS setting in the Android project (Android Options | Advanced | SSL/TLS implementation). Changing that setting made no difference, and I tried them all.

I made sure the Android manifest included required permission "INTERNET"

I tried connecting the client to a plain old SignalR server (not AspNetCore) hosted as a Windows Service, and could not connect. That one threw a 400 Bad Request error on the await hubConnection.StartAsync(); call, which was why I stood up the AspNetCore SignalR server in the first place, thinking that both the mobile client and the server would need to be on AspNetCore. I would much prefer to keep the SignalR server hosted as a Windows Service if the Xamarin Forms client could connect to it.

Not sure what to try next because after scouring the web for answers, I'm plumb out of ideas. Except maybe wait for the next version of AspNetCore.SignalR to come out.

teeinep
  • 41
  • 1
  • The IIS bindings are actually: Type: http IP address: All Unassigned Port: 80 Host name: Acme.RealTimeService.com. I had a screenshot but since I'm a newb and not allowed to post pics, I had to remove it. – teeinep May 18 '20 at 15:58
  • I also tried HTTPS which resulted in a timeout exception on the StartAsync() call. – teeinep May 18 '20 at 17:16

0 Answers0