AspNetCore / SignalR-从SignalR集线器服务器返回HttpResponseMessage会导致InvalidDataException

时间:2019-09-21 09:32:57

标签: signalr httpresponsemessage

我有两个Web应用程序。 我试图从一个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集线器服务器上的代码:

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集线器服务器上,当我尝试读取数据并将其作为对象ViewModel发送回时,SignalR Client会正常接收它。

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

非常感谢您的帮助。

0 个答案:

没有答案