'dotnet run' not copying files to output directory

0

I'm trying to test ability to have two dynamic link libraries for Windows and macOS in one app so it can choose required by DLLImport. I'm testing on Azure Pipeline which has following tasks:

enter image description here

So dll and dylib downloaded first from separate pipelines artifacts, then I create new console app with

dotnet new console -n ConsoleApp

then copy dll and dylib files:

Copy-Item test.dylib -Destination ConsoleApp
Copy-Item test.dll -Destination ConsoleApp

then set content for csproj:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <None Update="test.dll">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="test.dylib">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>

</Project>

and Program.cs:

using System;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Threading;

namespace ConsoleNetFramework
{
    class Program
    {
        [DllImport("test")]
        public static extern int Foo();

        static void Main(string[] args)
        {
            var result = Foo();
            Console.WriteLine($"Result = {result}");
        }
    }
}

And finally run the app with

dotnet run --project ConsoleApp/ConsoleApp.csproj

and get an error:

Unhandled exception. System.DllNotFoundException: Unable to load DLL 'test' or one of its dependencies: Access is denied. (0x80070005 (E_ACCESSDENIED))
   at ConsoleNetFramework.Program.Foo()
   at ConsoleNetFramework.Program.Main(String[] args) in D:\a\1\s\ConsoleApp\Program.cs:line 15

When the last task ran, we can see that dll and dylib are not copied to output directory:

----                -------------         ------ ----                                                                  
-a----        5/12/2021  12:28 PM           5120 ConsoleApp.dll                                                        


    Directory: D:\a\1\s\ConsoleApp\bin\Debug\net5.0


Mode                LastWriteTime         Length Name                                                                  
----                -------------         ------ ----                                                                  
-a----        5/12/2021  12:28 PM           4608 ConsoleApp.dll                                                        


    Directory: D:\a\1\s\ConsoleApp\obj\Debug\net5.0\ref


Mode                LastWriteTime         Length Name                                                                  
----                -------------         ------ ----                                                                  
-a----        5/12/2021  12:28 PM           5120 ConsoleApp.dll                                                        


    Directory: D:\a\1\s\ConsoleApp\obj\Debug\net5.0


Mode                LastWriteTime         Length Name                                                                  
----                -------------         ------ ----                                                                  
-a----        5/12/2021  12:28 PM           4608 ConsoleApp.dll                                                        


    Directory: D:\a\1\s\ConsoleApp


Mode                LastWriteTime         Length Name                                                                  
----                -------------         ------ ----                                                                  
d-----        5/12/2021  12:27 PM                test.dll                                                              
d-----        5/12/2021  12:27 PM                test.dylib

So I have no idea why files are not copied to output because I have

<CopyToOutputDirectory>Always</CopyToOutputDirectory>

in the csproj for those files.

On my local machine all these steps are working fine. What am I doing wrong?

c#
.net
.net-core
asked on Stack Overflow May 12, 2021 by Maxim

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0