命名空间“System.Windows”中不存在类型或命名空间名称“Forms”您是否缺少程序集引用?

问题描述

我正在使用 Visual Studio 2019 并且有一个控制台应用程序运行一些带有目标框架 .NET core 3.1 的 selenium 测试。我最近添加了使用 Windows 表单在我的应用程序中上传文件代码

System.Windows.Forms.SendKeys.SendWait(absolutePath);
System.Windows.Forms.SendKeys.SendWait("{Enter}");

当我构建它时,出现以下错误The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)

所以我用谷歌搜索并更新了我的项目文件以在 PropertyGroup 中添加以下两个标签

<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>

万岁,它在本地不再抱怨了。但是,当我在 Teamcity 中构建应用程序时,我遇到了同样的错误。有谁知道为什么会发生这种情况。我搜索了更多,每个人都在谈论添加程序集引用。但我没有在 Visual Studio添加引用的选项。

缺少AssemblyReferences

MissingAssemblyReferences

我尝试将其添加为 COM 依赖项,但也没有用。

但是,我确实看到在 Dependencies > Frameworks> System.Windows.Forms 下可用。

框架

Frameworks

谁能帮我理解为什么它仍然在抱怨。

同样,它在 Visual Studio 和命令行中本地运行良好,仅在 Teamcity 中是一个问题。在 Teamcity 中运行的命令如下:

"C:\Program Files\dotnet\dotnet.exe" build <myTestApp>.sln @C:\BuildAgent\temp\agentTmp\a84ed99789454a3fa1f0d0028fcff404.rsp

提前致谢!

解决方法

我终于从这篇帖子 .Net core 3: manually adding framework dependencies 的答案中弄明白了。

我必须在项目文件中包含对 Microsoft.AspNetCore.App 的框架引用,如下所示:

<ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>