dotnet / core

.NET news, announcements, release notes, and more!
https://dot.net
MIT License
20.96k stars 4.91k forks source link

Is it possible to call c++ cli class library with core 3.1 in windows nano container? #3927

Closed yunfandev closed 4 years ago

yunfandev commented 4 years ago

Issue Title

I write a demo console project with .NET Core 3.1.0 Preview 3 it works well on my computer, but when I try to run the project in windows docker it failed with the message:

Unhandled exception. System.BadImageFormatException: Could not load file or assembly 'ClassLibrary1, Version=1.0.7270.15360, Culture=neutral, PublicKeyToken=null'. An attempt was made to load a program with an incorrect format.

Then I copy all project files into docker then try to use dotnet run to start project, but it failed again with the follow message:

C:\Program Files\dotnet\sdk\3.1.100-preview3-014645\Microsoft.Common.CurrentVersion.targets(2079,5): warning MSB3246: Resolved file has a bad image, no metadata, or is otherwise inaccessible. Assembly file 'C:\Users\ContainerUser\.nuget\ packages\classlibrary1\1.0.0\lib\netcoreapp3.1\Ijwhost.dll' could not be opened -- PE image doesn't contain managed metadata. [C:\Users\ContainerUser\code\ConsoleApp.Core\ConsoleApp.Core.csproj] C:\Program Files\dotnet\sdk\3.1.100-preview3-014645\Microsoft.Common.CurrentVersion.targets(2079,5): warning MSB3246: Resolved file has a bad image, no metadata, or is otherwise inaccessible. Assembly file 'C:\Users\ContainerUser\.nuget\ packages\classlibrary1\1.0.0\lib\netcoreapp3.1\Ijwhost.dll' could not be opened -- PE image doesn't contain managed metadata. [C:\Users\ContainerUser\code\ConsoleApp\ConsoleApp.csproj] Unhandled exception. System.BadImageFormatException: Could not load file or assembly 'ClassLibrary1, Version=1.0.7271.12479, Culture=neutral, PublicKeyToken=null'. An attempt was made to load a program with an incorrect format. File name: 'ClassLibrary1, Version=1.0.7271.12479, Culture=neutral, PublicKeyToken=null' at ConsoleApp.Core.NativeUtil.Invoke() at ConsoleApp.Program.Main(String[] args) in C:\Users\ContainerUser\code\ConsoleApp\Program.cs:line 10

The classlibrary1 is a c++ cli class library, I pack this library into a nuget package, then add it into ConsoleApp. 😂😂😂

So, is it possible to call c++ cli class library with core 3.1 on nano server?

yunfandev commented 4 years ago

I also try to run the project in Windows Sandbox, but it failed with the same message. It seems like the project only works with the computer which has dotnet framework installed.

yunfandev commented 4 years ago

I found the project even can't run in dotnet framework container. 😂😂😂😂😂

But it works very well on my computer and windows server 2019.

Is this a bug of dotnet core 3.1 preview or windows container?

brendansensus commented 4 years ago

I am having the same issue. My ASP.NET Core 3.1 application can't load a C++/CLI library inside a Windows Nanoserver or a Windows Server Core container. However, it loads fine on my development machine. Did you find a solution to this problem?

yunfandev commented 4 years ago

@brendansensus Finally, we found our project can run in dotnet framework SDK container, docker pull mcr.microsoft.com/dotnet/framework/sdk

The program works fine, but the image is too large 😂😂😂

brendansensus commented 4 years ago

@yunfandev Thanks so much for your reply. I changed the base image for my container to the dotnet framework SDK image like you did, installed .NET Core 3.1 onto that, and then ran my application. My application was then able to run correctly and load the C++/CLI library. This seems like a bug in .NET Core 3.1, in that you shouldn't have to install the .NET Framework SDK into the container to get the .NET Core application to run. I think we should probably leave this issue open so someone can look into it and fix it. Would you agree, @yunfandev?

One thing to note is that my C++/CLI library is very old and the source code was purchased from a third party. I ported it from .NET Framework to .NET Core using the instructions here: https://docs.microsoft.com/en-us/dotnet/core/porting/cpp-cli

However, for some reason, like you found, I still need to install the .NET Framework SDK. I think .NET Core should be updated so that that isn't necessary.

yunfandev commented 4 years ago

@brendansensus I couldn't agree more

yunfandev commented 4 years ago

@brendansensus I found the documents about nano server

https://docs.microsoft.com/en-us/windows-server/get-started/developing-powershell-cmdlets-for-nano-server#building-managed-c-cppcli-for-nano-server

Building managed C++ (CPP/CLI) for Nano Server Managed C++ is not supported for CoreCLR. When porting to CoreCLR, rewrite managed C++ code in C# and make all native calls through PInvoke.

It means that it is impossible to call c++ cli class library with core 3.1 in windows nano container.😂😂😂

emanuel-v-r commented 4 years ago

Hi guys this issue may be helpful for you https://github.com/dotnet/runtime/issues/40131, specially this comment https://github.com/dotnet/runtime/issues/40131#issuecomment-670303894

yunfandev commented 4 years ago

@imaramos Did you successfully call C++/CLI dll in the nano server?

emanuel-v-r commented 4 years ago

@imaramos Did you successfully call C++/CLI dll in the nano server?

I was able to run it on windows server core first. Then I tried with with nano server and got the error related with vc_redist, after that I tried the fix mentioned in that issue and it solved the c++/cli dll error, unfortunately I got an error with another dll, but AFAIK it fixed the problem with c ++ / cli dll.

yunfandev commented 4 years ago

@imaramos I create a demo app, I tried to run this demo app in docker nano server according to the method you mentioned in another issue, but it failed. 😢😢😢

I want you to test whether you can successfully run this demo demo app and if you can, is it convenient to provide your Dockerfile

emanuel-v-r commented 4 years ago

@imaramos I create a demo app, I tried to run this demo app in docker nano server according to the method you mentioned in another issue, but it failed. 😢😢😢

I want you to test whether you can successfully run this demo demo app and if you can, is it convenient to provide your Dockerfile

Created a fork with a working Dockerfile https://github.com/imaramos/InteropApp/blob/master/Dockerfile . The first stage is crazy slow (and heavy) so I advise you to push it to some private registry and re-use it, taking advantage of docker cache.

yunfandev commented 4 years ago

@imaramos Thank you for your Dockerfile the demo app works well in nano server.

But I also encountered the same problem as you, I got an error with another DLL. 😂😂😂

emanuel-v-r commented 4 years ago

@imaramos Thank you for your Dockerfile the demo app works well in nano server.

But I also encountered the same problem as you, I got an error with another DLL. 😂😂😂

The error that I get doesn't happen in windows server core image, so you can try that one, that is still better than using the whole sdk, but then you need to install the runtime and vc_redist manually.

yunfandev commented 4 years ago

@brendansensus

Thanks, I have switched to the server core image, and the image size before compressed has been reduced from 10GB to 5GB.