C# csproj 文件中的“服务包含”是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18614342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:44:46  来源:igfitidea点击:

What is "Service Include" in a csproj file for?

c#visual-studiovisual-studio-2013csproj

提问by joe

In a C# solution, I added a existing project.
After that, Visual Studio has added the following entry in other .csproj files:

在 C# 解决方案中,我添加了一个现有项目。
之后,Visual Studio 在其他 .csproj 文件中添加了以下条目:

<ItemGroup>
    <Service Include="{B4F97281-0DBD-4835-9ED8-7DFB966E87FF}" />
</ItemGroup>

What's this for?
Can I delete it?

这是用来干嘛的?
我可以删除它吗?

采纳答案by avandeursen

I had a similar case, where this was added:

我有一个类似的案例,添加了以下内容:

<ItemGroup>
  <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>

This inclusion turns out to be generated on purpose by VS2013 if you create an NUnit test project, but forget to tag it as test project, as described in this answerfrom Microsoft:

如果您创建 NUnit 测试项目,但忘记将其标记为测试项目,则此包含结果是 VS2013 有意生成的,如Microsoft 的此答案中所述:

This behavior is intentional.

To support third-party test frameworks, like NUnit and XUnit, Visual Studio 2012 loaded Test Explorer on solution open, regardless of whether it contained test projects. This added seconds of delay to startup and solution open scenarios for all users, majority of whom don't use tests.

In Visual Studio 2013, we changed it so that Test Explorer package is loaded only when the solution contains one or more test projects. Test projects are identified in two different ways. Projects created from one of the built-in unit test project templates are identified using project type GUIDs. Other types of projects, such as Class Library project with XUnit or NUnit tests, are identified by Test Explorer during first test discovery and “tagged” with the <Service/>item.

这种行为是故意的。

为了支持第三方测试框架,如 NUnit 和 XUnit,Visual Studio 2012 在解决方案打开时加载测试资源管理器,无论它是否包含测试项目。这为所有用户的启动和解决方案开放场景增加了几秒钟的延迟,其中大多数用户不使用测试。

在 Visual Studio 2013 中,我们对其进行了更改,以便仅当解决方案包含一个或多个测试项目时才加载测试资源管理器包。测试项目以两种不同的方式标识。从内置单元测试项目模板之一创建的项目使用项目类型 GUID 进行标识。其他类型的项目,例如带有 XUnit 或 NUnit 测试的类库项目,在第一次测试发现期间由测试资源管理器识别并用<Service/>项目“标记” 。

回答by Polyfun

The good thing about well-known / constantGUIDs is that they are pretty much unique and therefore very easy to search for in Google. Which I did, and found: thisand this, as well as other interesting hits.
It looks like this is actually a known bug in the T4 DSL tool which comes with the SDK. And fortunately it's easy enough to resolve by changing some registry keys.

众所周知的/恒定的GUID的好处是它们非常独特,因此很容易在 Google 中搜索。我做了,并发现: thisthis,以及其他有趣的点击。
看起来这实际上是 SDK 附带的 T4 DSL 工具中的一个已知错误。幸运的是,通过更改一些注册表项很容易解决。

回答by Vladimirs

Personally I don't like this service added to my project files and I think having it is more like a workaround rather than a proper solution. So marking your test projects as test projectsseems more correct to me and this can be achieved by adding this to the first PropertyGroup:

就我个人而言,我不喜欢将此服务添加到我的项目文件中,我认为拥有它更像是一种解决方法而不是正确的解决方案。因此,将您的测试项目标记为测试项目对我来说似乎更正确,这可以通过将其添加到第一个来实现PropertyGroup

<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TestProjectType>UnitTest</TestProjectType>

{3AC096D0-A1C2-E12C-1390-A8335801FDAB}means Test Project and {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}- C#. For other project type guids go here

{3AC096D0-A1C2-E12C-1390-A8335801FDAB}表示测试项目和{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}- C#。对于其他项目类型的 guids 去这里