在ASP.NET Core SignalR中从TestServer初始化HubConnection

时间:2018-02-28 06:53:01

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

是否可以从HubConnection初始化Microsoft.AspNetCore.TestHost.TestServer

以下示例在HttpRequestException

处抛出await hubConnection.StartAsync();(未找到)异常
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.AspNetCore.TestHost;
using Xunit;

namespace FunctionalTests
{
    public class PubSubScenarios
    {
        [Fact]
        public async Task SignalRHubTest_Foo()
        {
            var webHostBuilder = WebHost.CreateDefaultBuilder().UseStartup<Startup>();

            using (var testServer = new TestServer(webHostBuilder))
            {
                var hubConnection = await StartConnectionAsync(testServer.BaseAddress);                
            }
        }

        private static async Task<HubConnection> StartConnectionAsync(Uri baseUri)
        {
            var hubConnection = new HubConnectionBuilder()
                .WithUrl($"http://{baseUri.Host}/fooHub")
                .WithConsoleLogger()
                .Build();

            await hubConnection.StartAsync();

            return hubConnection;
        }
    }
}

1 个答案:

答案 0 :(得分:3)

您需要调用testServer.CreateHandler()并将HttpMessageHandler传递给WithMessageHandler:

HtmlWebpackPlugin

这对于websockets不起作用(我在这里打开了一个问题https://github.com/aspnet/SignalR/issues/1595