带有 MessagePack 的 SignalR .Net 客户端 - IHubConnectionBuilder 中无法识别的 AddMessagePackProtocol 方法

SignalR .Net Client with MessagePack - AddMessagePackProtocol Method Unrecognised in IHubConnectionBuilder

新用户,正在学习 SignalR 和 Blazor Server,希望有人能帮助解决这个问题。努力让 SignalR .NET 客户端在 Blazor 服务器页面中使用 MessagePack 协议。

.csproj 包已安装

<ItemGroup>
    <PackageReference Include="Autofac" Version="5.2.0" />
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="6.0.0" />
    <!-- <PackageReference Include="MessagePack" Version="1.9.3" /> -->
    <PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="3.1.7" />
    <PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="3.1.7" />
</ItemGroup>

原来我安装了 3.1.8 的 SingalR Client 和 MessagePack 包。不过,我也试过降级到3.1.7,问题依旧。

这段代码:

    hubConnection = new HubConnectionBuilder()
                .WithUrl(hubUrl)
                .AddMessagePackProtocol()
                .Build();

导致构建错误:

error CS1061: 'IHubConnectionBuilder' does not contain a definition for 'AddMessagePackProtocol' and no accessible extension method 'AddMessagePackProtocol' accepting a first argument of type 'IHubConnectionBuilder' could be found (are you missing a using directive or an assembly reference?).....

有人可以帮忙吗?我是否缺少一个程序集 @using 参考?

Blazor 服务器页面

@page "/"

@using System.Threading;
@using System.Collections.Generic;
@using Microsoft.AspNetCore.SignalR.Client;
@using WebApp.Data;

@inject NavigationManager NavigationManager


<h1>Blazor Server App</h1>

<div>Latest message is => @_latestMessage</div>

<div id="scrollbox">
    @foreach (var item in _messages)
    {
        <div>
            <div>@item</div>
        </div>
    }
    <hr />
</div>


@code {
    private HubConnection hubConnection;
    private string _latestMessage = "";
    private List<string> _messages = new List<string>();
    public bool IsConnected => hubConnection.State == HubConnectionState.Connected;

    protected override async Task OnInitializedAsync()
    {
        var hubUrl = NavigationManager.BaseUri.TrimEnd('/') + "/motionhub";
        // Uri uri = NavigationManager.ToAbsoluteUri("/motionhub");

        try
        {
            hubConnection = new HubConnectionBuilder()
                .WithUrl(hubUrl)
                .AddMessagePackProtocol()
                .Build();

            hubConnection.On<string>("SendMotionDetection", ReceiveMessage);

            await hubConnection.StartAsync();

            Console.WriteLine("Index Razor Page initialised, listening on signalR hub url => " + hubUrl.ToString());
            Console.WriteLine("Hub Connected => " + IsConnected);
        }
        catch (Exception e)
        {
            Console.WriteLine("Encountered exception => " + e);
        }
    }

    private void ReceiveMessage(string message)
    {
        try
        {
            Console.WriteLine("Hey! I received a message");
            _latestMessage = message;
            _messages.Add(_latestMessage);

            StateHasChanged();
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine("An exception was encountered => " + ex.ToString());
        }
    }
}

最后,通过添加以下内容进行编译:

@using Microsoft.Extensions.DependencyInjection;

希望这能为遇到类似问题的其他新用户节省一些时间。

只是为了澄清@anon_dc3spp已经给出的正确答案,应该注意你:

  1. 需要在客户端和服务器上安装 Microsoft.AspNetCore.SignalR.Protocols.MessagePack Nuget 包。

  2. 然后您将在预期的剃须刀页面上使用下面显示的他的参考...或者将其添加到您的导入页面:

    @使用Microsoft.Extensions.DependencyInjection