Referencing a PCL library with Roslyn results in .NET version issues - c#

I'm trying to use Roslyn to execute a block of code that references a PCL library. Both my console application and the PCL library are targeted to .NET 4.5
The syntax tree executes a method in the referenced library that constructs a library class. There should be no .NET 4.0 references.
(5,27): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Has anyone had issues with PCL and Roslyn, or got it to work before?
MyCompanyApplication:Program.cs
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using System;
using System.IO;
using System.Reflection;
namespace MyCompanyApplication
{
class Program
{
static void Main(string[] args)
{
EmitResult Result;
var Options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
CSharpCompilation Compilation = CSharpCompilation.Create(
assemblyName: Path.GetRandomFileName(),
syntaxTrees: new[] { CSharpSyntaxTree.ParseText(
#"class Test
{
public void Run(MyCompanyLibrary.Class Class)
{
var Label = Class.NewLabel();
}
}") },
references: new[]
{
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
MetadataReference.CreateFromFile(typeof(MyCompanyLibrary.Class).Assembly.Location),
},
options: Options);
Assembly Assembly = null;
using (var Stream = new MemoryStream())
{
Result = Compilation.Emit(Stream);
if (Result.Success)
Assembly = Assembly.Load(Stream.GetBuffer());
}
if (Result.Success)
{
var TestType = Assembly.GetType("Test");
var Instance = TestType.GetConstructor(new Type[0]).Invoke(new object[0]);
var RunMethod = TestType.GetMethod("Run");
RunMethod.Invoke(Instance, new object[] { new MyCompanyLibrary.Class() });
}
else
{
Console.WriteLine("Test (PCL) failed");
Console.ReadLine();
}
}
}
}
class Test
{
public void Run(MyCompanyLibrary.Class Class)
{
var Label = Class.NewLabel();
}
}
MyCompanyLibrary:Class.cs
namespace MyCompanyLibrary
{
public class Class
{
public Class()
{
}
public Label NewLabel()
{
return new Label(this);
}
}
public class Label
{
internal Label(Class Class)
{
this.Class = Class;
}
private Class Class;
}
}

You are adding a reference to object from your "MyCompanyApplication", which is not a portable class library.
Change this:
MetadataReference.CreateFromFile(typeof(object).Assembly.Location)
to this:
MetadataReference.CreateFromFile(#"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.5\Profile\Profile7\System.Runtime.dll")

Related

Runtime compiling DbContext with EF core, missing reference for Migrator()

I'm building an app that requires a EntityFrameWorkCore DbContext to be created runtime and then migrated, the code below compiles and runs if i don't use the dbContext.Database.Migrate() method, but if i do i get a diagnositcs error about missing a directive/reference.
error CS1061: 'DatabaseFacade' does not contain a definition for 'Migrate' and no accessible extension method 'Migrate' accepting a first argument of type 'DatabaseFacade' could be found (are you missing a using directive or an assembly reference?)
I also don't get any errors if i simply create a file with the code in my project. From what i can tell the "DatabaseFacade" is part of EntityFrameWorkcore.Infrastructure, which should be part if Microsoft.EntityFrameworkCore.
These are the references i'm including in the CSharpCompiler:
"System"
"System.Console"
"System.Runtime"
"System.Private.CoreLib"
"System.Linq"
"System.Data.Common"
"System.Data"
"System.Data.SqlClient"
"System.ComponentModel"
"Microsoft.EntityFrameworkCore"
"Microsoft.EntityFrameworkCore.SqlServer"
"netstandard"
I'm using Microsoft.CodeAnalysis to create a CSharpCompilation and then emit an assembly that i'm calling the main method in during runtime.
I've even tried to call the migrate() method with reflection to bypass intellisence, but GetMethod("Migrate") returns null, so it's obviously not there.
This is the code i am trying to compile and use runtime (Simplyfied):
using System;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using mothership_shared.Data;
using mothership_shared.Enums;
using mothership_shared.Interfaces;
using mothership_shared.Models;
using static mothership_shared.Attributes.PropertyCalculations;
using static mothership_shared.Attributes.PropertyTypes;
using static mothership_shared.Attributes.Settings;
using static mothership_shared.Data.SqlServiceClasses;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Infrastructure;
namespace mothership_shared.MigratorProgram
{
public class Program
{
static void Main(string[] args)
{
SqlConnectionSettings sqlConnectionSettings = new SqlConnectionSettings()
{
ServerUrl = args[0],
Catalog = args[1],
User = args[2],
Password = args[3],
};
var dbContext = new ApplicationDbContext(sqlConnectionSettings.AppConnection);
ISql sqlService = new SqlService();
var request = new DeleteMigrationHistory.Request()
{
ConnectionSettings = sqlConnectionSettings,
};
sqlService.DeleteMigrationHistory(request);
dbContext.Database.Migrate();
}
public void Run(string[] args)
{
Main(args);
}
public class ApplicationDbContext : DbContext
{
private readonly string _connectionString;
public ApplicationDbContext(string connectionString)
{
_connectionString = connectionString;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(_connectionString);
}
public DbSet<SimpleEntity> SimpleEntity { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var allTypes = Assembly.GetCallingAssembly().GetTypes();
var EntityTypes = allTypes.Where(t => t.BaseType == typeof(BaseEntity));
foreach (var t in EntityTypes)
{
var crud = t.GetCustomAttribute<CRUDAttribute>();
var properties = t.GetProperties();
foreach (var p in properties)
{
IsObjectAttribute otm = p.GetCustomAttribute<IsObjectAttribute>();
if (otm != null)
{
if (crud.ClassType != ClassType.MTM)
{
modelBuilder.Entity(t)
.HasOne(p.PropertyType, p.Name)
.WithMany(otm.WithMany)
.OnDelete(otm.DeleteBehavior);
}
else
{
modelBuilder.Entity(t)
.HasOne(p.PropertyType, p.Name)
.WithMany(otm.WithMany)
.OnDelete(DeleteBehavior.Cascade);
}
};
IsTimeSpanAttribute ts = p.GetCustomAttribute<IsTimeSpanAttribute>();
if (ts != null)
{
modelBuilder.Entity(t)
.Property(p.Name)
.HasConversion(new TimeSpanToTicksConverter());
}
}
}
}
}
[CRUDAttribute(ClassType.Referal)]
[HeaderAttribute("Simple entity", "Simple entitys", 0)]
[IconAttribute("ms-Icon--Document")]
[MenuAttribute("Test menu")]
[TooltipAttribute("This is a model that contains all simple propertytypes")]
public class SimpleEntity : BaseEntity
{
[IsTextAttribute(false)]
[HeaderAttribute("Text", "Texts", 0)]
[PriorityAttribute(1)]
[TooltipAttribute("This is a text property")]
[IconAttribute("ms-Icon--Text")]
[DefaultValueAttribute("This is the defaultvalue")]
public string TextProperty { get; set; }
}
}
}
You need to install the extension Microsoft.EntityFrameworkCore.Tools, because Migrate is a part of it. If it still doesn't work, you can try dotnet restore, it often helps with extension related problems.

Custom AssemblyLoadContext failing to load Microsoft.AspNetCore.Components

Edit: I have uploaded the source code for the issue to GitHub if you would like to download: https://github.com/bryanenroute/assemblyloadcontext-issue
I have a .NET Core 3.0 console application that references a .NET Standard 2.0 class library with a single interface (IModule). I also have a ASP.NET Core 3.0 application that references the same .NET Standard 2.0 class library and implements the interface (Module : IModule).
I am trying to load the ASP.NET Core assembly from the .NET Core console application using a custom AssemblyLoadContext and a common class library interface (IModule)... a simple plugin system.
Unfortunately, the ASP.NET Core module/plugin fails in the ALC override function for Load(AssemblyName) with the following exception:
Could not load file or assembly 'Microsoft.AspNetCore.Components, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
When I try with a different project type (e.g. .NET Core Console Application or .NET Standard 2.0 Class Library), the module/plugin loads as intended.
Here's the Console app code:
using NetStandardCommon;
using System;
using System.IO;
namespace NetCoreConsoleApp
{
class Program
{
static void Main(string[] args)
{
LoadNetCoreModule();
LoadAspNetCoreModule();
}
static void LoadNetCoreModule()
{
//Works!
FileInfo asm = new FileInfo(#"..\..\..\..\NetCoreModule\bin\debug\netcoreapp3.0\NetCoreModule.dll");
var moduleDirectory = asm.DirectoryName;
ModuleAssemblyLoadContext context = new ModuleAssemblyLoadContext(asm.Name, moduleDirectory, typeof(IModule));
context.Scan();
foreach (var module in context.GetImplementations<IModule>())
{
module.Start();
}
}
static void LoadAspNetCoreModule()
{
//Fails!
FileInfo asm = new FileInfo(#"..\..\..\..\AspNetCoreApp\bin\debug\netcoreapp3.0\AspNetCoreApp.dll");
var moduleDirectory = asm.DirectoryName;
ModuleAssemblyLoadContext context = new ModuleAssemblyLoadContext(asm.Name, moduleDirectory, typeof(IModule));
context.Scan();
foreach (var module in context.GetImplementations<IModule>())
{
module.Start();
}
}
}
}
Here's the ModuleAssemblyLoadContext code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.Loader;
using System.Linq;
namespace NetCoreConsoleApp
{
public class ModuleAssemblyLoadContext : AssemblyLoadContext
{
private List<Assembly> _loaded;
private Dictionary<string, Assembly> _shared;
private string _path;
private AssemblyDependencyResolver _resolver;
public ModuleAssemblyLoadContext(string name, string path, params Type[] sharedTypes) : base(name)
{
_path = path;
_resolver = new AssemblyDependencyResolver(_path);
_loaded = new List<Assembly>();
_shared = new Dictionary<string, Assembly>();
if (sharedTypes != null)
{
foreach (Type sharedType in sharedTypes)
{
_shared[Path.GetFileName(sharedType.Assembly.Location)] = sharedType.Assembly;
}
}
}
public void Scan()
{
foreach (string dll in Directory.EnumerateFiles(_path, "*.dll"))
{
var file = Path.GetFileName(dll);
if (_shared.ContainsKey(file))
{
continue;
}
var asm = this.LoadFromAssemblyPath(dll);
_loaded.Add(asm);
}
}
public IEnumerable<T> GetImplementations<T>()
{
return _loaded
.SelectMany(a => a.GetTypes())
.Where(t => typeof(T).IsAssignableFrom(t))
.Select(t => Activator.CreateInstance(t))
.Cast<T>();
}
protected override Assembly Load(AssemblyName assemblyName)
{
string filename = $"{assemblyName.Name}.dll";
if (_shared.ContainsKey(filename))
{
return _shared[filename];
}
string assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
if (assemblyPath != null)
{
return LoadFromAssemblyPath(assemblyPath);
}
return null;
}
protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
{
string libraryPath = _resolver.ResolveUnmanagedDllToPath(unmanagedDllName);
if (libraryPath != null)
{
return LoadUnmanagedDllFromPath(libraryPath);
}
return IntPtr.Zero;
}
}
}
I tried modifying the ALC Load function to load the assemblies directly from the shared folder (C:\Program Files (x86)\dotnet\shared\Microsoft.AspNetCore.App\3.0.0) which lets the execution continue a bit farther, but it ultimately fails with the following exception:
An attempt was made to load a program with an incorrect format. (0x8007000B)
Here's the revised Load function:
protected override Assembly Load(AssemblyName assemblyName)
{
string filename = $"{assemblyName.Name}.dll";
if (_shared.ContainsKey(filename))
{
return _shared[filename];
}
try
{
if (File.Exists(#"C:\Program Files (x86)\dotnet\shared\Microsoft.AspNetCore.App\3.0.0\" + filename))
{
return Assembly.LoadFrom(#"C:\Program Files (x86)\dotnet\shared\Microsoft.AspNetCore.App\3.0.0\" + filename);
}
}
catch (Exception ex)
{
//Message displayed is 'An attempt was made to load a program with an incorrect format. (0x8007000B)'
Console.WriteLine(ex.Message);
}
return Assembly.Load(assemblyName);
}
I'm excited about the possibilities of loading/unloading assemblies for a .net Core plugin system, but I'm struggling to get over this hurdle. What am I missing?
I had this issue about a month ago with loading a Assembly to my SQL server. Are you using virtual drives to store your Assembly? I found out that our share drive actual drive path was a E drive and not a P drive which is what is mapped on my computer. I was virtually connected to it, so I had to give the real Drive path which started with E instead of P. Also, your program might be mapping it to the wrong drive as well. I would check that, and if that doesn't help I have about 3-4 more things to try as far as this particular issue in concerned.
I believe the library might also need to be built with the target framework set to .NetCore 3.0 (netcoreapp3.0).

error powershell c# visual studio

I have got a simple class libary project in visual studio:
using System;
using System.Linq;
using System.Management.Automation;
namespace EdoX_auslesen_xml
{
[Cmdlet(VerbsCommon.Get, "DemoNames")]
public class Get_DemoNames : Cmdlet
{
[Parameter(Mandatory = false)]
public string Prefix;
protected override void ProcessRecord()
{
var names = new string[] { "Chris", "Charlie", "Anne" };
if (string.IsNullOrEmpty(this.Prefix))
{
this.WriteObject(names, true);
}
else
{
var prefixed_names = names.Select(n => this.Prefix + n);
this.WriteObject(prefixed_names, true);
}
}
}
}
When I open PowerShell an try to "Get-DemoNames" an error occurs:
in English:
Get-DemoNames: the file or assebly "System.Runtime, Version=4.2.0.0. Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" or a dependency of it was not found. The system cannot find the given file.
In line:1 sign:1
+ Get- DemoNames...
I think you need to add a reference to System.Runtime.
View this StackOverflow question.

Unable to load NuGet dll with platform specific dlls in netcoreapp

I am unable to load System.Data.Client dll from it's nuget package using the ICompilationAssemblyResolver I have available in a netcoreapp. The bulk of the Assembly resolving is borrowed from here, and works great for the most part. It looks like so:
internal sealed class AssemblyResolver : IDisposable
{
private readonly ICompilationAssemblyResolver assemblyResolver;
private readonly DependencyContext dependencyContext;
private readonly AssemblyLoadContext loadContext;
public AssemblyResolver(string path)
{
this.Assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(path);
this.dependencyContext = DependencyContext.Load(this.Assembly);
this.assemblyResolver = new CompositeCompilationAssemblyResolver(new ICompilationAssemblyResolver[]
{
new AppBaseCompilationAssemblyResolver(Path.GetDirectoryName(path)),
new ReferenceAssemblyPathResolver(),
new PackageCompilationAssemblyResolver()
});
this.loadContext = AssemblyLoadContext.GetLoadContext(this.Assembly);
this.loadContext.Resolving += OnResolving;
}
public Assembly Assembly { get; }
public void Dispose()
{
this.loadContext.Resolving -= this.OnResolving;
}
private Assembly OnResolving(AssemblyLoadContext context, AssemblyName name)
{
bool NamesMatch(RuntimeLibrary runtime)
{
return string.Equals(runtime.Name, name.Name, StringComparison.OrdinalIgnoreCase);
}
RuntimeLibrary library =
this.dependencyContext.RuntimeLibraries.FirstOrDefault(NamesMatch);
if (library != null)
{
var wrapper = new CompilationLibrary(
library.Type,
library.Name,
library.Version,
library.Hash,
library.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths),
library.Dependencies,
library.Serviceable);
var assemblies = new List<string>();
this.assemblyResolver.TryResolveAssemblyPaths(wrapper, assemblies);
if (assemblies.Count > 0)
{
return this.loadContext.LoadFromAssemblyPath(assemblies[0]);
}
}
return null;
}
}
However I am unable to load a dll that references System.Data.Client # 4.3.1 as at runtime I get the error message:
Exception has occurred: CLR/System.IO.FileNotFoundException
An unhandled exception of type 'System.IO.FileNotFoundException'
occurred in System.Private.CoreLib.ni.dll: 'Could not load file or
assembly 'System.Data.SqlClient, Version=4.1.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.'
I am not sure why it is trying to load 4.1.0 when I have specified 4.3.0 but I think that is a bit of a red herring. I suspect that the PackageCompilationAssemblyResolver only looks under the lib folder, and the package in question does not have one for netstandard. It does however have one for specific runtimes:
Armed with this information I have created an incredibly crude AssemblyLoader that looks under the runtimes folder for a nuget package and I am able to load the dll and run my program as I expect.
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.DotNet.PlatformAbstractions;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.DependencyModel.Resolution;
namespace Loader
{
public class CrudeCompilationAssemblyResolver : ICompilationAssemblyResolver
{
private readonly string[] _nugetPackageDirectories;
public CrudeCompilationAssemblyResolver()
{
var basePath = Environment.GetEnvironmentVariable("HOME");
var defaultPath = Path.Combine(basePath, ".nuget", "packages");
_nugetPackageDirectories = new [] { defaultPath };
}
public bool TryResolveAssemblyPaths(CompilationLibrary library, List<string> assemblies)
{
if (_nugetPackageDirectories == null || _nugetPackageDirectories.Length == 0 || !string.Equals(library.Type, "package", StringComparison.OrdinalIgnoreCase))
{
return false;
}
foreach (var directory in _nugetPackageDirectories)
{
string packagePath;
var fullPath = Path.Combine(directory, library.Name, library.Version, "runtimes", "unix", "lib", "netstandard1.3", $"{library.Name}.dll");
if (File.Exists(fullPath))
{
assemblies.AddRange(new[] { fullPath });
return true;
}
}
return false;
}
}
}
My question is: Is there a better/officially sanctioned way of loading this troublesome assembly from a nuget package? Or do I need to make my crude loader a lot less crude?
Full repo is here: CustomAssemblyResolver

Error deserializing object having a field of type declared in the other assembly loaded on AssemblyResolve

I have an application which embedes (via BuildAction: Embedded Resource) referenced assembly (called ClassLibrary1) inside itself and loads it on AppDomain.CurrentDomain.AssemblyResolve event.
Main assembly defines a class Class1:
public class Class1
{
public Class2 MyField { get; set; }
}
It has a property of type Class2 defined in ClassLibrary1.
Definition of Class2:
public class Class2
{
public int A { get; set; }
}
In the main method I`m creating a new XmlSerializer(typeof(Class1)):
static void Main()
{
SubscribeAssemblyResolver();
MainMethod();
}
private static void MainMethod()
{
XmlSerializer xs2 = new XmlSerializer(typeof(Class1));
Class1 cl = new Class1();
}
While executing a programm I get the following error:
Unable to generate a temporary class (result=1).
error CS0012: The type 'ClassLibrary1.Class2' is defined in an assembly that is not referenced. You must add a reference to assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c06f123f2868e8c8'.
error CS0266: Cannot implicitly convert type 'object' to 'ClassLibrary1.Class2'. An explicit conversion exists (are you missing a cast?)
Any ideas?
The rest of the code:
private static void SubscribeAssemblyResolver()
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}
static Dictionary<String, Assembly> _assemblies = new Dictionary<String, Assembly>(StringComparer.OrdinalIgnoreCase);
static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
return ResolveAssembly(args.Name);
}
private static Assembly ResolveAssembly(string argsName)
{
Assembly dll;
var name = "WindowsFormsApplication1.Libs." + new AssemblyName(argsName).Name + ".dll";
if (!_assemblies.TryGetValue(name, out dll))
{
Assembly res = typeof(Program).Assembly;
using (var input = res.GetManifestResourceStream(name))
{
if (input == null)
{
//TODO: log
return null;
}
Byte[] assemblyData = new Byte[input.Length];
input.Read(assemblyData, 0, assemblyData.Length);
if (null == (dll = Assembly.Load(assemblyData)))
{
//TODO: log
return null;
}
//TODO: log
_assemblies[name] = dll;
return dll;
}
}
return dll;
}
UPDATE: Created a BUG on the microsoft Connect site. You can also download a sample visual stuido 2010 solution (just expand Details fieldgroup) from there to reproduce it.
I've solved similar problem by saving assembly in temporary folder
public static byte[] ReadFully(Stream input)
{
var buffer = new byte[16 * 1024];
using (var ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
public App()
{
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
var assemblyName = new AssemblyName(args.Name);
if (assemblyName.Name != "Omikad.Core")
return null;
var resourceName = "Terem." + assemblyName.Name + ".dll";
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
if (stream == null)
return null;
var assemblyData = ReadFully(stream);
var tmp = Path.Combine(Path.GetTempPath(), "Omikad.Core.dll");
File.WriteAllBytes(tmp, assemblyData);
return Assembly.LoadFrom(tmp);
}
};
}
Try to add atribute:
[XmlInclude(typeof(Class2))]
public class Class1
{
public Class2 MyField { get; set; }
}
As for now I`ve ended up with two somewhat bad solutions:
While you can`t instanciate XmlSerializer for the type Class1, you still can instanciate it for the type Class2 from the main assembly. That does mean that if you move Class1 to ClassLibrary1 or Class2 to the main assembly - it will deserialize without errors. It works, but it is not possible to use this solution everywhere, plus it is ideologically wrong.
Use ILMerge to merge those assemblies into one. But it only works for non-wpf stuff, plus you should manage the situation with the assemblies attributes (there could be conflicts).
And one very bad idea:
Generate ClassLibrary1.XmlSerializer.dll with sgen.exe.
Also embed it into the main assembly.
Explicitly load it to the XmlSerializer cache calling one of it`s internal methods via reflection.
Although I had to use solution number one for now, I`m not satisfied with it, because it is too constraining.
I'd try the XmlSerializer(Type, Type[]) constructor and provide Class2 as an additional type using the second parameter. I've few experience with the XmlSerializer, but for DataContractSerializer this does the trick.

Categories