简体   繁体   中英

System.InvalidOperationException: 'No service for type 'Microsoft.AspNetCore.Hosting.Server.IServer' has been registered.'

Was using Microsoft.AspNetCore.Connections.Abstractions v2.2.0. This nuget package is a transitive dependency for Microsoft.AspNetCore.Server.Kestrel. Upgraded Using Microsoft.AspNetCore.Hosting v2.2.7. There is no UseKestrel showing up in intellisense. So, commented it out. At run time see the exception - System.InvalidOperationException: 'No service for type 'Microsoft.AspNetCore.Hosting.Server.IServer' has been registered.' at host.RunAsync(cancellationToken.Token).GetAwaiter().GetResult();

IWebHost host;

host = new WebHostBuilder()
    .UseUrls($"http://{Environment.MachineName}:5000")
    .ConfigureServices(services =>
    {
        services.AddSingleton<IStartupFilter, ApiVersioningStartupFilter>();
        services.AddSingleton(typeof(SdsContext), context);
    })
     .UseStartup<Startup>()
     .Build();
host.RunAsync(cancellationToken.Token).GetAwaiter().GetResult();

You could replace your custom WebHostBuilder setup with WebHost.CreateDefaultBuilder or Host.CreateDefaultBuilder .

But there should also be an extension method for UseKestrel in ASP.NET Core 2.2, see https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.hosting.webhostbuilderkestrelextensions.usekestrel?view=aspnetcore-2.2

You might not have a using for the namespace those extension methods are in, which is Microsoft.AspNetCore.Hosting .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM