[英]Project reference not resolved when running dotnet build
我有一个 Visual Studio 2022 解决方案,有多个项目,但这里有四个特别有趣。
Provider1
基于 .NET Framework 4。Provider2
基于 .NET 6.Provider1Test
。Provider2Test
基于 .NET 6. Provider1
项目有许多类,都在Provider.Data
命名空间中,其中一个是Class1
。 这是我的源代码。 Provider1.csproj
文件:
<ItemGroup>
<Compile Include="Class1.cs">
<SubType>Code</SubType>
</Compile>
...
</ItemGroup>
Class1.cs
文件:
namespace Provider.Data
{
public class Class1
{
...
}
}
Provider2
项目有指向这些源文件的链接,即“添加”->“现有项目”->“作为链接”。 它使用不同的条件编译符号进行编译,因此 output 与Provider1
项目不同。 Provider2.csproj
文件:
<ItemGroup>
<Compile Include="..\Provider1\Class1.cs" Link="Class1.cs" />
</ItemGroup>
Provider1Test
项目是一个 NUnit 测试项目,用于测试Provider1
。 它有多个测试类,其中之一是TestClass1
。
Provider2Test
项目也是一个 NUnit 测试项目,具有对Provider2
的 ProjectReference。 它以与源代码相同的方式链接到Provider1Test
中的测试类。 Provider2Test.csproj
文件:
<ItemGroup>
<ProjectReference Include="..\Provider2\Provider2.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\Provider1Test\TestClass1.cs" Link="TestClass1.cs" />
</ItemGroup>
TestClass1.cs
文件:
using Provider.Data;
namespace ProviderTests
{
public class TestClass1
{
...
}
}
现在,它在 Visual Studio 中构建和运行得很好,但是如果我导航到Provider2Test
文件夹并尝试使用do.net build
命令进行构建,它找不到源代码。
C:\dev\DataProvider\Provider2Test>dotnet build
MSBuild version 17.3.1+2badb37d1 for .NET
Determining projects to restore...
All projects are up-to-date for restore.
Provider2 -> C:\dev\DataProvider\Provider2\bin\x64\Debug\net6.0\Provider.Data.dll
1 file(s) copied.
C:\dev\DataProvider\Provider1Test\TestClass1.cs(14,7): error CS0246: The type or namespace name 'Provider' could not be found (are you missing a using directive or an assembly reference?) [C:\dev\DataProvider\Provider2Test\Provider2Test.csproj]
Build FAILED.
这里有什么问题,为什么do.net build
不按照这里的参考路径,怎么解决?
我试图直接在Provider2Test
中创建一个TestClass2.cs
文件,这不是一个链接而是一个标准的编译包含,并且还使用了Provider.Data
命名空间。 它产生相同的错误。
我找到了一个解决方法,所以我将它发布在这里并暂时使用它,但我认为这不是一个好的解决方案,并且它没有解释原始问题,所以我不打算将此标记为已接受的答案。
在Provider2.csproj
中,我添加了如果它是使用do.net build
的,它有一个构建后事件,将其源代码 dll 复制到Provider2Test
。 如果项目是在 Visual Studio 中生成的( "$(MSBuildRuntimeType)" == "Full"
),则不会运行。
if "$(MSBuildRuntimeType)" == "Core" XCOPY "$(OutDir)Provider.Data.dll" "$(ProjectDir)..\Provider2Test\$(OutDir)" /Y /F
在Provider2Test.csproj
中,我添加了条件程序集引用。
<ItemGroup>
<Reference Include="Provider.Data" Condition="$(MSBuildRuntimeType) == 'Core'">
<HintPath>$(OutDir)Provider.Data.dll</HintPath>
</Reference>
</ItemGroup>
我在所有情况下( "Full"
和"Core"
)都保留了ProjectReference
,以便在构建Provider2Test
时触发Provider2
构建。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.