Reputation: 3992
I am generating some dynamic C# and want to compile it to a .NET Core App. However, it seems that the deps.json file is missing to make it actually runnable.
So the compiling itself works, but on running dotnet [name of dll]
it gives an error:
In code I do
CSharpCompilation compilation = CSharpCompilation.Create(assemblyName,
syntaxTrees: files,
references: references,
options: new CSharpCompilationOptions(OutputKind.ConsoleApplication,
optimizationLevel: OptimizationLevel.Debug
)
);
FileUtility.CreateDirectory(outputDllPath);
EmitResult result = compilation.Emit(outputDllPath, pdbPath: outputPdbPath);
The references collection contains Microsoft.NETCore.App and the netstandard 2.0.0 ref dll, besides other specific dll's that are netstandard2.0 compliant.
This works without errors. On running I get:
Unhandled Exception: System.TypeLoadException: Could not load type 'System.Object' from assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
How do I produce the correct deps.json file for my compilation?
Upvotes: 5
Views: 2372
Reputation: 3992
We solved it by doing the following things:
Compile the C# files against the dll's from C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.0.0\ref\netcoreapp2.0 (don't add references to .NET 4. dll's!):
public static IEnumerable<PortableExecutableReference> CreateNetCoreReferences()
{
foreach(var dllFile in Directory.GetFiles(@"C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.0.0\ref\netcoreapp2.0", "*.dll"))
{
yield return MetadataReference.CreateFromFile(dllFile);
}
}
Create a CSharpCompilation with a ConsoleApp as output:
CSharpCompilation.Create(assemblyName,
syntaxTrees: files,
references: references,
options: new CSharpCompilationOptions(OutputKind.ConsoleApplication)
);
Now you only need to place runtimeconfig.json ([dllname].runtimeconfig.json) next to the output, with the following content:
{
"runtimeOptions": {
"tfm": "netcoreapp2.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "2.0.0"
}
}
}
The output can be run with dotnet.exe.
Upvotes: 3