简体   繁体   中英

SingalR server build using .Net Core version talking to client build using .NET framework

I have set up a .NET Core SignalR server. It uses Microsoft.AspNetCore.SignalR and Microsoft.AspNetCore.SignalR.Core nuget packages. This application is configured in a startup class as follows.

public class Startup
{
    public IConfiguration Configuration;

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

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

    public void Configure(IApplicationBuilder app)
    {
        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHub<ChatHub>("/chatHub");
        });
    }
}

The ChatHub class exposes a SendThisMessage method as shown below.

public class ChatHub : Hub
{

    public ChatHub() { }

    // Overrides
    public override Task OnConnectedAsync()
    {
        Console.WriteLine($"OnConnectedAsync - {this.Context.ConnectionId}");
        return base.OnConnectedAsync();
    }

    public override Task OnDisconnectedAsync(Exception exp)
    {
        Console.WriteLine($"OnDisconnectedAsync - {this.Context.ConnectionId}");
        return base.OnDisconnectedAsync(exp);
    }

    public async Task SendThisMessage(string userName, string message)
    {
        Console.WriteLine("Hello SendThisMessage");
        await Clients.All.SendAsync("ReceiveMessage", userName, message);
    }
}

I created a simple .NET Core client like the following and its working as expected. Note that it uses Microsoft.AspNetCore.SignalR.Client nuget package

static void Main(string[] args)
{
    var connection = new HubConnectionBuilder()
        .WithUrl("http://localhost:5000/chatHub")
        .Build();


    connection.StartAsync().Wait();
    connection.InvokeCoreAsync("SendThisMessage", args: new[] { "hello", "world" });
    connection.On("ReceiveMessage", (string userName, string message) =>
    {
        Console.WriteLine(userName + " ; " + message);
    });
    Console.ReadKey();
} 

However, I also need to build a similar client for .NET Framework 4.6. This uses Microsoft.AspNet.SignalR.Client nuget package. For this, I started with the following code.

static void Main(string[] args)
{
    HubConnection connection = new HubConnection("http://localhost:5000/");
    IHubProxy proxy = connection.CreateHubProxy("chatHub");

    connection.Start().Wait();
    Console.ReadKey();

}

It causes the following exception.

Inner Exception 1: HttpClientException: StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Cache-Control: private Date: Wed, 02 Sep 2020 21:43:50 GMT Server: Microsoft-IIS/10.0 X-Powered-By: ASP.NET
Content-Length: 4935 Content-Type: text/html; charset=utf-8 }

Should it be possible for a .NET 4.6 Client to communicate with a SignalR server implemented using .NET Core?

The short answer is no, you cannot mix .NET 4.x and .NET Core. The .NET Core was a complete rewrite.

See my answer here regarding the same thing which has a little more detailed info.

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