Search code examples
c#uwpwindows-10-iot-coreasp.net-core-signalrasp.net-core-2.1

SignalR Webhost with ASP.NET Core 2.1 and UWP app as Client returns ' 405 Method not allowed'


I am struggling with this for a while now and I can't get it to work. I have 2 applications, both running on a Raspberry Pi 2 with Win IoT:

  • ASP.NET Core 2.1 Web Api - Within this WebApi I am running a SignalR Host (published as win8-arm win10-arm).

  • Universal Windows App - This app will be a client which should communicate with the host and all other clients (published as ARM).

When I deploy the Web Api to the Raspberry, all is well. I have a Test Console Application acting as a client who can connect with the SignalR Host with no issues. When I deploy the UWP app and I try to run it, it crashes in the hubConn.Start(...) method of the HubConnection and returns the error:

HttpClientException: StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Server: Kestrel Date: Sat, 02 Jun 2018 17:04:15 GMT
Content-Length: 0 Content-Type: text/plain }

The testconsole and UWP are both using the Microsoft.AspNet.SignalR.Client (2.2.3) nuget package (which is the latest). The ASP.NET Core 2.1 Web Api is using the Microsoft.AspNetCore.App SDK (2.1.0).

By reading this post SignalR version compatability (StatusCode: 405 'Method Not Allowed') I am familiar with the fact that SignalR is not backwards compatible for both host as client. But I would expect that as I am using the latest nuget packages and versions which are available on nuget everything would work.

What I also do not get is why the Test Console is actually able to connect with no errors but the UWP app isn't even they are using the same SignalR Client nuget package.

Also, I am aware that ASP.NET Core 2.1 package and the SignalR Client package are 2 different packages, but I cannot add the nuget package Microsoft.AspNetCore.App to the UWP project as it is not compatible with any win10-arm platform.

Any idea how I can get the connection up and running?

Update the question June 4th, 2018

I removed my earlier assumptions in this question. I thought that the Test Console app was successfully connected but it turns out that isn't the case. The difference was that Test Console tried to connect synchronously but this resulted in a swallow of the error. I verified the state of the connection to the SignalR Host, which went from 'Connecting' straight to 'Disconnected'.

The question still remains: If I want to work with ASP.NET Core 2.1 SignalR, which nuget package should I include in my Test Console app, as well in my UWP project?

Here some of the code; all projects are completely defaults via File - New project, plus the code below.

ASP.NET Core 2.1 Web Api

Startup.cs

public class Startup
{
    ...

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        ...

        services.AddMvc();
        services.AddSignalR();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {

        ...

        app.UseSignalR(route =>
        {
            route.MapHub<HomeAutomationHost>("/HomeAutomationHost");
        });

        app.UseMvc();
    }
}

Universal Windows App

MainPage.xaml.cs

public class MainPage 
{
    ...

    public async Task DoConnect()
    {
            var hubConn = new HubConnection("http://192.168.2.49:5000/HomeAutomationHost", false);
            hubConn.EnsureReconnecting();

            var signalRHost = hubConn.CreateHubProxy("HomeAutomationHost");

            await hubConn.Start(new WebSocketTransport());
    }
}

Solution

  • I was actually using the wrong NuGet Package. I want Microsoft.AspNetCore.SignalR.Client (note AspNetCore in the name and not AspNet). It's definitely easy to get those mixed up though. The package I was using is the client for ASP.NET SignalR, the one I need to refer is the client for ASP.NET Core SignalR.

    Similar package names which might slip very easily in the project without noticing it.