natemcmaster / DotNetCorePlugins

.NET Core library for dynamically loading code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to Publish DLLs with their Dependencies for use with this library?

pha3z opened this issue · comments

commented

So, this isn't a feature request for the library code itself, but for the documentation.

I love this library. It's amazing, but I've hit a snag.

When trying to create a DLL that has nuget dependencies, dotnet offers no obvious way to publish the DLL and aggregate all of its dependency DLLs. This makes it a pain-in-the-but to deploy hot-loadable DLLs for use with the plugin. I found an SO question mentioning the problem here:
https://stackoverflow.com/questions/51042647/publish-net-standard-library-with-all-its-dependencies

One of the commentor's says his solution:
"I have a very dirty way to do it, create a .net core application, reference the library and take the results except the exe, but it's totally ugly, Not sure why they did this without an option to "publish" the results."

The solution works. But it was not obvious to me, and I bet plenty of people have run into this. Maybe something in the basic usage documentation explaining a technique for building DLLs for use with this lib would be helpful. Even if it just said "Make a .net core app, reference the library, publish it."

commented

So, having fought with this for a bit, I came up with a more elegant solution. I tested this approach and deployed a full build with a plugin with many external dependencies to Ubuntu 18.04 and it ran perfectly.

Create a new project of type Console Application instead of Class Library. Put all your library code files into the Console Application and all all the dependencies. Get rid of the Class Library project (you don't need it anymore). Finally, publish the Console Application. You will get a DLL with all of the dependencies. You can use this DLL like any other DLL.

Elegant solution. I suggest naming the console app project with "Library" on the end of it and adding a README just to document its not really an application even though the project is configured to build as one.

I thought the README and samples covered this already....did you try using dotnet publish?

commented

The problem is that "dotnet publish" on a DLL does NOT publish any of the dependencies. The only thing it produces is a .nuget package. Its very unintuitive behavior. If you want to publish a DLL and get all of its dependencies, you actually have to turn the DLL into an application and publish the application -or- reference the DLL in an application and publish that. Its a very clunky dotnet design that cost me many hours of troubleshooting. The SO link I referenced talks about this.

Its clearly a dotnet issue -- not a problem with the DotNetCorePlugins library at all. It just seems likely that a lot of people are probably running into this snag.

Are you talking about dotnet pack? The pack command will produce a nuget package, not the publish command. You should be able to dotnet publish a class library and get its dependencies just fine:

root@1eac45b3a9fa:/mylib# dotnet new classlib
The template "Class Library" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on /mylib/mylib.csproj...
  Determining projects to restore...
  Restored /mylib/mylib.csproj (in 63 ms).
Restore succeeded.

root@1eac45b3a9fa:/mylib# dotnet add package Newtonsoft.Json
  Determining projects to restore...
  Writing /tmp/tmpxO19lq.tmp
info : Adding PackageReference for package 'Newtonsoft.Json' into project '/mylib/mylib.csproj'.
info :   CACHE https://api.nuget.org/v3/registration5-gz-semver2/newtonsoft.json/index.json
info : Restoring packages for /mylib/mylib.csproj...
info : Package 'Newtonsoft.Json' is compatible with all the specified frameworks in project '/mylib/mylib.csproj'.
info : PackageReference for package 'Newtonsoft.Json' version '13.0.1' added to file '/mylib/mylib.csproj'.
info : Committing restore...
info : Writing assets file to disk. Path: /mylib/obj/project.assets.json
log  : Restored /mylib/mylib.csproj (in 76 ms).
root@1eac45b3a9fa:/mylib# dotnet publish
Microsoft (R) Build Engine version 17.0.0+c9eb9dd64 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  All projects are up-to-date for restore.
  mylib -> /mylib/bin/Debug/net6.0/mylib.dll
  mylib -> /mylib/bin/Debug/net6.0/publish/
root@1eac45b3a9fa:/mylib# ls -la ./bin/Debug/net6.0/publish/
total 708
drwxr-xr-x 2 root root   4096 Nov 15 20:05 .
drwxr-xr-x 4 root root   4096 Nov 15 20:05 ..
-rwxr--r-- 1 root root 695336 Mar 17  2021 Newtonsoft.Json.dll
-rw-r--r-- 1 root root    987 Nov 15 20:05 mylib.deps.json
-rw-r--r-- 1 root root   3584 Nov 15 20:05 mylib.dll
-rw-r--r-- 1 root root  10124 Nov 15 20:05 mylib.pdb
root@1eac45b3a9fa:/mylib#

For plugins, I've always used <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> in the csproj file. I decided to use this instead of publish, as I can also use debug to launch the host application for debugging and testing.

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please comment if you believe this should remain open, otherwise it will be closed in 14 days. Thank you for your contributions to this project.

Closing due to inactivity.
If you are looking at this issue in the future and think it should be reopened, please make a commented here and mention natemcmaster so he sees the notification.