Build error: You must add a reference to System.Runtime
Adding a reference to this System.Runtime.dll assembly fixed the issue:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\Facades\System.Runtime.dll
Though that file in that explicit path doesn't exist on the build server.
I will post back with more information once I've found some documentation on PCL and these Facades.
Update
Yeah pretty much nothing on facade assemblies on the whole internet.
Google:
(Facades OR Facade) Portable Library site:microsoft.com
ASP.NET: .NET Framework/Standard/Core DLL collisions, caused by Nuget. You must add a reference to assembly System.Runtime...
Although I have seen System.Runtime missing in many places, at this current moment, it was blowing up when loading a view.
Little did I know, apparently the view engine doesn't necessarily use the same assemblies as the main project! See my answer in another post for how to add assemblies to the view engine: https://stackoverflow.com/a/57350759/2589506
Receiving You must add a reference to assembly 'netstandard' Errors during Runtime Compilation
The solution was to add a reference to the netstandard.dll:
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
namespace Compiler
{
public static class Compiler
{
public static Assembly CompileAssembly(string code, bool compileAsExecutable = false)
{
var outputKind = compileAsExecutable ? OutputKind.ConsoleApplication : OutputKind.DynamicallyLinkedLibrary;
var syntaxTree = CSharpSyntaxTree.ParseText(code);
var syntaxTrees = new[] {syntaxTree};
// Requested the reference be fetched
var runtimeSpecificReference = GetRuntimeSpecificReference();
var systemAssemblyReference = GetMetadataReference<object>();
var patchBandAssemblyReference = GetMetadataReference<AssemblyTargetedPatchBandAttribute>();
// Added the reference to the value here
var references = new[] {runtimeSpecificReference, systemAssemblyReference, patchBandAssemblyReference};
var cSharpCompilationOptions = new CSharpCompilationOptions(outputKind);
var compilation = CSharpCompilation.Create(@"Countdown", syntaxTrees, references, cSharpCompilationOptions);
using (var assemblyStream = new MemoryStream())
{
var emitResult = compilation.Emit(assemblyStream);
if (emitResult.Success)
{
var assemblyBytes = assemblyStream.ToArray();
return Assembly.Load(assemblyBytes);
}
var errors = emitResult
.Diagnostics
.Select(diagnostic => diagnostic.GetMessage())
.Select(message => new Exception(message));
throw new AggregateException(errors);
}
}
private static string GetAssemblyLocation<T>()
{
var typeOfT = typeof(T);
return typeOfT.Assembly.Location;
}
private static PortableExecutableReference GetMetadataReference<T>()
{
var assemblyLocation = GetAssemblyLocation<T>();
return MetadataReference.CreateFromFile(assemblyLocation);
}
// This function was needed
private static PortableExecutableReference GetRuntimeSpecificReference()
{
var assemblyLocation = GetAssemblyLocation<object>();
var runtimeDirectory = Path.GetDirectoryName(assemblyLocation);
var libraryPath = Path.Join(runtimeDirectory, @"netstandard.dll");
return MetadataReference.CreateFromFile(libraryPath);
}
}
}
Related Topics
What Are Some Reasons Networkstream.Read Would Hang/Block
What Is the Fastest Way to Create a Checksum for Large Files in C#
Build Error: You Must Add a Reference to System.Runtime
Why/When Would It Be Appropriate to Override Tostring
Dependency Injection Using Azure Webjobs Sdk
How to Detect When a Windows Form Is Being Minimized
Print Existing PDF (Or Other Files) in C#
Why Isn't My Public Property Serialized by the Xmlserializer
How to Get the Names of Method Parameters
How to Access a Specific Item in a Listbox with Datatemplate
I Didn't Find "Zipfile" Class in the "System.Io.Compression" Namespace
What Is the Static Variable Initialization Order Across Classes in C#
Differencebetween Server.Mappath and Hostingenvironment.Mappath
Http 404 Page Not Found in Web API Hosted in Iis 7.5
Export Datatable to Excel with Open Xml Sdk in C#