简体   繁体   中英

SignalR AzureFunction client to AspNetCore WebApi Hub connection

I have a webapi (aspnet core 2.2) app that calls an azure function which in turn starts up a signalR connection and stuffs a load of data down the socket then closes.

Until today that worked perfectly, apparently now something appears to have changed (presumably a signalR client/server package update) that's broken this with the following exception...

Executed 'Execute' (Failed, Id=e2e2a0f8-f56d-4fe1-a445-598b9338cb92) [06/11/2019 10:40:32] System.Private.CoreLib: Exception while executing function: Execute. Microsoft.AspNetCore.Http.Connections.Common: Invalid negotiation response received. Microsoft.AspNetCore.Http.Connections.Common: The type initializer for 'Microsoft.AspNetCore.Http.Connections.NegotiateProtocol' threw an exception. System.Text.Json: Method not found: 'Int32 System.Text.Encodings.Web.TextEncoder.FindFirstCharacterToEncodeUtf8(System.ReadOnlySpan`1)'.

The server code is as follows...

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddSignalR();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseSignalR(routeBuilder => {
        routeBuilder.MapHub<NotificationHub>("/Hubs/Notification");
        routeBuilder.MapHub<WorkflowHub>("/Hubs/Workflow");
    });
}

WorkflowHub.cs

public class WorkflowHub : Hub
{
    public async Task ConsoleSend(string level, string message, string thread)
    {
        await Clients.Group(thread).SendAsync("ConsoleReceive", level, message, thread);
    }
}

...

and now the client code (in the azure function)...

Function.cs

var con = new HubConnectionBuilder().WithUrl(msg.Api + "Hubs/Workflow").Build();
con.On<Exception>("error", (ex) => Console.WriteLine(ex.Message + "\n" + ex.StackTrace));
await con.StartAsync();

Package versions: On the server... - Microsoft.AspNetCore.Signalr 1.1.0

In the Azure function client... - Micorsoft.AspNetCore.Signalr.Client 3.0.0

...

I generally use Newtonsoft.Json for my Json needs but this new System.Text.Json does appear to be gaining traction and I'm not sure if this is some sort of versioning issue but adding the nuget reference to System.Text.Json on both ends makes no difference to this problem.

Update

So I upgraded the whole solution to.Net Core 3.0 this morning apart form the.Net Standard assemblies which are.Net Standard 2.x based.

By doing this I could reference the relevant protocol assemblies for either the new System.Text.Json api or the one for Newtonsoft.Json but in doing so I get faced more issues:

It still doesn't work, it results in the exception...

Executed 'Execute' (Failed, Id=e2265bbb-e59b-4b51-a0dd-d5798adccac1) [11/11/2019 12:50:46] System.Private.CoreLib: Exception while executing function: Execute. wf: Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified. System.Private.CoreLib: Could not load the specified file.

... but further to that a few of the calls I make in my WebApi application that hosts the hubs now can't be made, it seems that the OData framework doesn't like endpoint routing and appears to have open tickets on github that suggest this support is still in the works.

but either way... SignalR still isn't playing ball here.

Does anyone have a working example that has both a.Net Core and a web based client talking to a.Net Core hub all running under either.Net Core 2.x or.Net Core 3.x as I can't seem to find all of this working anywhere?

Looks like this was a caching issue with builds. Cleaning your build for a single build configuration seems to result "under some condition I can't pin down" the build picking up a version of the assembly from another build def.

Net result, I did a full manual clean of both my nuget cache and my bin folders throughout the solution and it all sprang to life.

Seems like a tooling issue to me not versioning / framework one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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