Merge pull request #434 from dotnet/anurse/401-loader

Add ProjectAssemblyLoadContext
This commit is contained in:
Piotr Puszkiewicz 2015-12-08 16:30:33 -08:00
commit 68216b80cf
14 changed files with 390 additions and 12 deletions

View file

@ -47,6 +47,16 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Extensions.Testin
EndProject EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.DotNet.Tools.Test", "src\Microsoft.DotNet.Tools.Test\Microsoft.DotNet.Tools.Test.xproj", "{F003F228-2AE2-4E9D-877B-93EB773B5061}" Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.DotNet.Tools.Test", "src\Microsoft.DotNet.Tools.Test\Microsoft.DotNet.Tools.Test.xproj", "{F003F228-2AE2-4E9D-877B-93EB773B5061}"
EndProject EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.DotNet.ProjectModel.Loader", "src\Microsoft.DotNet.ProjectModel.Loader\Microsoft.DotNet.ProjectModel.Loader.xproj", "{C7AF0290-EF0D-44DC-9EDC-600803B664F8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{17735A9D-BFD9-4585-A7CB-3208CA6EA8A7}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "TestApp", "test\TestApp\TestApp.xproj", "{58808BBC-371E-47D6-A3D0-4902145EDA4E}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "TestLibrary", "test\TestLibrary\TestLibrary.xproj", "{947DD232-8D9B-4B78-9C6A-94F807D2DD58}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "LoadContextTest", "test\LoadContextTest\LoadContextTest.xproj", "{7A75ACC4-3C2F-44E1-B492-0EC08704E9FF}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -347,6 +357,70 @@ Global
{F003F228-2AE2-4E9D-877B-93EB773B5061}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU {F003F228-2AE2-4E9D-877B-93EB773B5061}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU
{F003F228-2AE2-4E9D-877B-93EB773B5061}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU {F003F228-2AE2-4E9D-877B-93EB773B5061}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU
{F003F228-2AE2-4E9D-877B-93EB773B5061}.RelWithDebInfo|x64.Build.0 = Release|Any CPU {F003F228-2AE2-4E9D-877B-93EB773B5061}.RelWithDebInfo|x64.Build.0 = Release|Any CPU
{C7AF0290-EF0D-44DC-9EDC-600803B664F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C7AF0290-EF0D-44DC-9EDC-600803B664F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C7AF0290-EF0D-44DC-9EDC-600803B664F8}.Debug|x64.ActiveCfg = Debug|Any CPU
{C7AF0290-EF0D-44DC-9EDC-600803B664F8}.Debug|x64.Build.0 = Debug|Any CPU
{C7AF0290-EF0D-44DC-9EDC-600803B664F8}.MinSizeRel|Any CPU.ActiveCfg = Debug|Any CPU
{C7AF0290-EF0D-44DC-9EDC-600803B664F8}.MinSizeRel|Any CPU.Build.0 = Debug|Any CPU
{C7AF0290-EF0D-44DC-9EDC-600803B664F8}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU
{C7AF0290-EF0D-44DC-9EDC-600803B664F8}.MinSizeRel|x64.Build.0 = Debug|Any CPU
{C7AF0290-EF0D-44DC-9EDC-600803B664F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C7AF0290-EF0D-44DC-9EDC-600803B664F8}.Release|Any CPU.Build.0 = Release|Any CPU
{C7AF0290-EF0D-44DC-9EDC-600803B664F8}.Release|x64.ActiveCfg = Release|Any CPU
{C7AF0290-EF0D-44DC-9EDC-600803B664F8}.Release|x64.Build.0 = Release|Any CPU
{C7AF0290-EF0D-44DC-9EDC-600803B664F8}.RelWithDebInfo|Any CPU.ActiveCfg = Release|Any CPU
{C7AF0290-EF0D-44DC-9EDC-600803B664F8}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU
{C7AF0290-EF0D-44DC-9EDC-600803B664F8}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU
{C7AF0290-EF0D-44DC-9EDC-600803B664F8}.RelWithDebInfo|x64.Build.0 = Release|Any CPU
{58808BBC-371E-47D6-A3D0-4902145EDA4E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{58808BBC-371E-47D6-A3D0-4902145EDA4E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{58808BBC-371E-47D6-A3D0-4902145EDA4E}.Debug|x64.ActiveCfg = Debug|Any CPU
{58808BBC-371E-47D6-A3D0-4902145EDA4E}.Debug|x64.Build.0 = Debug|Any CPU
{58808BBC-371E-47D6-A3D0-4902145EDA4E}.MinSizeRel|Any CPU.ActiveCfg = Debug|Any CPU
{58808BBC-371E-47D6-A3D0-4902145EDA4E}.MinSizeRel|Any CPU.Build.0 = Debug|Any CPU
{58808BBC-371E-47D6-A3D0-4902145EDA4E}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU
{58808BBC-371E-47D6-A3D0-4902145EDA4E}.MinSizeRel|x64.Build.0 = Debug|Any CPU
{58808BBC-371E-47D6-A3D0-4902145EDA4E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{58808BBC-371E-47D6-A3D0-4902145EDA4E}.Release|Any CPU.Build.0 = Release|Any CPU
{58808BBC-371E-47D6-A3D0-4902145EDA4E}.Release|x64.ActiveCfg = Release|Any CPU
{58808BBC-371E-47D6-A3D0-4902145EDA4E}.Release|x64.Build.0 = Release|Any CPU
{58808BBC-371E-47D6-A3D0-4902145EDA4E}.RelWithDebInfo|Any CPU.ActiveCfg = Release|Any CPU
{58808BBC-371E-47D6-A3D0-4902145EDA4E}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU
{58808BBC-371E-47D6-A3D0-4902145EDA4E}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU
{58808BBC-371E-47D6-A3D0-4902145EDA4E}.RelWithDebInfo|x64.Build.0 = Release|Any CPU
{947DD232-8D9B-4B78-9C6A-94F807D2DD58}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{947DD232-8D9B-4B78-9C6A-94F807D2DD58}.Debug|Any CPU.Build.0 = Debug|Any CPU
{947DD232-8D9B-4B78-9C6A-94F807D2DD58}.Debug|x64.ActiveCfg = Debug|Any CPU
{947DD232-8D9B-4B78-9C6A-94F807D2DD58}.Debug|x64.Build.0 = Debug|Any CPU
{947DD232-8D9B-4B78-9C6A-94F807D2DD58}.MinSizeRel|Any CPU.ActiveCfg = Debug|Any CPU
{947DD232-8D9B-4B78-9C6A-94F807D2DD58}.MinSizeRel|Any CPU.Build.0 = Debug|Any CPU
{947DD232-8D9B-4B78-9C6A-94F807D2DD58}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU
{947DD232-8D9B-4B78-9C6A-94F807D2DD58}.MinSizeRel|x64.Build.0 = Debug|Any CPU
{947DD232-8D9B-4B78-9C6A-94F807D2DD58}.Release|Any CPU.ActiveCfg = Release|Any CPU
{947DD232-8D9B-4B78-9C6A-94F807D2DD58}.Release|Any CPU.Build.0 = Release|Any CPU
{947DD232-8D9B-4B78-9C6A-94F807D2DD58}.Release|x64.ActiveCfg = Release|Any CPU
{947DD232-8D9B-4B78-9C6A-94F807D2DD58}.Release|x64.Build.0 = Release|Any CPU
{947DD232-8D9B-4B78-9C6A-94F807D2DD58}.RelWithDebInfo|Any CPU.ActiveCfg = Release|Any CPU
{947DD232-8D9B-4B78-9C6A-94F807D2DD58}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU
{947DD232-8D9B-4B78-9C6A-94F807D2DD58}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU
{947DD232-8D9B-4B78-9C6A-94F807D2DD58}.RelWithDebInfo|x64.Build.0 = Release|Any CPU
{7A75ACC4-3C2F-44E1-B492-0EC08704E9FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7A75ACC4-3C2F-44E1-B492-0EC08704E9FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7A75ACC4-3C2F-44E1-B492-0EC08704E9FF}.Debug|x64.ActiveCfg = Debug|Any CPU
{7A75ACC4-3C2F-44E1-B492-0EC08704E9FF}.Debug|x64.Build.0 = Debug|Any CPU
{7A75ACC4-3C2F-44E1-B492-0EC08704E9FF}.MinSizeRel|Any CPU.ActiveCfg = Debug|Any CPU
{7A75ACC4-3C2F-44E1-B492-0EC08704E9FF}.MinSizeRel|Any CPU.Build.0 = Debug|Any CPU
{7A75ACC4-3C2F-44E1-B492-0EC08704E9FF}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU
{7A75ACC4-3C2F-44E1-B492-0EC08704E9FF}.MinSizeRel|x64.Build.0 = Debug|Any CPU
{7A75ACC4-3C2F-44E1-B492-0EC08704E9FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7A75ACC4-3C2F-44E1-B492-0EC08704E9FF}.Release|Any CPU.Build.0 = Release|Any CPU
{7A75ACC4-3C2F-44E1-B492-0EC08704E9FF}.Release|x64.ActiveCfg = Release|Any CPU
{7A75ACC4-3C2F-44E1-B492-0EC08704E9FF}.Release|x64.Build.0 = Release|Any CPU
{7A75ACC4-3C2F-44E1-B492-0EC08704E9FF}.RelWithDebInfo|Any CPU.ActiveCfg = Release|Any CPU
{7A75ACC4-3C2F-44E1-B492-0EC08704E9FF}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU
{7A75ACC4-3C2F-44E1-B492-0EC08704E9FF}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU
{7A75ACC4-3C2F-44E1-B492-0EC08704E9FF}.RelWithDebInfo|x64.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -370,5 +444,9 @@ Global
{DB29F219-DC92-4AF7-A2EE-E89FFBB3F5F0} = {ED2FE3E2-F7E7-4389-8231-B65123F2076F} {DB29F219-DC92-4AF7-A2EE-E89FFBB3F5F0} = {ED2FE3E2-F7E7-4389-8231-B65123F2076F}
{DCDFE282-03DE-4DBC-B90C-CC3CE3EC8162} = {ED2FE3E2-F7E7-4389-8231-B65123F2076F} {DCDFE282-03DE-4DBC-B90C-CC3CE3EC8162} = {ED2FE3E2-F7E7-4389-8231-B65123F2076F}
{F003F228-2AE2-4E9D-877B-93EB773B5061} = {ED2FE3E2-F7E7-4389-8231-B65123F2076F} {F003F228-2AE2-4E9D-877B-93EB773B5061} = {ED2FE3E2-F7E7-4389-8231-B65123F2076F}
{C7AF0290-EF0D-44DC-9EDC-600803B664F8} = {ED2FE3E2-F7E7-4389-8231-B65123F2076F}
{58808BBC-371E-47D6-A3D0-4902145EDA4E} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7}
{947DD232-8D9B-4B78-9C6A-94F807D2DD58} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7}
{7A75ACC4-3C2F-44E1-B492-0EC08704E9FF} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View file

@ -0,0 +1,43 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Loader;
namespace Microsoft.DotNet.ProjectModel.Loader
{
public static class LoaderProjectContextExtensions
{
public static AssemblyLoadContext CreateLoadContext(this ProjectContext context, string configuration = "Debug")
{
var exporter = context.CreateExporter(configuration);
var assemblies = new Dictionary<AssemblyName, string>();
var dllImports = new Dictionary<string, string>();
foreach (var export in exporter.GetAllExports())
{
// TODO: Handle resource assemblies
foreach (var asset in export.RuntimeAssemblies)
{
// REVIEW: Should we use the following?
// AssemblyLoadContext.GetAssemblyName(asset.ResolvedPath);
var assemblyName = new AssemblyName(asset.Name);
assemblies[assemblyName] = asset.ResolvedPath;
}
foreach (var asset in export.NativeLibraries)
{
dllImports[asset.Name] = asset.ResolvedPath;
}
}
return new ProjectLoadContext(
assemblies,
dllImports,
// Add the project's output directory path to ensure project-to-project references get located
new[] { context.GetOutputDirectoryPath(configuration) });
}
}
}

View file

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0.24720" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0.24720</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>c7af0290-ef0d-44dc-9edc-600803b664f8</ProjectGuid>
<RootNamespace>Microsoft.DotNet.ProjectModel.Loader</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View file

@ -0,0 +1,101 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
namespace Microsoft.DotNet.ProjectModel.Loader
{
/// <summary>
/// An <see cref="AssemblyLoadContext"/> designed to allow loading from a Project.
/// </summary>
public class ProjectLoadContext : AssemblyLoadContext
{
private readonly IDictionary<AssemblyName, string> _assemblyPaths;
private readonly IDictionary<string, string> _nativeLibraries;
private readonly IEnumerable<string> _searchPaths;
private static readonly string[] NativeLibraryExtensions;
private static readonly string[] ManagedAssemblyExtensions = new[]
{
".dll",
".ni.dll",
".exe",
".ni.exe"
};
static ProjectLoadContext()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
NativeLibraryExtensions = new[] { ".dll" };
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
NativeLibraryExtensions = new[] { ".dylib" };
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
NativeLibraryExtensions = new[] { ".so" };
}
else
{
NativeLibraryExtensions = new string[0];
}
}
public ProjectLoadContext(IDictionary<AssemblyName, string> assemblyPaths,
IDictionary<string, string> nativeLibraries,
IEnumerable<string> searchPaths)
{
_assemblyPaths = assemblyPaths;
_nativeLibraries = nativeLibraries;
_searchPaths = searchPaths;
}
protected override Assembly Load(AssemblyName assemblyName)
{
string path;
if (_assemblyPaths.TryGetValue(assemblyName, out path) || SearchForLibrary(ManagedAssemblyExtensions, assemblyName.Name, out path))
{
return LoadFromAssemblyPath(path);
}
return null;
}
protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
{
string path;
if (_nativeLibraries.TryGetValue(unmanagedDllName, out path) || SearchForLibrary(NativeLibraryExtensions, unmanagedDllName, out path))
{
return LoadUnmanagedDllFromPath(path);
}
return base.LoadUnmanagedDll(unmanagedDllName);
}
private bool SearchForLibrary(string[] extensions, string name, out string path)
{
foreach (var searchPath in _searchPaths)
{
foreach (var extension in extensions)
{
var candidate = Path.Combine(searchPath, name + extension);
if (File.Exists(candidate))
{
path = candidate;
return true;
}
}
}
path = null;
return false;
}
}
}

View file

@ -0,0 +1,10 @@
{
"dependencies": {
"Microsoft.DotNet.ProjectModel": "1.0.0-*",
"System.AppContext": "4.0.1-beta-23504",
"System.Runtime.Loader": "4.0.0-beta-23504"
},
"frameworks": {
"dnxcore50": { }
}
}

View file

@ -21,22 +21,23 @@ namespace Microsoft.DotNet.ProjectModel.Workspaces
{ {
private Dictionary<string, AssemblyMetadata> _cache = new Dictionary<string, AssemblyMetadata>(); private Dictionary<string, AssemblyMetadata> _cache = new Dictionary<string, AssemblyMetadata>();
private readonly string[] _projectPaths; public ProjectJsonWorkspace(ProjectContext context) : base(MefHostServices.DefaultHost, "Custom")
{
AddProject(context);
}
public ProjectJsonWorkspace(string projectPath) : this(new[] { projectPath }) public ProjectJsonWorkspace(string projectPath) : this(new[] { projectPath })
{ {
} }
public ProjectJsonWorkspace(string[] projectPaths) : base(MefHostServices.DefaultHost, "Custom") public ProjectJsonWorkspace(IEnumerable<string> projectPaths) : base(MefHostServices.DefaultHost, "Custom")
{ {
_projectPaths = projectPaths; Initialize(projectPaths);
Initialize();
} }
private void Initialize() private void Initialize(IEnumerable<string> projectPaths)
{ {
foreach (var projectPath in _projectPaths) foreach (var projectPath in projectPaths)
{ {
AddProject(projectPath); AddProject(projectPath);
} }

View file

@ -0,0 +1,12 @@
using Microsoft.CodeAnalysis;
namespace Microsoft.DotNet.ProjectModel.Workspaces
{
public static class WorkspaceProjectContextExtensions
{
public static Workspace CreateWorkspace(this ProjectContext context)
{
return new ProjectJsonWorkspace(context);
}
}
}

View file

@ -0,0 +1,11 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.DotNet.ProjectModel
{
internal static class Constants
{
public static readonly string DefaultOutputDirectory = "bin";
public static readonly string DefaultConfiguration = "Debug";
}
}

View file

@ -130,14 +130,18 @@ namespace Microsoft.DotNet.ProjectModel
ProjectFile.Name + FileNameSuffixes.DotNet.ProgramDatabase); ProjectFile.Name + FileNameSuffixes.DotNet.ProgramDatabase);
} }
private string GetOutputDirectoryPath(string buildConfiguration) public string GetOutputDirectoryPath(string buildConfiguration)
{ {
return Path.Combine( var outDir = Path.Combine(
ProjectDirectory, ProjectDirectory,
DirectoryNames.Bin, DirectoryNames.Bin,
buildConfiguration, buildConfiguration,
TargetFramework.GetShortFolderName(), TargetFramework.GetShortFolderName());
ProjectModel.RuntimeIdentifier.Current); if (!string.IsNullOrEmpty(RuntimeIdentifier))
{
outDir = Path.Combine(outDir, RuntimeIdentifier);
}
return outDir;
} }
} }
} }

View file

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>7a75acc4-3c2f-44e1-b492-0ec08704e9ff</ProjectGuid>
<RootNamespace>LoadContextTest</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View file

@ -0,0 +1,51 @@
using System;
using System.IO;
using System.Reflection;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Loader;
using NuGet.Frameworks;
namespace LoadContextTest
{
public class Program
{
public static int Main(string[] args)
{
Console.WriteLine("Press enter to start");
Console.ReadLine();
// Get the path to the project
if(args.Length < 1)
{
Console.Error.WriteLine("Usage: LoadContextTest <path to project>");
return 1;
}
var project = Path.GetFullPath(args[0]);
// Load the project load context
Console.WriteLine($"Creating load context for {project}");
var context = ProjectContext.Create(project, FrameworkConstants.CommonFrameworks.DnxCore50, new[] { RuntimeIdentifier.Current });
var loadContext = context.CreateLoadContext();
// Load the project assembly
var asm = loadContext.LoadFromAssemblyName(new AssemblyName(context.ProjectFile.Name));
// Find the helper type and method
var type = asm.GetType("TestLibrary.Helper");
if(type == null)
{
Console.Error.WriteLine("Failed to find type");
return 1;
}
var method = type.GetMethod("SayHi", BindingFlags.Public | BindingFlags.Static);
if(method == null)
{
Console.Error.WriteLine("Failed to find method");
return 1;
}
// Call it!
method.Invoke(null, new object[0]);
return 0;
}
}
}

View file

@ -0,0 +1,22 @@
{
"version": "1.0.0-*",
"description": "LoadContextTest Console Application",
"compilationOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.CSharp": "4.0.1-beta-23516",
"System.Collections": "4.0.11-beta-23516",
"System.Console": "4.0.0-beta-23516",
"System.Linq": "4.0.1-beta-23516",
"System.Threading": "4.0.11-beta-23516",
"Microsoft.DotNet.ProjectModel.Loader": { "target": "project" }
},
"frameworks": {
"dnxcore50": { }
}
}

View file

@ -11,5 +11,10 @@ namespace TestLibrary
{ {
return "This string came from the test library!"; return "This string came from the test library!";
} }
public static void SayHi()
{
Console.WriteLine("Hello there!");
}
} }
} }

View file

@ -1,7 +1,8 @@
{ {
"version": "1.0.0-*", "version": "1.0.0-*",
"dependencies": { "dependencies": {
"System.Runtime": "4.0.21-beta-23428" "System.Runtime": "4.0.21-beta-23428",
"System.Console": "4.0.0-beta-23428"
}, },
"frameworks": { "frameworks": {