1

我有两个网络应用程序。从一个 Web 应用程序,我试图通过 SignalR 与另一个应用程序通信。我正在使用 InvokeAsync 通过 SignalR 发送数据并在客户端等待结果。在服务器上,随着数据的发送,我正在调用一个 web api,然后我想将结果作为 HttpResponseMessage 发送到正在等待的 SignalR 客户端。但是当尝试这样做时,我收到以下错误:

{System.IO.InvalidDataException: Connection terminated while reading a message.
   at Microsoft.AspNetCore.SignalR.Client.HubConnection.ReceiveLoop(ConnectionState connectionState)
   at Microsoft.AspNetCore.SignalR.Client.HubConnection.InvokeCoreAsyncCore(String methodName, Type returnType, Object[] args, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.SignalR.Client.HubConnection.InvokeCoreAsync(String methodName, Type returnType, Object[] args, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.SignalR.Client.HubConnectionExtensions.InvokeCoreAsync[TResult](HubConnection hubConnection, String methodName, Object[] args, CancellationToken cancellationToken)
   at SecretariatMedical.Services.WebService.LogInAndGetToken(LoginViewModel loginViewModel) in C:\Users\SylvainB\Desktop\aspnetcore\Projet Clinique Charles\SecretariatMedical\SecretariatMedical\Services\WebService.cs:line 82}

这是来自客户端的代码:

connection = new HubConnectionBuilder()
            .WithUrl("https://localhost:44379/ServeurWebServiceSignalRHub")
            .Build();

connection.Closed += async (error) =>
{
    await Task.Delay(new Random().Next(0, 5) * 1000);
    await connection.StartAsync();
};
            

await connection.StartAsync();
                    
HttpResponseMessage responseSignalR = await connection.InvokeAsync<HttpResponseMessage>("LogInAndGetToken", loginViewModel );

SignalR Hub 服务器上的代码:

public async Task<HttpResponseMessage> LogInAndGetToken(LoginViewModel loginViewModel)
        {
            try
            {
                string apiUrl = "api/authenticationapi/gettoken";
                HttpResponseMessage response;
                using (HttpClient client = new HttpClient())
                {
                    client.BaseAddress = new Uri("https://localhost:44379");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                    response = await client.PostAsJsonAsync(apiUrl, loginViewModel);
                    //ResultLoginViewModel resultLogin = await response.Content.ReadAsAsync<ResultLoginViewModel>();

                    return response;
                }
            }
            catch (Exception e)
            {
                HttpResponseMessage responseError = new     HttpResponseMessage(HttpStatusCode.InternalServerError);
                return responseError;
            }
           
        }

我的 StartUp.cs 中的代码:

services.AddSignalR().AddJsonProtocol(options => 
            {
                options.PayloadSerializerSettings.ContractResolver = new DefaultContractResolver();
            });

在 SignalR Hub 服务器上,当我尝试读取数据并将其作为对象 ViewModel 发送回时,SignalR 客户端正常接收它。

有人知道为什么传输 HttpResponseMessage 会出现问题吗?

非常感谢你的帮助。

4

0 回答 0