Esempio n. 1
0
        private static void AddProbingPaths(AssemblyLoadContextBuilder builder, RuntimeOptions options, string tfm)
        {
            if (options.AdditionalProbingPaths == null)
            {
                return;
            }

            foreach (string item in options.AdditionalProbingPaths)
            {
                string path = item;
                if (path.Contains("|arch|"))
                {
                    path = path.Replace("|arch|", RuntimeInformation.OSArchitecture.ToString().ToLowerInvariant());
                }

                if (path.Contains("|tfm|"))
                {
                    if (tfm == null)
                    {
                        // We don't have enough information to parse this
                        continue;
                    }

                    path = path.Replace("|tfm|", tfm);
                }

                builder.AddProbingPath(path);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Add a pre-parsed <see cref="DependencyContext" /> to the load context.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="dependencyContext">The dependency context.</param>
        /// <returns>The builder.</returns>
        public static AssemblyLoadContextBuilder AddDependencyContext(this AssemblyLoadContextBuilder builder, DependencyContext dependencyContext)
        {
            IReadOnlyList <RuntimeFallbacks> ridGraph = dependencyContext.RuntimeGraph.Any() || DependencyContext.Default == null
               ? dependencyContext.RuntimeGraph
               : DependencyContext.Default.RuntimeGraph;

            string           rid           = Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.GetRuntimeIdentifier();
            string           fallbackRid   = GetFallbackRid();
            RuntimeFallbacks fallbackGraph = ridGraph.FirstOrDefault(g => g.Runtime == rid)
                                             ?? ridGraph.FirstOrDefault(g => g.Runtime == fallbackRid)
                                             ?? new RuntimeFallbacks("any");

            foreach (ManagedLibrary managed in dependencyContext.ResolveRuntimeAssemblies(fallbackGraph))
            {
                builder.AddManagedLibrary(managed);
            }

            foreach (RuntimeLibrary library in dependencyContext.ResolveResourceAssemblies())
            {
                foreach (ResourceAssembly resource in library.ResourceAssemblies)
                {
                    /*
                     * For resource assemblies, look in $packageRoot/$packageId/$version/$resourceGrandparent
                     *
                     * For example, a deps file may contain
                     *
                     * "Example/1.0.0": {
                     *    "runtime": {
                     *         "lib/netcoreapp2.0/Example.dll": { }
                     *     },
                     *     "resources": {
                     *         "lib/netcoreapp2.0/es/Example.resources.dll": {
                     *           "locale": "es"
                     *         }
                     *     }
                     * }
                     *
                     * In this case, probing should happen in $packageRoot/example/1.0.0/lib/netcoreapp2.0
                     */

                    string resourceDir = Path.GetDirectoryName(Path.GetDirectoryName(resource.Path));

                    if (resourceDir != null)
                    {
                        string path = Path.Combine(library.Name.ToLowerInvariant(),
                                                   library.Version,
                                                   resourceDir);

                        builder.AddResourceProbingSubpath(path);
                    }
                }
            }

            foreach (NativeLibrary native in dependencyContext.ResolveNativeAssets(fallbackGraph))
            {
                builder.AddNativeLibrary(native);
            }

            return(builder);
        }
Esempio n. 3
0
        /// <summary>
        /// Adds additional probing paths to a managed load context using settings found in the runtimeconfig.json
        /// and runtimeconfig.dev.json files.
        /// </summary>
        /// <param name="builder">The context builder</param>
        /// <param name="runtimeConfigPath">The path to the runtimeconfig.json file</param>
        /// <param name="includeDevConfig">Also read runtimeconfig.dev.json file, if present.</param>
        /// <param name="error">The error, if one occurs while parsing runtimeconfig.json</param>
        /// <returns>The builder.</returns>
        public static AssemblyLoadContextBuilder TryAddAdditionalProbingPathFromRuntimeConfig(
            this AssemblyLoadContextBuilder builder,
            string runtimeConfigPath,
            bool includeDevConfig,
            out Exception error)
        {
            error = null;
            try
            {
                RuntimeConfig config = TryReadConfig(runtimeConfigPath);
                if (config == null)
                {
                    return(builder);
                }

                RuntimeConfig devConfig = null;
                if (includeDevConfig)
                {
                    string configDevPath = runtimeConfigPath.Substring(0, runtimeConfigPath.Length - JsonExt.Length) + ".dev.json";
                    devConfig = TryReadConfig(configDevPath);
                }

                string tfm = config.runtimeOptions?.Tfm ?? devConfig?.runtimeOptions?.Tfm;

                if (config.runtimeOptions != null)
                {
                    AddProbingPaths(builder, config.runtimeOptions, tfm);
                }

                if (devConfig?.runtimeOptions != null)
                {
                    AddProbingPaths(builder, devConfig.runtimeOptions, tfm);
                }

                if (tfm != null)
                {
                    string dotnet = Process.GetCurrentProcess().MainModule.FileName;
                    if (string.Equals(Path.GetFileNameWithoutExtension(dotnet), "dotnet", StringComparison.OrdinalIgnoreCase))
                    {
                        string dotnetHome = Path.GetDirectoryName(dotnet);
                        if (dotnetHome != null)
                        {
                            builder.AddProbingPath(Path.Combine(dotnetHome, "store", RuntimeInformation.OSArchitecture.ToString().ToLowerInvariant(), tfm));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            return(builder);
        }
Esempio n. 4
0
        /// <summary>
        /// Add dependency information to a load context from a .deps.json file.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="depsFilePath">The full path to the .deps.json file.</param>
        /// <returns>The builder.</returns>
        public static AssemblyLoadContextBuilder AddDependencyContext(this AssemblyLoadContextBuilder builder, string depsFilePath)
        {
            DependencyContextJsonReader reader = new DependencyContextJsonReader();

            using (FileStream file = File.OpenRead(depsFilePath))
            {
                DependencyContext deps = reader.Read(file);
                builder.AddDependencyContext(deps);
            }

            return(builder);
        }
Esempio n. 5
0
        /// <summary>
        /// Add dependency information to a load context from a .deps.json file.
        /// </summary>
        /// <param name="builder">The builder.</param>
        /// <param name="depsFilePath">The full path to the .deps.json file.</param>
        /// <param name="error">An error, if one occurs while reading .deps.json</param>
        /// <returns>The builder.</returns>
        public static AssemblyLoadContextBuilder TryAddDependencyContext(this AssemblyLoadContextBuilder builder, string depsFilePath, out Exception error)
        {
            error = null;
            try
            {
                builder.AddDependencyContext(depsFilePath);
            }
            catch (Exception ex)
            {
                error = ex;
            }

            return(builder);
        }