props/Props/Services/Modules/ShopAssemblyLoadContext.cs

33 lines
1.2 KiB
C#
Raw Permalink Normal View History

using System;
using System.Reflection;
using System.Runtime.Loader;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.Logging;
using Props.Shop.Framework;
namespace Props.Services.Modules
{
internal class ShopAssemblyLoadContext : AssemblyLoadContext
{
private AssemblyDependencyResolver resolver;
public ShopAssemblyLoadContext(string path)
{
resolver = new AssemblyDependencyResolver(path);
}
protected override Assembly Load(AssemblyName assemblyName)
{
if (assemblyName.FullName.Equals(typeof(IShop).Assembly.FullName)) return null;
if (assemblyName.FullName.Equals(typeof(ILoggerFactory).Assembly.FullName)) return null;
string assemblyPath = resolver.ResolveAssemblyToPath(assemblyName);
return assemblyPath != null ? LoadFromAssemblyPath(assemblyPath) : null;
}
protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
{
string libPath = resolver.ResolveUnmanagedDllToPath(unmanagedDllName);
return libPath != null ? LoadUnmanagedDllFromPath(libPath) : IntPtr.Zero;
}
}
}