为纳米雌EStrel类的构造函数添加throws的构造函
ErrorSystem.ArgumentNullexception:值不能为null。 (范围 “提供者”) 在system.throwhelper.throw(字符串paramname)上 在 Microsoft.extensions.dipendentiondoction.ServiceProviderServiceExtensions.getRequiredService [t](iserviceProvider 提供商) 在 microsoft.aspnetcore.hosting.listenoptionshttpsextensions.usehttps(listerOptions 倾听,httpsconnectionadapteroptions httpsoptions) 在 nanokestrel。<>c__displayClass16_0.b__2(侦听 听力)<.ctor>
public class NanoKestrel
{
public IServer Server { get; set; }
private HostingApplication _application { get; set; }
private X509Certificate2 _serverCertificate { get; set; }
public NanoKestrel(IPEndPoint endPoint, X509Certificate2 certificate = null)
{
_serverCertificate = certificate;
var services = new ServiceCollection();
services.AddLogging(b =>
{
b.AddFilter("Microsoft.AspNetCore.Server.Kestrel", LogLevel.Warning);
});
services.Configure<KestrelServerOptions>(options =>
{
options.Listen(endPoint, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http1AndHttp2AndHttp3;
if (_serverCertificate != null) listenOptions.UseHttps(new HttpsConnectionAdapterOptions() { ServerCertificate = _serverCertificate }); // <--exception thrown at this line in stacktrace
});
});
services.AddSingleton<IServer, KestrelServer>();
services.AddSingleton<IConnectionListenerFactory, SocketTransportFactory>();
services.AddSingleton<IHostApplicationLifetime, ApplicationLifetime>();
services.AddTransient<IHttpContextFactory, DefaultHttpContextFactory>();
services.AddTransient<HostingApplication>();
var serviceProvider = services.BuildServiceProvider();
Server = serviceProvider.GetRequiredService<IServer>();
_application = serviceProvider.GetRequiredService<HostingApplication>();
}
public async void StartAsync()
{
await Server.StartAsync(_application, default).ConfigureAwait(false);
}
}
我试图向
listenOptions.UseHttps();
提供证书,但抛出了同样的例外。
I打算在Kestrel上启用Quic/http3,遵循Https://www.meziantou.net/ususe-http-3-quic-in-dotnet.htm
的指导。 包装参考:
Microsoft.extensions.features7.0.2
frameworkReference:include =“ microsoft.aspnetcore.app”
我通过添加以下标记的行来解决问题:
i正在尝试类似的事情,以在构建服务提供商之前创建服务集合,添加服务和配置来设置Kestrel服务器。当我尝试获得主机时,它会实例化
在Github上进行一些挖掘后,我看到了Microsoft的设置。他们有一个扩展的
KestrelServerOptions
类,该类将当前服务提供商分配给
ApplicationServices
属性。
我要做的就是这样的事情:
KestrelServerOptionsSetup
,但您可能会注意到IConfigureOptions<KestrelServerOptions>
课是内部的,所以我使用反思来帮助我。在我的项目中,我创建了自己的应用程序类:
ApplicationServices
以及配置服务集合的扩展类。
这是我的整个扩展课(截至今天):
services.AddTransient<IConfigureOptions<KestrelServerOptions>, Microsoft.AspNetCore.Server.Kestrel.Core.Internal.KestrelServerOptionsSetup>();