【问题标题】:Is it possible to use a SignalR ASP.NET Server and SignalR WinForms App Client?是否可以使用 SignalR ASP.NET 服务器和 SignalR WinForms 应用程序客户端?
【发布时间】:2021-03-02 17:30:19
【问题描述】:

我想使用 SignalR 在 3 个简单的 Windows 窗体应用程序和 ASP.NET Web API 服务器之间创建通信。 我根据https://docs.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-5.0&tabs=visual-studio 的 Microsoft 文档设置了服务器。我什至学习了 PluralSight 课程“ASP.NET Core SignalR 入门”,但这似乎有点过时了。

[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);
        }
    }

Startup.cs我添加了配置:

public void ConfigureServices(IServiceCollection services)
  {
    ...
    services.AddSignalR();
    ...
  }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  {
    ...
    app.UseEndpoints(endpoints =>
      {
         endpoints.MapControllers();
         endpoints.MapHub<NetworkHub>("/networkhub");
      });
  }

对于我的客户,我想使用 Windows 窗体应用程序,因为我正在基于一项复杂的技术构建一个简单的原型。我创建了 1 个控制台应用程序来尝试连接,如下所示:

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();
        }
    }

我见过一些人尝试同样的事情的例子,但他们都遇到了连接问题。我遇到的问题是我无法启动连接,因为我得到:

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)

我尝试将.WithUrl("http://127.0.0.1:52366/MyNetworkHub") 换成.WithUrl("http://127.0.0.1:52366/networkhub"),但这会导致同样的错误。

【问题讨论】:

  • 你从哪里得到这个异常?发布 full 异常文本,而不仅仅是消息。您使用了哪个 NuGet 包?该错误抱怨缺少程序集,而不是 URL。也许你使用了错误的包?或者与另一个包有冲突?你的 csproj 是什么样子的?
  • 我想我找到了问题,在服务器和客户端中使用了错误的版本...看起来客户端现在可以连接,但我必须将客户端的版本从 5.0.0 降级到 1.0.0 ,好像有点奇怪……

标签: asp.net-core .net-core signalr signalr.client


【解决方案1】:

我已经解决了将System.Text.Encodings.Web 包添加到客户端的.csproj 中的问题。

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="5.0.0" />
    <PackageReference Include="System.Text.Encodings.Web" Version="5.0.0" />
  </ItemGroup>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 2021-10-03
    相关资源
    最近更新 更多