'An exception that occurs after.NET Core is compiled to a single file

There's code in the program that looks something like this

            var references = dbType.Assembly
            .GetReferencedAssemblies()
            .Select(e => MetadataReference.CreateFromFile(Assembly.Load(e).Location))
            .Union(new MetadataReference[]
            {
                MetadataReference.CreateFromFile(Assembly.Load("netstandard").Location),
                MetadataReference.CreateFromFile(Assembly.Load("System.Runtime").Location),
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(ModelSnapshot).Assembly.Location),
                MetadataReference.CreateFromFile(dbType.Assembly.Location)
            });

Under normal circumstances, debugging and publishing are fine, but when I compile the program into a file, an exception occurs

enter image description here

When using CreateFromStream

enter image description here

When using MetadataReference. CreateFromFile

enter image description here

How should this problem be solved?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source