This commit is contained in:
Peter Hsu 2019-03-06 18:24:51 -08:00
Родитель 0919d80dda
Коммит b11b771e51
4 изменённых файлов: 89 добавлений и 9 удалений

Просмотреть файл

@ -20,7 +20,7 @@
<SystemSecurityCryptographyCspVersion>4.3.0</SystemSecurityCryptographyCspVersion>
<SystemServiceProcessServiceControllerVersion>4.5.0</SystemServiceProcessServiceControllerVersion>
<MicrosoftNETestSdkVersion>15.9.0</MicrosoftNETestSdkVersion>
<MicrosoftNETestSdkVersion>16.0.1</MicrosoftNETestSdkVersion>
<XUnitVersion>2.4.1</XUnitVersion>
<XUnitAbstractionsVersion>2.0.3</XUnitAbstractionsVersion>
<XUnitCoreVersion>2.4.1</XUnitCoreVersion>

Просмотреть файл

@ -5,7 +5,8 @@
namespace Microsoft.IIS.Administration
{
using AspNetCore.Hosting;
using Core;
using Core;
using Microsoft.IIS.Administration.Extensibility;
using Serilog;
using System;
using System.Collections.Generic;
@ -16,6 +17,7 @@ namespace Microsoft.IIS.Administration
class ModuleLoader
{
private IHostingEnvironment _env;
private AssemblyLoadContext _loader;
private List<Assembly> _loadedAssemblies;
private AdminHost _moduleHolder;
private string _moduleLoadBasePath;
@ -28,6 +30,7 @@ namespace Microsoft.IIS.Administration
this._moduleLoadBasePath = Path.Combine(env.ContentRootPath, PLUGINS_FOLDER_NAME);
this._loadedAssemblies = new List<Assembly>();
this._moduleHolder = AdminHost.Instance;
this._loader = new PluginAssemblyLoadContext(_moduleLoadBasePath);
}
public Assembly LoadModule(string assemblyName)
@ -36,18 +39,14 @@ namespace Microsoft.IIS.Administration
Log.Logger.Debug($"Loading plugin {assemblyName}");
var assembly = Assembly.LoadFrom(assemblyPath);
Assembly assembly = _loader.LoadFromAssemblyPath(assemblyPath);
_loadedAssemblies.Add(assembly);
//
// Every module should expose a type called Startup in a namespace equivalent to the assembly name
Type type = assembly.GetType(assemblyName + ".Startup");
IModule module = (IModule)Activator.CreateInstance(type);
IModule module = (IModule) Activator.CreateInstance(type);
_moduleHolder.Add(module);
return assembly;
}

Просмотреть файл

@ -0,0 +1,80 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.IIS.Administration.Extensibility
{
using Serilog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
// Implemented for https://github.com/dotnet/coreclr/issues/5837
// Can move back to resolver callback when fixed
public class PluginAssemblyLoadContext : AssemblyLoadContext
{
private string _pluginDir;
public PluginAssemblyLoadContext(string pluginDirectory)
{
this._pluginDir = pluginDirectory;
}
protected override Assembly Load(AssemblyName assemblyName)
{
var loaded = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == assemblyName.FullName);
if (loaded != null)
{
return loaded;
}
Assembly asm = null;
IList<string> rootPaths = new List<string>();
rootPaths.Add(Path.Combine(this._pluginDir, $"{assemblyName.Name}.{assemblyName.Version}"));
rootPaths.Add(this._pluginDir);
foreach (var path in rootPaths)
{
string asmPath = Path.Combine(path, $"{assemblyName.Name}.dll");
Log.Logger.Debug($"Resolving plugin dependency {assemblyName} using location {asmPath}");
if (File.Exists(asmPath))
{
// If LoadFromAssemblyPath's argument does not point to a valid assembly a fatal error will occur that will not throw an exception
// The process will terminate ungracefully
asm = this.LoadFromAssemblyPath(asmPath);
}
}
// Possible runtime assembly
if (asm == null)
{
string winRuntime = null;
string runtimes = Path.Combine(this._pluginDir, "runtimes");
if (Directory.Exists(runtimes))
{
winRuntime = Directory.GetDirectories(runtimes).FirstOrDefault(d => d.ToLower().Contains("win"));
}
if (winRuntime != null)
{
foreach (var file in Directory.GetFiles(winRuntime, "*.dll", SearchOption.AllDirectories))
{
if (Path.GetFileName(file) == assemblyName.Name + ".dll")
{
asm = this.LoadFromAssemblyPath(file);
break;
}
}
}
}
return asm;
}
}
}

Просмотреть файл

@ -24,7 +24,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETestSdkVersion)" />
<PackageReference Include="xunit" Version="$(XUnitVersion)" />
<PackageReference Include="xunit.assert" Version="2.4.1" />
<PackageReference Include="xunit.core" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XUnitRunnerVisualStudioVersion)">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>