dotnet / standard

This repo is building the .NET Standard
3.07k stars 429 forks source link

Build issues when referencing project that targets multiple frameworks C# WPF #1776

Closed AksTheNerd closed 3 years ago

AksTheNerd commented 3 years ago

I'm working on converting a large WPF solution's projects to SDK style and ran into some issues.

The solution is large with multiple projects(old style) all targeting .net452 and some nugets(some made by me) installed, targeting both .net452 and .netstandard2.1 This all seems to work fine, somehow the nuget manages to install the correct version targeting .net452.

I have decided to slowly convert the projects inside the solution to SDK style targeting .net452 and .netstandard2.1 just like the nugets.

This is where the problem starts, one project was converted so far that is referenced by multiple other projects. Localy everything seems to build and run fine, however when Azure DevOps(VS version 2019) is used to build the solution there are 2 scenarios:

  1. The famous error message is shown:

            "Error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 
             'netstandard, Version=2.1.0.0"
  2. It builds fine but on app launch it crashes with:

             "Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' 
             or one of its dependencies. The system cannot find the file specified."

I'm suspecting that the problem is related to referencing the SDK project, it looks like that somehow it references the .netstandard2.1 instead of the .net452.

The reason this is my suspicion is that if targetframework is set to only .net452 on the referenced project everything works fine. Google has led me to two possible solutions to tell the project what framework to use from the reference:

<ProjectReference Include="..\..\..\..\foo\foo.csproj"
                  AdditionalProperties="TargetFramework=net452">
      <Project>{xxxxxx-xxxxxx-xxxxx-xxxxx-xxxxxxx}</Project>
      <Name>foo</Name>
</ProjectReference>

and

<ProjectReference Include="..\..\..\..\foo\foo.csproj">
      <SetTargetFramework>TargetFramework=net452</SetTargetFramework>
      <Project>{xxxxxx-xxxxxx-xxxxx-xxxxx-xxxxxxx}</Project>
      <<Name>foo</Name>
</ProjectReference>

This doesn't look like it's doing anything and the problem still persists. At the moment I ran out of ideas and I'm here asking for help. Please let me know what I'm doing wrong or what else I can try.

AksTheNerd commented 3 years ago

I have found the problem. The issue was that I had set an output directory in AzureDevOps. When the builds for the projects were finished everything was copied there, first the .net4.5.2 target DLLs and then overwritten with the netstandard2.1 dlls. To avoid this issue in case it happens to anyone, let the compiler put the files into the root project by default and copy them afterwards wherever you want.

image