Есть ли альтернатива для BuildManager.GetReferencedAssemblies () в ASP.NET Core? - PullRequest
0 голосов
/ 31 декабря 2018

В ASP.NET MVC 5 я использовал метод BuildManager.GetReferencedAssemblies(), чтобы получить все сборки в папке bin и загрузить их до начала зависимости, чтобы все dll были доступны для сканирования и внедрения.

Есть ли альтернатива в ASP.NET Core?

https://docs.microsoft.com/en-us/dotnet/api/system.web.compilation.buildmanager.getreferencedassemblies?view=netframework-4.7.2

Я попробовал этот код, но он запустился, выдает ошибки загрузки, например, исключение файла не найдено.

foreach (var compilationLibrary in deps.CompileLibraries)
{
    foreach (var resolveReferencePath in compilationLibrary.ResolveReferencePaths())
    {
        Console.WriteLine($"\t\tReference path: {resolveReferencePath}");
        dlls.Add(resolveReferencePath);
    }
}
dlls = dlls.Distinct().ToList();
var infolder = dlls.Where(x => x.Contains(Directory.GetCurrentDirectory())).ToList();
foreach (var item in infolder)
{
    try
    {
        Assembly.LoadFile(item);
    }
    catch (System.IO.FileLoadException loadEx)
    {

    } // The Assembly has already been loaded.
    catch (BadImageFormatException imgEx)
    {

    } // If a BadImageFormatException exception is thrown, the file is not an assembly.
    catch (Exception ex)
    {

    }
}

1 Ответ

0 голосов
/ 06 января 2019

Мне удалось создать решение, но я не уверен, что оно решает все случаи.

Я опубликую как «Работает на моей машине»

Разница в мажосахперейти из Assembly.LoadFile в AssemblyLoadContext.Default.LoadFromAssemblyPath

Я использовал эти тексты в качестве исследования

https://natemcmaster.com/blog/2018/07/25/netcore-plugins/ https://github.com/dotnet/coreclr/blob/v2.1.0/Documentation/design-docs/assemblyloadcontext.md

        var dlls = DependencyContext.Default.CompileLibraries
            .SelectMany(x => x.ResolveReferencePaths())
            .Distinct()
            .Where(x => x.Contains(Directory.GetCurrentDirectory()))
            .ToList();
        foreach (var item in dlls)
        {
            try
            {
                AssemblyLoadContext.Default.LoadFromAssemblyPath(item);
            }
            catch (System.IO.FileLoadException loadEx)
            {
            } // The Assembly has already been loaded.
            catch (BadImageFormatException imgEx)
            {
            } // If a BadImageFormatException exception is thrown, the file is not an assembly.
            catch (Exception ex)
            {
            }
        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...