当测试运行时,我们会看到以下错误:
System.InvalidoperationException:端点/集线器/通知/协商包含授权元数据,但是没有发现中间件支持授权。 通过在应用程序启动代码中添加app.useauthorization()来配置应用程序启动。如果有呼叫app.userouting()和app.useendpoints(...),则必须在它们之间进行调用。
当我们等待连接startasync时,这会发生,但是,如果我们调试测试,一切都可以正常工作,并且测试通过。这是单位测试:
public async Task CanConnectClientHubToServerUsingLongPolling()
{
ConfigureServicesForTest((ctx, services) =>
{
services.AddNotification(builder =>
{
builder.Configure(options =>
{
options.Enabled = true;
});
builder.AddWebNotification(web =>
{
web.AddHubOptions(hub =>
{
});
});
});
});
Configure(app =>
{
app.UseNotificationServices();
});
INotifier notifier = null!;
IAuthenticationTokensAccessor accessor = null!;
FluentActions.Invoking(() =>
{
notifier = Services.GetRequiredService<INotifier>();
accessor = Services.GetRequiredService<IAuthenticationTokensAccessor>();
}).Should().NotThrow();
var conn = CreateConnectionUsingLongPolling(
Services.GetRequiredService<IOptionsMonitor<WebNotificationOptions>>().Get(Options.DefaultName),
async () => (await accessor.GetAuthenticationTokensAsync("bob")).EncodedUserJwt);
string expected = "this is a test";
string recieved = "";
conn.On<string>(HubMethods.RecieveSystemMessage, (msg) =>
{
Console.WriteLine($"System: {msg}");
});
conn.On<string>(HubMethods.RecieveMessage, (msg) =>
{
recieved = msg;
_delayForMessageSentTokenSource.Cancel();
});
await conn.StartAsync();
conn.ConnectionId.Should().NotBeNull();
conn.State.Should().Be(HubConnectionState.Connected);
await notifier.BroadCastAsync(expected);
DelayAsync(TimeSpan.FromSeconds(60));
recieved.Should().Be(expected);
await conn.StopAsync();
}
几天来,我一直在尝试解决此问题,但是它是一个头部刮擦者,我可以使用一些帮助。 webapplicationFactory使用的程序入口点是此处
using SubSonic.Configuration;
using SubSonic.Notification;
var builder = WebApplication.CreateBuilder(args);
if(!builder.Environment.IsTestIntegration())
{
var section = builder.Configuration.GetRequiredSection("Settings");
builder.Services.AddEnvironment(opt =>
{
opt.Environment = section.GetRequiredValue<string>("Environment");
});
builder.Services
.AddHttpContextAccessor()
.AddControllers();
builder.Services.AddNotification(notify =>
{
notify.Configure(options =>
{
options.Enabled = true;
});
notify.AddWebNotification(web =>
{
});
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
}
// program is primarily hosted in a test framework for integration testing of packages
var app = builder.Build();
if (!app.Environment.IsTestIntegration())
{
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseNotificationServices();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
}
app.Run();
没有调试的日志运行:dbug:microsoft.extensions.hosting.internal.host
1
托管开始dbug:subsonic.testing.webapitestfactory [0] 调用用户outingdbug:subsonic.testing.webapitestfactory [0] 端点路由中间件添加了falsedbug:subsonic.testing.webapitestfactory [0] 建筑商属性:[server.features,microsoft.aspnetcore.http.features.featurecollection]
dbug:subsonic.testing.webapitestfactory [0] 建筑商属性:[__ENDPOINTROUTEBUILDER,MICROSOFT.ASPNETCORE.ROUTING.DEFAULTENDENDPOINTROUTEBUILDER]
dbug:subsonic.testing.webapitestfactory [0] 建筑商属性:[__userouting,system.func`2 [microsoft.aspnetcore.builder.iapplicationbuilder,microsoft.aspnetcore.builder.iapplicationbuilder]]
烧伤:subsonic.notification.notification providerFactory [0] 映射到 /枢纽 /通知
的NotificationHub
dbug:microsoft.extensions.hosting.internal.host2
托管开始在此处运行的日志:
dbug:microsoft.extensions.hosting.internal.host
1 托管开始
dbug:subsonic.testing.webapitestfactory [0] 调用用户outingdbug:subsonic.testing.webapitestfactory [0] 端点路由中间件添加了truedbug:subsonic.testing.webapitestfactory [0] 建筑商属性:[server.features,microsoft.aspnetcore.http.features.featurecollection]
dbug:subsonic.testing.webapitestfactory [0] 建筑商属性:[__MiddleWaredScriptions,System.Collections.generic.list'1 [System.String]]
dbug:subsonic.testing.webapitestfactory [0] 建筑商属性:[__ENDPOINTROUTEBUILDER,MICROSOFT.ASPNETCORE.ROUTING.DEFAULTENDENDPOINTROUTEBUILDER]
dbug:subsonic.testing.webapitestfactory [0] 建筑商属性:[__userouting,system.func`2 [microsoft.aspnetcore.builder.iapplicationbuilder,microsoft.aspnetcore.builder.iapplicationbuilder]]
dbug:subsonic.testing.webapitestfactory [0] microsoft.aspnetcore.hostfiltering.hostfilteringmiddleware
dbug:subsonic.testing.webapitestfactory [0] microsoft.aspnetcore.httpspolicy.httpsredirectionmiddleware
dbug:subsonic.testing.webapitestfactory [0] Microsoft.aspnetcore.Routing.EndPoIntRoutingMiddleware
烧伤:subsonic.notification.notification providerFactory [0] 映射到 /枢纽 /通知
的NotificationHub
dbug:microsoft.extensions.hosting.internal.host2
托管开始
根据错误消息,您丢失了the the Middleware。
您可以像下面一样cahnge您的代码。