-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Direct P/Invoke does not seem to work when "encapsulated" in a library #108009
Comments
Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas |
This looks like the same kind of problem one would have without native AOT in the picture and a project that has a native dependency. Adding just a reference to the project doesn't lead to the native dependency to be binplaced/packaged where it's needed. It leaks out to the consuming project that needs to know there's extra files. This can be fixed through e.g. packaging the referenced project into a NuGet package. The NuGet package can have msbuild targets/tasks to modify the build/publish of whoever references it. I think this should be solved through nuget as well - package the managed assembly into a NuGet, package the native static library, into a nuget, and add .targets/.props to the nuget package that do the right thing when building projects that reference the nuget. |
@MichalStrehovsky I see, thanks for the explanation. Now I am wondering, is there any advantage to setting
If I put my native library in |
If you set PublishAot on a library project, you can publish such library and you'll get a native library that can be consumed from non-.NET languages. If you don't intend to publish the library project, the presence of PublishAot=true in it only enables the trim/AOT/single-file analyzers. You can achieve the same thing by setting
I didn't realize NuGet now does this for DLLs. It could potentially do the same for static libraries, but that would have to be a request for NuGet. Absent a NuGet feature, it should be possible to do this by embedding a .props file in the NuGet package that adds the two items (DirectPInvoke and NativeLibrary) to any project that references the NuGet (I think you just need a .props file with the same name as the NuGet package, and place it in the |
Description
I have two C# projects in my solution - one library and one exe project. The library project exposes a native method using Direct P/Invoke and the exe project calls the method exposed in the library. I would like to "encapsulate" the
DirectPInvoke
andNativeLibrary
properties within the library project, but as far as I can tell, they must be configured on the exe project in order to work. Is this true? If so, can I use this issue to request that "encapsulation" of Direct P/Invoke in a library project be supported? If not, then can somebody help point me in the right direction to get this working?Reproduction Steps
First, verify that Direct P/Invoke works on the exe project:
Now move the following lines from
Exe/Exe.csproj
toLibrary/Library.csproj
:Rerun the
dotnet publish
command from above and see that it produces the following exception:Expected behavior
When moving the
DirectPInvoke
andNativeLibrary
properties from the exe project to the library project, I would expect them to be applied to the native methods exposed by the library. This is what I mean by "encapsulating" the Direct P/Invoke functionality within the library. Otherwise, the exe project needs to set these properties, which breaks the principle of encapsulation and tightly couples the exe project to the library project.Actual behavior
DirectPInvoke
andNativeLibrary
properties apparently need to be set on the exe project, not on the library project. Please correct me if this is wrong.Regression?
No response
Known Workarounds
No response
Configuration
Other information
No response
The text was updated successfully, but these errors were encountered: