I want to use SignalR to create communication between 3 simple Windows Forms apps and a ASP.NET Web API server. I set up the server according to the Microsoft documentation at https://docs.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-5.0&tabs=visual-studio. I even followed the PluralSight course 'Getting Started With ASP.NET Core SignalR', but that seems to be a little bit outdated.
[HubName("MyNetworkHub")]
public class NetworkHub : Hub
{
private readonly ISomeService_someService;
public NetworkHub(ISomeService someService)
{
_someService = someService;
}
public Task SendMessage(string message)
{
return Clients.All.SendAsync("ReceiveMessage", message);
}
}
In Startup.cs
I added configuartion:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSignalR();
...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<NetworkHub>("/networkhub");
});
}
For my clients I want to use Windows Forms Applications, because I'm building a simple prototype on a complex technology. I created 1 Console App to try out connection, like so:
using Microsoft.AspNetCore.SignalR.Client;
using System;
using System.Threading.Tasks;
...
class Program
{
static async Task Main(string[] args)
{
HubConnection connection = null;
try
{
connection = new HubConnectionBuilder()
.WithUrl("http://127.0.0.1:52366/MyNetworkHub")
.Build();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
// The part below might be wrong, but I can't fix that if the connection doesn't work.
connection.On<string>("ReceiveMessage", (message) =>
{
Console.WriteLine(message);
});
await connection.StartAsync();
}
}
I've seen a few examples of people trying the same thing, but all of them ran into issues with connection. The issue I'm running into is that I cannot start a connection, because I'm getting:
Could not load file or assembly 'System.Text.Encodings.Web, Version=5.0.0.0, Culture=neutral, PublicKeyToken=xxxx'. The located assembly's manifest definition does not match the assembly reference. (0x80131040)
I tried swapping the .WithUrl("http://127.0.0.1:52366/MyNetworkHub")
to .WithUrl("http://127.0.0.1:52366/networkhub")
but that results in the same error.
I've solved the issue with adding the System.Text.Encodings.Web
package into the .csproj of the Client.
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="5.0.0" />
<PackageReference Include="System.Text.Encodings.Web" Version="5.0.0" />
</ItemGroup>
User contributions licensed under CC BY-SA 3.0