程序定义了多个入口点? CS0017 main() 有问题?
Posted
技术标签:
【中文标题】程序定义了多个入口点? CS0017 main() 有问题?【英文标题】:Program has more than one entry point defined? CS0017 Problem with main()? 【发布时间】:2019-07-31 15:59:22 【问题描述】:当我尝试在 Visual Studio 中运行以下代码时,我收到以下错误:“程序定义了多个入口点。使用 /main 编译以指定包含入口点的类型。”
我尝试过寻找其他入口点,但对 c# 没有那么丰富的经验并且遇到了麻烦。据我了解,一切都应该是正确的。
namespace Demos
using System;
using System.Drawing;
using Applitools;
using Applitools.Selenium;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
public class HelloWorld2
static string appName = "Hello World 2 App C#";
// change the value of testName so that it has a unique value on your Eyes system
static string testName = "Hello World 2 v1";
// if you have a dedicated Eyes server, set the value of the variable serverURLstr to your URL
static string serverURLstr = "https://eyesapi.applitools.com";
//set the value of runAsBatch to true so that the tests run as a single batch
static Boolean runAsBatch = true;
// set the value of changeTest to true to introduce changes that Eyes will detect as mismatches
static Boolean changeTest = true;
static string weburl = "https://applitools.com/helloworld2";
public static void Main(string[] args)
var eyes = new Eyes();
eyes.ServerUrl = serverURLstr;
setup(eyes);
var viewportSizeLandscape = new Size(/*width*/1024, /*height*/ 768);
var viewportSizePortrait = new Size(/*width*/500, /*height*/ 900);
IWebDriver innerDriver = new ChromeDriver();
if (!changeTest)
Test01(innerDriver, eyes, viewportSizeLandscape);
Test01Changed(innerDriver, eyes, viewportSizePortrait);
else
Test01Changed(innerDriver, eyes, viewportSizeLandscape);
Test01Changed(innerDriver, eyes, viewportSizePortrait);
innerDriver.Quit();
private static void Test01(IWebDriver innerDriver, Eyes eyes, Size viewportSize)
// Start the test and set the browser's viewport size
IWebDriver driver = eyes.Open(innerDriver, appName, testName, viewportSize);
try
driver.Url = weburl;
eyes.CheckWindow("Before enter name"); // Visual checkpoint 1
driver.FindElement(By.Id("name")).SendKeys("My Name"); //enter the name
eyes.CheckWindow("After enter name"); // Visual checkpoint 2
driver.FindElement(By.TagName("button")).Click(); // Click the button
eyes.CheckWindow("After Click"); // Visual checkpoint 3
Applitools.TestResults result = eyes.Close(false); //false means don't thow exception for failed tests
HandleResult(result);
catch (Exception ex)
Console.WriteLine(ex);
finally
eyes.AbortIfNotClosed();
private static void HandleResult(TestResults result)
string resultStr;
string url = result.Url;
if (result == null)
resultStr = "Test aborted";
url = "undefined";
else
url = result.Url;
int totalSteps = result.Steps;
if (result.IsNew)
resultStr = "New Baseline Created: " + totalSteps + " steps";
else if (result.IsPassed)
resultStr = "All steps passed: " + totalSteps + " steps";
else
resultStr = "Test Failed : " + totalSteps + " steps";
resultStr += " matches=" + result.Matches; /* matched the baseline */
resultStr += " missing=" + result.Missing; /* missing in the test*/
resultStr += " mismatches=" + result.Mismatches; /* did not match the baseline */
resultStr += "\n" + "results at " + url;
Console.WriteLine(resultStr);
private static void Test01Changed(IWebDriver innerDriver, Eyes eyes, Size viewportSize)
// Start the test and set the browser's viewport size
IWebDriver driver = eyes.Open(innerDriver, appName, testName, viewportSize);
try
string webUrlToUse = weburl;
if (changeTest)
webUrlToUse += "?diff2";
driver.Url = webUrlToUse;
if (!changeTest)
eyes.CheckWindow("Before enter name"); // Visual checkpoint 1
driver.FindElement(By.Id("name")).SendKeys("My Name"); //enter the name
eyes.CheckWindow("After enter name"); // Visual checkpoint 2
driver.FindElement(By.TagName("button")).Click(); // Click the button
eyes.CheckWindow("After click"); // Visual checkpoint 3
if (changeTest)
eyes.CheckWindow("After click again"); // Visual checkpoint 3
TestResults result = eyes.Close(false); //false means don't thow exception for failed tests
HandleResult(result);
catch (Exception ex)
Console.WriteLine(ex);
finally
eyes.AbortIfNotClosed();
private static void setup(Eyes eyes)
eyes.ApiKey = Environment.GetEnvironmentVariable("APPLITOOLS_API_KEY");
if (runAsBatch)
var batchInfo = new Applitools.BatchInfo("Hello World 2 Batch");
eyes.Batch = batchInfo;
//eliminate artifacts caused by a blinking cursor - on by default in latest SDK
eyes.IgnoreCaret = true;
【问题讨论】:
您的项目根目录中是否有一个Program.cs
类和另一个 public static void Main(string[] args)
方法? namespace Demos
也应该在使用之后和之前 public class HelloWorld2
您项目中的另一个类也包含 Main() 方法
有时候遇到这个错误,删除项目的/bin和/obj文件夹可以解决这个问题。
@Lemm - 实际上,using
语句可以在 namespace
声明的内部或外部,这样做可能有一些微妙的原因。见this link
@Lemm 不,我没有
【参考方案1】:
尝试将此添加到您的 .csproj
项目文件中:
<PropertyGroup>
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>
【讨论】:
好答案!谢谢 谢谢你,@Thijs 我不得不关闭 Visual Studio 并重新启动,然后执行了清理解决方案,然后重新构建解决方案以发现错误不再显示重新构建所有...成功。【参考方案2】:如果您添加对Microsoft.NET.Test.Sdk
的引用,就会发生这种情况。
所以,如果它只是控制台应用程序而不是测试项目,请删除对 Microsoft.NET.Test.Sdk
的引用。
【讨论】:
我这样做是因为我希望我的测试与代码在同一个项目中,有点蹩脚。如何选择要使用的入口点? @justin.m.chase - 如错误消息中所述,“使用 /main 编译以指定包含入口点的类型。”。因此,您似乎可以使用此开关来指定要使用的开关。由于我不再遇到此问题,因此我从未研究过开关。查看文档,这通常很有帮助。以上是关于程序定义了多个入口点? CS0017 main() 有问题?的主要内容,如果未能解决你的问题,请参考以下文章
Docker 在 VS 中运行,但在发布到 AWS 时出错?错误 CS5001:程序不包含适用于入口点的静态“Main”方法
如何在 VB.Net winforms 应用程序中找到 main() 入口点?
ld:入口点(_main)未定义。对于架构 x86_64:Xcode 9