Search code examples
.netbuild

.NET Publish looking for dependency in wrong location


Before anybody says anything, this is similar to Published output looking for wrong dependency but I'm getting an error sooner in the process than that one describes.

I have a solution with a project that has dependencies on other projects.
The main application is .net6.0 the other projects are standard2.0 built as AnyCPU.

Build works fine and the DLL projects output goes to bin\Debug\netstandard2.0 or bin\Release\netstandard2.0 depending on configuration.

But when I try to publish (currently publishing for Debug for testing), I get the following error:

----- Build started: Project: MyDll.dll, Configuration: Debug Any CPU -----
error CS0006: Metadata file 'D:\<redacted>\bin\x64\Debug\netstandard2.0\MyDll2.dll' could not be found

Note I've renamed all the DLLs for this post.

It's looking for the dependent DLL in bin\x64\Debug\netstandard2.0. There will never be an x64 folder because they're built as AnyCPU.

Here's the project file for the MyDll (the one getting the error)

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <LangVersion>11</LangVersion>
        <GeneratePackageOnBuild>False</GeneratePackageOnBuild>
        <Version>24.1.1</Version>
    </PropertyGroup>

    <ItemGroup>
      <ProjectReference Include="..\MyDll2\MyDll2.vbproj" />
      <ProjectReference Include="..\MyDll2\MyDll3.vbproj" />
    </ItemGroup>

</Project>

I've renamed the DLLs for this post,and shortened the list of ProjectReferences (there's actually about 7) but it only has problems with MyDll2.

Here's the project file for MyDll2. Note that this one is VB.NET, not C#. I suspected that might be the problem, but MyDll3 is also VB.NET and caused no issues. The rest of the solution is C#.

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <LangVersion>latest</LangVersion>
        <VBRuntime>Embed</VBRuntime>
        <GeneratePackageOnBuild>False</GeneratePackageOnBuild>
        <!--<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>-->
        <RootNamespace>MyDll2</RootNamespace>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
    </ItemGroup>

</Project>

I tried the <AppendTargetFrameworkToOutputPath> setting but that didn't help

Here's the pubxml

<Project>
  <PropertyGroup>
    <Configuration>Debug</Configuration>
    <Platform>x64</Platform>
    <PublishDir>bin\Debug\net6.0\publish\</PublishDir>
    <PublishProtocol>FileSystem</PublishProtocol>
    <_TargetId>Folder</_TargetId>
    <TargetFramework>net6.0</TargetFramework>
    <SelfContained>true</SelfContained>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PublishSingleFile>true</PublishSingleFile>
    <PublishReadyToRun>false</PublishReadyToRun>
    <PublishTrimmed>false</PublishTrimmed>
  </PropertyGroup>
</Project>

I've tried with PublishSingleFile set to false too.

The problem seems to be with how the build of MyDll during publish is looking for MyDll2.

An obvious (and unusable) solution would be to build all the DLLs as x64. But they're also used by another application which is x86 so they have to be AnyCPU.


Solution

  • I got it working!

    I don't remember how I got it working on the original solution, but I just had the same problem on another solution. I Googled and found this and thought "hey, this is the exactly the problem I'm having! Oh, that's because I wrote this 6 months ago".

    The thing that seemed to be causing the problem was that the solution had configurations for Any CPU, x64, and Mixed Platforms.

    They all seemed to be configured the same. No matter which one I chose, the DLL projects were set to build as Any CPU and the EXE projects as x64.

    I deleted two of the configurations leaving just the Any CPU and now publish works.