Reputation: 1281
I am working on a service which, given a URL to a Swagger definition, uses NSwag to generate a C# Proxy for the Service. The entire proxy is contained within a single C# file.
I would like to automate compilation of the generated code into a .NET Standard 2.0 DLL using Roslyn. This should be done in-memory, using the CSharpCompilation class.
To confirm that this is possible, I have created a .NET Standard 2.0 class library project in Visual Studio 2017 and added NuGet dependencies for Newtonsoft.Json (10.0.3) and System.ComponentModel.Annotations (4.4.0). I can see that the project type also brings in an SDK dependency on NETStandard.Library.
I understand that I will not use a project file to generate the DLL with Roslyn, however, I am struggling to figure out the equivalent steps to take to generate the same DLL.
Upvotes: 3
Views: 1650
Reputation: 1281
The key to building a .NET Standard DLL was selecting the correct DLLs to add as MetadataReference objects. It was not initially clear to me what these were because the Visual Studio 2017 .csproj format does not explicitly reference the them.
I was able to download the NuGet package for netstandard.library and loop through the contents, adding each DLL inside as a MetadataReference. I believe that this achieves the same goal as adding a .csproj SDK dependency on NetStandard.Library.
With the addition of these references, I was able to compile my generated code. I tested my output assembly by referencing it from a .NET Core 2.0 Console Application.
There may be an easier way to do this - I'd be happy to hear it!
Upvotes: 5