I would like to know how to reference a local, unsigned CLR 4.0 assembly, when building a new Console application using the .NET Core tools and only targeting the .NET 4.5.1 framework.
I did the following on a Windows 10 machine:
- Create a new MyLibrary.cs with the following code:
using System;
namespace MyNamespace
{
public class MyLibrary
{
public static void SayHi()
{
Console.WriteLine("Hi");
}
}
}
Compile the above with
csc /t:library MyLibrary.cs
.Create a new folder and a .NET Core project.
mkdir MyConsoleApp
cd MyConsoleApp
dotnet new
- Edit the auto-generated project.json file to target .NET 4.5.1.
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {},
"frameworks": {
"net451" : { }
}
}
- Test that the project builds and runs. It does.
dotnet restore
dotnet build
bin\Debug\net451\MyConsoleApp.exe
- Edit Program.cs to call code in MyLibrary.dll.
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
MyNamespace.MyLibrary.SayHi();
}
And this is where I am stuck.
Where do I place my DLL and what do I put in the project.json so that I can call code in my assembly?
The legacy DLL must be wrapped in a nuget package. I have never created a nuget package before. Here is how I did it in this case:
1. Prepare to create a nuget package for the legacy DLL:
2. Fill in or delete fields in the nuspec file:
3. Pack the nuget package and move it into the nuget folder structure:
4. Create a new nuget-source folder:
5. In the project folder (e.g, MyConsoleApp), create a new nuget.config file with the following:
6. Add your local nuget-source folder to the list of nuget's sources:
In the project.json file, add a dependency to the legacy DLL.
7. At this point, you should be able to restore, build, and run the project.
HT: Thanks to @michael-kennedy for helping me work this out.