Skip to content

Microsoft.Extensions.PlatformAbstractions package split into two #149

@pakrym

Description

@pakrym

Microsoft.Extensions.PlatformAbstractions package split into two
In order to make the transition to the .NET CLI a smooth process we split Microsoft.Extensions.PlatformAbstractions package into two:

  1. Microsoft.Extensions.PlatformAbstractions - API's that would still exist in dotnet cli world: IRuntimeEnvironment, IApplicationEnvironment
  2. Microsoft.Extensions.PlatformAbstractions.Dnx - API's supported only by DNX: IAssemblyLoaderContainer, IAssemblyLoadContextAccessor, ILibraryManager accessible using DnxPlatformServices.Default static property.

Alternatives

  1. IAssemblyLoadContextAccessor may be replaced by calls to Assebly.Load* on desktop CLR and AssemblyLoadContext.Default.Load* on CoreCLR
  2. IAssemblyLoaderContainer loaders may be replaced by implementing AssemblyLoadContext on CoreClr and subscribing to AppDomain.AssemblyResolve event on desktop CLR.
  3. ILibraryManager may be replaced by:

DependencyContext class from Microsoft.Extensions.DependencyModel shoud be used when you need to access dependencies at runtime and don't have access to project.json or project.lock.json. To use DependencyModel, preserveCompilationContext shoud be set to true in compilerOptions section of project.json which makes build system embed dependency info into assembly being build and copy compilation references on publish.

WorkspaceContext from Microsoft.DotNet.ProjectModel package if you want to operate on project`s source code.

DependencyModel sample

Lets say you want to list all dependencies your entry assembly was compiled against:

project.json

{
    "compilationOptions": {
        "preserveCompilationContext": true,
        "emitEntryPoint": true
    },
    "dependencies" : {
        "Microsoft.Extensions.DependencyModel": "1.0.0-*"
    },
    "frameworks": {
        "net451": {
          "frameworkAssemblies":
          {
            "System.Runtime": {}
          }
        }
    }
}

Program.cs

using System;
using Microsoft.Extensions.DependencyModel;

namespace DependencyList
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            // Default DependencyContext is retrieved from entry assembly
            var deps = DependencyContext.Default;
            Console.WriteLine($"Compilation depenencies");
            foreach (var compilationLibrary in deps.CompileLibraries)
            {
                Console.WriteLine($"\tPackage {compilationLibrary.PackageName} {compilationLibrary.Version}");
                // ResolveReferencePaths returns full paths to compilation assemblies
                foreach (var resolveReferencePath in compilationLibrary.ResolveReferencePaths())
                {
                    Console.WriteLine($"\t\tReference path: {resolveReferencePath}");
                }
            }

            Console.WriteLine($"Runtime depenencies");
            foreach (var compilationLibrary in deps.RuntimeLibraries)
            {
                Console.WriteLine($"\tPackage {compilationLibrary.PackageName} {compilationLibrary.Version}");
                foreach (var assembly in compilationLibrary.Assemblies)
                {
                    Console.WriteLine($"\t\tReference: {assembly.Name}");
                }
            }
        }
    }
}

nuget.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="NuGet.org" value="https://api.nuget.org/v3/index.json" />
    <add key="dotnet-core" value="https://www.myget.org/F/dotnet-core/api/v3/index.json" />
    <add key="dotnet-cli" value="https://www.myget.org/F/dotnet-cli/api/v3/index.json" />
  </packageSources>
</configuration>

Output

Compilation depenencies
        Package MyTestApp 1.0.0
                Reference path: D:\MyTestApp\bin\.dotnetrun\ad9216fd95ff46a8929a709ebc2c170e\Debug\net451\MyTestApp.exe
        Package Microsoft.Extensions.DependencyModel 1.0.0-dev-001134
                Reference path: D:\MyTestApp\bin\.dotnetrun\ad9216fd95ff46a8929a709ebc2c170e\Debug\net451\refs\Microsoft.Extensions.DependencyModel.dll
        Package Newtonsoft.Json 7.0.1
                Reference path: D:\MyTestApp\bin\.dotnetrun\ad9216fd95ff46a8929a709ebc2c170e\Debug\net451\refs\Newtonsoft.Json.dll
        Package System.Runtime 4.0.10.0
                Reference path: D:\MyTestApp\bin\.dotnetrun\ad9216fd95ff46a8929a709ebc2c170e\Debug\net451\refs\System.Runtime.dll
        Package mscorlib 4.0.0.0
                Reference path: D:\MyTestApp\bin\.dotnetrun\ad9216fd95ff46a8929a709ebc2c170e\Debug\net451\refs\mscorlib.dll
        Package System 4.0.0.0
                Reference path: D:\MyTestApp\bin\.dotnetrun\ad9216fd95ff46a8929a709ebc2c170e\Debug\net451\refs\System.dll
        Package System.Core 4.0.0.0
                Reference path: D:\MyTestApp\bin\.dotnetrun\ad9216fd95ff46a8929a709ebc2c170e\Debug\net451\refs\System.Core.dll
        Package Microsoft.CSharp 4.0.0.0
                Reference path: D:\MyTestApp\bin\.dotnetrun\ad9216fd95ff46a8929a709ebc2c170e\Debug\net451\refs\Microsoft.CSharp.dll
Runtime depenencies
        Package MyTestApp 1.0.0
                Reference: MyTestApp
        Package Microsoft.Extensions.DependencyModel 1.0.0-dev-001134
                Reference: Microsoft.Extensions.DependencyModel
        Package Newtonsoft.Json 7.0.1
                Reference: Newtonsoft.Json
        Package System.Runtime 4.0.10.0
        Package mscorlib 4.0.0.0
        Package System 4.0.0.0
        Package System.Core 4.0.0.0
        Package Microsoft.CSharp 4.0.0.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions