将某些RC1代码转换为RC2

时间:2016-06-08 14:01:58

标签: asp.net-core asp.net-core-mvc asp.net-core-1.0

在RC1中,通过Library执行此操作,我能够获得Librarymanager的程序集:

_libraryManager.GetLibraries().SelectMany(l => l.Assemblies).Distinct().ToList();

在RC2中,似乎没有任何API可用于获取库的程序集。

我已阅读公告:https://github.com/aspnet/Announcements/issues/149并且此公告仅解释了如何获取程序集的依赖项,但它没有解释如何获取库的程序集,现在不推荐使用Assemblies属性。

有人有什么想法吗?

我还在这里提出了一个github问题https://github.com/aspnet/Home/issues/1554

2 个答案:

答案 0 :(得分:1)

从RC1到RC2的解决方案是抛弃ILibraryManager并使用新的替换(DependencyContext)。看看MVC6如何在这里做到:

https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.Core/Internal/DefaultAssemblyPartDiscoveryProvider.cs#L55-L158

我也会在这里包含源代码,以防链接由于某种原因而消失:

 // Discovers assemblies that are part of the MVC application using the DependencyContext.
public static class DefaultAssemblyPartDiscoveryProvider
{
    internal static HashSet<string> ReferenceAssemblies { get; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
    {
        "Microsoft.AspNetCore.Mvc",
        "Microsoft.AspNetCore.Mvc.Abstractions",
        "Microsoft.AspNetCore.Mvc.ApiExplorer",
        "Microsoft.AspNetCore.Mvc.Core",
        "Microsoft.AspNetCore.Mvc.Cors",
        "Microsoft.AspNetCore.Mvc.DataAnnotations",
        "Microsoft.AspNetCore.Mvc.Formatters.Json",
        "Microsoft.AspNetCore.Mvc.Formatters.Xml",
        "Microsoft.AspNetCore.Mvc.Localization",
        "Microsoft.AspNetCore.Mvc.Razor",
        "Microsoft.AspNetCore.Mvc.Razor.Host",
        "Microsoft.AspNetCore.Mvc.TagHelpers",
        "Microsoft.AspNetCore.Mvc.ViewFeatures"
    };

    public static IEnumerable<ApplicationPart> DiscoverAssemblyParts(string entryPointAssemblyName)
    {
        var entryAssembly = Assembly.Load(new AssemblyName(entryPointAssemblyName));
        var context = DependencyContext.Load(Assembly.Load(new AssemblyName(entryPointAssemblyName)));

        return GetCandidateAssemblies(entryAssembly, context).Select(p => new AssemblyPart(p));
    }

    internal static IEnumerable<Assembly> GetCandidateAssemblies(Assembly entryAssembly, DependencyContext dependencyContext)
    {
        if (dependencyContext == null)
        {
            // Use the entry assembly as the sole candidate.
            return new[] { entryAssembly };
        }

        return GetCandidateLibraries(dependencyContext)
            .SelectMany(library => library.GetDefaultAssemblyNames(dependencyContext))
            .Select(Assembly.Load);
    }

    // Returns a list of libraries that references the assemblies in <see cref="ReferenceAssemblies"/>.
    // By default it returns all assemblies that reference any of the primary MVC assemblies
    // while ignoring MVC assemblies.
    // Internal for unit testing
    internal static IEnumerable<RuntimeLibrary> GetCandidateLibraries(DependencyContext dependencyContext)
    {
        if (ReferenceAssemblies == null)
        {
            return Enumerable.Empty<RuntimeLibrary>();
        }

        var candidatesResolver = new CandidateResolver(dependencyContext.RuntimeLibraries, ReferenceAssemblies);
        return candidatesResolver.GetCandidates();
    }

    private class CandidateResolver
    {
        private readonly IDictionary<string, Dependency> _dependencies;

        public CandidateResolver(IReadOnlyList<RuntimeLibrary> dependencies, ISet<string> referenceAssemblies)
        {
            _dependencies = dependencies
                .ToDictionary(d => d.Name, d => CreateDependency(d, referenceAssemblies), StringComparer.OrdinalIgnoreCase);
        }

        private Dependency CreateDependency(RuntimeLibrary library, ISet<string> referenceAssemblies)
        {
            var classification = DependencyClassification.Unknown;
            if (referenceAssemblies.Contains(library.Name))
            {
                classification = DependencyClassification.MvcReference;
            }

            return new Dependency(library, classification);
        }

        private DependencyClassification ComputeClassification(string dependency)
        {
            Debug.Assert(_dependencies.ContainsKey(dependency));

            var candidateEntry = _dependencies[dependency];
            if (candidateEntry.Classification != DependencyClassification.Unknown)
            {
                return candidateEntry.Classification;
            }
            else
            {
                var classification = DependencyClassification.NotCandidate;
                foreach (var candidateDependency in candidateEntry.Library.Dependencies)
                {
                    var dependencyClassification = ComputeClassification(candidateDependency.Name);
                    if (dependencyClassification == DependencyClassification.Candidate ||
                        dependencyClassification == DependencyClassification.MvcReference)
                    {
                        classification = DependencyClassification.Candidate;
                        break;
                    }
                }

                candidateEntry.Classification = classification;

                return classification;
            }
        }

        public IEnumerable<RuntimeLibrary> GetCandidates()
        {
            foreach (var dependency in _dependencies)
            {
                if (ComputeClassification(dependency.Key) == DependencyClassification.Candidate)
                {
                    yield return dependency.Value.Library;
                }
            }
        }

        private class Dependency
        {
            public Dependency(RuntimeLibrary library, DependencyClassification classification)
            {
                Library = library;
                Classification = classification;
            }

            public RuntimeLibrary Library { get; }

            public DependencyClassification Classification { get; set; }

            public override string ToString()
            {
                return $"Library: {Library.Name}, Classification: {Classification}";
            }
        }

        private enum DependencyClassification
        {
            Unknown = 0,
            Candidate = 1,
            NotCandidate = 2,
            MvcReference = 3
        }
    }
}

答案 1 :(得分:-1)

您可以通过以下内容获取程序集:

libManager.GetLibraries()
   .Where(x => x.Identity.Type == LibraryType.Project)
   .Select(x => Assembly.Load(new AssemblyName(x.Identity.Name))).ToList();

这应该获取所有项目引用并加载程序集