5

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?

9
  • Have you tried creating the mscorlib reference like this: var mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location); Then try to use references like this: references: new[] { mscorlib } ? See: joshvarty.wordpress.com/2016/01/16/… Commented Sep 22, 2017 at 14:12
  • @SlapY I reference netstandard and netcore dll's, not mscorlib. And compiling works. The problem is in running the dll. Commented Sep 22, 2017 at 14:24
  • I'm not fammiliar with CSharpCompilation.Create, does it in fact compile or does the compilation/evaluation happen on Emit? Commented Sep 22, 2017 at 14:57
  • I think you need all the dependent assemblies copied into the same directory as your emitted library. Commented Sep 22, 2017 at 17:35
  • 1
    @MichielOvereem when you build with visual studio you have a build system (msbuild) that figures out the locations of all the dependent assemblies and gives those file paths to the compiler. When you use the Roslyn Compilation API you have to do that work yourself. The same would be true if you tried to compile your app on the command line by calling csc.exe directly. Commented Sep 27, 2017 at 22:26

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Any idea how to do this with a Class Library output type? I am trying to build a deploy file for AWS lambda. I used the code snippets to get the dependencies, but I can't build as a ConsoleApplication.
Hi @TimothyGonzalez how do you mean? If you change the OutputKind to classlibrary it will work. You just have to find the right set of dependencies (either a NetStandard.dll or the mscorlib for full framework).
I can build the project with OutputKind.DynamicallyLinkedLibrary, but there is no deps.json file. What packages are you using? I'm building for .netcore 1.0 from a .net 4.7 runtime to build. I even tried to build as a console application and there is still no deps.json. I ended up just resorting to calling the dotnet build command programmatically.
@TimothyGonzalez in this specific case we didn't need the deps.json. Through trial and error it seems that in some cases (if all dll's are in the same folder?) the deps json is not needed.
When Using the output of the vs2017 it shows the csc command with all the references. With these reference it will compile
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.