C
C#16mo ago
demndev

Error during SignalR integration test: The 'InvokeCoreAsync' method cannot be called if the connecti

I get the exception System.InvalidOperationException: The 'InvokeCoreAsync' method cannot be called if the connection is not active after running this test:
public class HelpCommandTests
{
[Fact]
public async Task HelpCommand_Success()
{
const string command = "/help";
var response = string.Empty;
var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddSignalR();
})
.Configure(app =>
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ChatHub>("/chatHub");
});
});

var server = new TestServer(webHostBuilder);
var connection = new HubConnectionBuilder()
.WithUrl(
"http://localhost/chatHub",
o => o.HttpMessageHandlerFactory = _ => server.CreateHandler())
.Build();

await connection.StartAsync();

await connection.InvokeAsync("Command", command);

connection.On<string>("Receive", msg =>
{
response += msg;
});
}
}
public class HelpCommandTests
{
[Fact]
public async Task HelpCommand_Success()
{
const string command = "/help";
var response = string.Empty;
var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddSignalR();
})
.Configure(app =>
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ChatHub>("/chatHub");
});
});

var server = new TestServer(webHostBuilder);
var connection = new HubConnectionBuilder()
.WithUrl(
"http://localhost/chatHub",
o => o.HttpMessageHandlerFactory = _ => server.CreateHandler())
.Build();

await connection.StartAsync();

await connection.InvokeAsync("Command", command);

connection.On<string>("Receive", msg =>
{
response += msg;
});
}
}
BUT when I remove this peace of code:
typeof(ITerminalCommand).Assembly.GetTypesAssignableFrom<ITerminalCommand>().ForEach(s =>
{
services.AddScoped(typeof(ITerminalCommand), s);
});
typeof(ITerminalCommand).Assembly.GetTypesAssignableFrom<ITerminalCommand>().ForEach(s =>
{
services.AddScoped(typeof(ITerminalCommand), s);
});
all works fine.
2 Replies
demndev
demndev16mo ago
ChatHub.cs if needed: https://pastebin.com/7hpAB1Pv
Pastebin
public class ChatHub : Hub{ private readonly IEnumerable
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Accord
Accord16mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.