简体   繁体   English

没有为.NET Core Kestrel应用程序注册任何服务

[英]No service registered for .NET Core Kestrel application

I have a console application that I'm trying to get to run Kestrel. 我有一个尝试运行Kestrel的控制台应用程序。 The code files are all the boilerplate files from an empty ASP.Core application: 代码文件是来自空ASP.Core应用程序的所有样板文件:

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.Run(async (context) =>
        {
            await context.Response.WriteAsync("Hello World!");
        });
    }
}

However, when I run it I get the following exception: 但是,当我运行它时,出现以下异常:

Unhandled Exception: System.InvalidOperationException: No service for type 'Microsoft.AspNetCore.Hosting.IStartup' has been registered.

My proj file is: 我的项目文件是:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" version="2.0" />
  </ItemGroup>
</Project>

As discussed in the comments, I couldn't reproduce the reported issue: 如评论中所述,我无法重现所报告的问题:

I found single reference regarding the exception System.InvalidOperationException: No service for type 'Microsoft.AspNetCore.Hosting.IStartup' has been registered. 我发现有关异常System.InvalidOperationException: No service for type 'Microsoft.AspNetCore.Hosting.IStartup' has been registered.单一引用System.InvalidOperationException: No service for type 'Microsoft.AspNetCore.Hosting.IStartup' has been registered. you mentioned, below quoted from Reference : 您提到过,以下引用自Reference

its the ASP.NET Core configuration system that throws this exception stating that there is no IStartup class. 它的ASP.NET Core配置系统抛出此异常,表明没有IStartup类。

I'm not this familiar with what you are doing here, but it seems that you're doing your configuration in an incorrect order . 我不熟悉您在这里所做的事情,但是似乎您在以错误的顺序进行配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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