Asp.Net Core:程序不包含适用于入口点的静态“Main”方法

问题描述 投票:8回答:3

我正在尝试将基于Onion Architecture设计方法的Asp.Net WebApi项目移植到Asp.Net Core。但是,当我构建我的类库时,编译器正在寻找Program.cs中的静态Main方法,我得到:

C:\ Projects \ Some \ src \ Some.Core \ error CS5001:程序不包含适用于入口点的静态“Main”方法

我假设整个解决方案应该只有一个Program.cs /入口点,而且它位于我的WebApi项目中。我不对吗?否则,我该如何解决此错误?我错误地认为"emitEntryPoint": true服务于此目的。

这是我的类库的project.json的一个例子:

{
  "version": "1.0.0-*",
  "description": "Some.Core Class Library",
  "buildOptions": {
    "emitEntryPoint": true
  },
  "dependencies": {
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Authentication.JwtBearer": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Routing": "1.0.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.AspNetCore.Identity": "1.0.0",
    "Microsoft.Extensions.Configuration": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0"
  },

  "frameworks": {
    "netstandard1.6": {
      "dependencies": {
        "NETStandard.Library": "1.6.0"
      }
    }
  },
  "runtimes": {
    "win7-x64": {}
  }
}

建议表示赞赏。

c# asp.net-core
3个回答
7
投票

为了避免类库中的“程序不包含适用于入口点的静态'Main'方法”的错误,从buildOptions中删除emitEntryPoint-

  "buildOptions": {
    "emitEntryPoint": true
  },

emitEntryPoint告诉编译器是创建控制台应用程序还是库。有关更多信息,请参阅此post


1
投票

一个明显的解决方案是检查官方MSDN错误代码描述here

我有async修饰符,不得不转到C#语言版本7.1+


0
投票

我一直在使用.NET Core 2.0处理macOS上的这个错误,它似乎与我将我的解决方案存储在Google Drive文件夹中有关。这肯定不是因为我缺少项目中的主要方法,虽然我间歇地看到文件/文件夹从Rider中消失并重新添加它们(添加现有项目)正在解决问题。我在VS for Mac和VS Code中得到了相同的错误,但它们似乎没有更新文件夹内容以反映丢失的文件,因此更难确定问题。

tl; dr尝试将您的解决方案/项目移出已同步的网络驱动器!

© www.soinside.com 2019 - 2024. All rights reserved.