Reputation: 3999
I have external dll which I want to include to project. I cannot add this dll as standard library (it is c++ dll).
I am using DllImport for external dll.
[DllImport("MyExternal.dll")]
public static extern UInt32 Authent([Out] UInt32[] LibRandNum);
This is working if I include dll in main project:
"publishOptions": {
"include": [
"MyExternal.dll"
]
}
But it is not working in class libaray (same publish option in class library project.json file).
If I add dll to "C:\windows\system32" than it is working. But I do not want to assume that dll exist, I want to add dll from project library.
I found "How to use native dependencies", but still not working. I don't know how to do it with nuget package, but I would like to avoid nuget if possible.
Something like this, but in Core class library.
I have tried:
"copyToOutput": {
"include": [ "MyExternal.dll" ]
}
Still without success.
I created minimal solution (.NET core - visual studio 2015). You can get here : http://www.filedropper.com/myapp_1
There is dll file in class library (SGLW64.dll)
If you add this dll to C:\Windows\System32 than it is working.
Upvotes: 1
Views: 1058
Reputation: 1826
Unfortunately, copyToOutput
is not transitive in project.json
. See the GitHub Issue for more details. It's likely this will be fixed with the transition back to MSBuild, but for now we have to manage these dependencies manually.
Since we cannot transitively bring in copyToOutput
'ed resources from the ClassLibrary to MyApp, we must tell MyApp where to find the .dll
and where to put it. As we want to distribute the .dll
with the ClassLibrary, this means that MyApp will have to pick it up using a relative path. Here, we can take advantage of the mappings
object in copyToOutput
, which gives us a ton of control over where we get files from and where we put them. If you try to use include
instead of mappings
, you'll end up getting the whole folder structure your .dll
is in.
Add this to your buildOptions
and publishOptions
in MyApp's project.json
:
"copyToOutput": {
"mappings" : {
"SGLW64.dll": "../ClassLibrary/SGLW64.dll"
}
}
While it's not great that MyApp has to "know" about the internal implementation details of ClassLibrary by manually copying its .dll
, this is a compromise we need to make until we move back to MSBuild.
Upvotes: 2