Could not load file or assembly or one of its dependencies. Reference assemblies should not be loaded for execution

Hi,

I have a web application on .net 4.7 which references Microsoft.Azure.ServiceBus

One of the assemblies it copies into the bin directory is System.Buffers.dll. When I run the webapp I get this error

[BadImageFormatException: Cannot load a reference assembly for execution.]

[BadImageFormatException: Could not load file or assembly ‘System.Buffers’ or one of its dependencies. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)]
System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +232
System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) +113
System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +23
System.Reflection.Assembly.Load(String assemblyString) +35
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +48

If I delete system.buffers then it loads ok.

Cheers,
John

Curious. I assume that copying System.Buffers.dll is a mistake, and it should be used from the main framework install instead. I’d like to investigate why this happens — do you have a simple testcase, or is simply adding a “Microsoft.Azure.ServiceBus:4.0.0” NuGet reference to a 4.7 project enough to reproduce the issue?

Thanks, logged as bugs://83426

Im not really sure what it should be doing, I guess not copying. Im running a console web app on my mac using mono and the app runs even though the file is present.

I also have it running in azure and it runs without that file in the bin folder.

Its not as simple as adding a reference, Im trying to produce a simpler test case

1 Like

It was as simple as adding a reference, I forgot to set copy local.
WebApplication10 (2).zip (782.5 KB)

This is the error

Cannot load a reference assembly for execution.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.BadImageFormatException: Cannot load a reference assembly for execution.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

`[BadImageFormatException: Cannot load a reference assembly for execution.]

[BadImageFormatException: Could not load file or assembly ‘System.Buffers’ or one of its dependencies. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)]
System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +232
System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) +113
System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +23
System.Reflection.Assembly.Load(String assemblyString) +35
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +48

[ConfigurationErrorsException: Could not load file or assembly ‘System.Buffers’ or one of its dependencies. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)]
System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +767
System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +256
System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +58
System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +278
System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +45
System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +137
System.Web.Compilation.BuildManager.ExecutePreAppStart() +140
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +827

[HttpException (0x80004005): Could not load file or assembly ‘System.Buffers’ or one of its dependencies. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +518
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +111
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +729`

1 Like

That project has a lot of missing references. it looks like you’re using an EBuild package reference for Microsoft.Azure.ServiceBus:[4.0.0], but old-style/VS-style package references or other dlls such as Microsoft.CodeDom.Providers.DotNetCompilerPlatform & co.

That said, I can reproduce the issue in a small/clean testcase when I just add the one Microsoft.Azure.ServiceBus reference. Investigating.

After taking a look, i see noting specific in System.Buffers that would let me decide to not copy-local it, compared to other packages. The only thing i coukld tink of woukd be to hard-exclide any packages that start with System. (or any .dlls that do) from Copy-Local.

Could you confirm for me (because I’m on Mac, so whats missing here does count, per se, and also, I cant be sure if my minimal test app would trigger the right code paths) if the app runs fine if you delete all System.*.dll files from the output folder? IOW, does ever System.* named dll get found in the core framework?

thanx!
marc

I dont think we should be doing that.

Did you want an example that uses references inside system.buffers ?

I have a web app that uses ServiceBus with framework 4.7, it runs fine without system.buffers in the bin folder.

Probably not — bit it begs the question: how am I (is EBuild) supposed to know, from a set of some ~20 direct and indirect packages that are pulled in, which files should be copy-local’ed and which should not?

Thats also built with Elements? But yeah, the whole problem is that System.Buffers is in the bin folder, no? so it’d make sense that an app that doesnt have it works. Question is — why doesnt that one get it copied, also?

I found this page https://docs.microsoft.com/en-us/dotnet/standard/assembly/reference-assemblies

Says that ref assemblies are in the ref folder separate from the lib folder. They also use a ReferenceAssembly attribute.

Yes I built everything using Fire. I never discovered this problem because I was developing on my mac, running with mono and deploying to Azure. When deploying to azure I just copy the minimum required assemblies. ie I try and run the web app and if it complains about missing assemblies, I copy whatever it wants.

Think I got a solution, but its fairly involved, so I’ll keep it out of the upcoming official build, and send you a separate one later today.

What was the solution ?

Two parts, for one, I’ll reference .dlls for compile only from the ref folder, but copy local (and not use in the compile at all) only from the lib folder. Also, it seems I should not (at least for lib, not sure about ref) fall back to netstandard2.0, when there’s no net4x folder (coz that too brings back the Buffer .dll)

with the current setup I get:

                  Reference: /Users/mh/Code/Elements/Bin/References/Echoes/NET/Echoes.dll
                  Reference: /Users/mh/Code/Elements/Bin/References/Echoes/NET/Elements.dll
                  Reference: /Users/mh/Code/Fire Support/_NETFramework/v4.7/mscorlib.dll
                  Reference: /Users/mh/Code/Fire Support/_NETFramework/v4.7/System.Core.dll
                  Reference: /Users/mh/Code/Fire Support/_NETFramework/v4.7/System.Data.DataSetExtensions.dll
                  Reference: /Users/mh/Code/Fire Support/_NETFramework/v4.7/System.Data.dll
                  Reference: /Users/mh/Code/Fire Support/_NETFramework/v4.7/System.dll
                  Reference: /Users/mh/Code/Fire Support/_NETFramework/v4.7/System.Xml.dll
                  Reference: /Users/mh/Code/Fire Support/_NETFramework/v4.7/System.Xml.Linq.dll
                  Reference: /Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.buffers/4.5.0/ref/net45/System.Buffers.dll
                  Reference: /Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.io/4.3.0/ref/net462/System.IO.dll
                  Reference: /Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.net.http/4.3.4/ref/net46/System.Net.Http.dll
                  Reference: /Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.net.websockets.client/4.3.2/ref/net46/System.Net.WebSockets.Client.dll
                  Reference: /Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.net.websockets/4.3.0/ref/net46/System.Net.WebSockets.dll
                  Reference: /Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.numerics.vectors/4.5.0/ref/net46/System.Numerics.Vectors.dll
                  Reference: /Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.runtime.compilerservices.unsafe/4.6.0/ref/netstandard2.0/System.Runtime.CompilerServices.Unsafe.dll
                  Reference: /Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.runtime.serialization.primitives/4.3.0/ref/net46/System.Runtime.Serialization.Primitives.dll
                  Reference: /Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.runtime/4.3.1/ref/net462/System.Runtime.dll
                  Reference: /Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.security.cryptography.algorithms/4.3.1/ref/net463/System.Security.Cryptography.Algorithms.dll
                  Reference: /Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.security.cryptography.encoding/4.3.0/ref/net46/System.Security.Cryptography.Encoding.dll
                  Reference: /Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.security.cryptography.primitives/4.3.0/ref/net46/System.Security.Cryptography.Primitives.dll
                  Reference: /Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.security.cryptography.x509certificates/4.3.2/ref/net461/System.Security.Cryptography.X509Certificates.dll
                  Reference: /Library/Frameworks/Mono.framework/Versions/5.16.0/lib/mono/gac/RemObjects.Elements.Cirrus/10.0.0.2458__3df3cad1b7aa5098/RemObjects.Elements.Cirrus.dll

D:             Generated Files for 'ServiceBusNuGetTest' target 'Echoes':
D:               Echoes.dll (/Users/mh/Code/Elements/Bin/References/Echoes/NET/Echoes.dll)
D:               Echoes.dll.mdb (/Users/mh/Code/Elements/Bin/References/Echoes/NET/Echoes.dll.mdb)
D:               Echoes.pdb (/Users/mh/Code/Elements/Bin/References/Echoes/NET/Echoes.pdb)
D:               Elements.dll (/Users/mh/Code/Elements/Bin/References/Echoes/NET/Elements.dll)
D:               Elements.dll.mdb (/Users/mh/Code/Elements/Bin/References/Echoes/NET/Elements.dll.mdb)
D:               Elements.pdb (/Users/mh/Code/Elements/Bin/References/Echoes/NET/Elements.pdb)
D:               Microsoft.Azure.Amqp.dll (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/microsoft.azure.amqp/2.4.3/lib/net45/Microsoft.Azure.Amqp.dll)
D:               Microsoft.Azure.Services.AppAuthentication.dll (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/microsoft.azure.services.appauthentication/1.3.1/lib/net461/Microsoft.Azure.Services.AppAuthentication.dll)
D:               Microsoft.IdentityModel.Clients.ActiveDirectory.dll (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/microsoft.identitymodel.clients.activedirectory/5.2.4/lib/net45/Microsoft.IdentityModel.Clients.ActiveDirectory.dll)
D:               Microsoft.IdentityModel.JsonWebTokens.dll (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/microsoft.identitymodel.jsonwebtokens/5.6.0/lib/net461/Microsoft.IdentityModel.JsonWebTokens.dll)
D:               Microsoft.IdentityModel.Logging.dll (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/microsoft.identitymodel.logging/5.6.0/lib/net461/Microsoft.IdentityModel.Logging.dll)
D:               Microsoft.IdentityModel.Tokens.dll (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/microsoft.identitymodel.tokens/5.6.0/lib/net461/Microsoft.IdentityModel.Tokens.dll)
D:               Newtonsoft.Json.dll (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/newtonsoft.json/12.0.3/lib/net45/Newtonsoft.Json.dll)
D:               ServiceBusNuGetTest.exe (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Obj/ServiceBusNuGetTest-07DA3FC117F5334FA018358D2FFEF386349307E1/Release/Echoes/ServiceBusNuGetTest.exe)
D:               System.Diagnostics.DiagnosticSource.dll (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.diagnostics.diagnosticsource/4.6.0/lib/net46/System.Diagnostics.DiagnosticSource.dll)
D:               System.IdentityModel.Tokens.Jwt.dll (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.identitymodel.tokens.jwt/5.6.0/lib/net461/System.IdentityModel.Tokens.Jwt.dll)
D:               System.IO.dll (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.io/4.3.0/lib/net462/System.IO.dll)
D:               System.Net.Http.dll (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.net.http/4.3.4/lib/net46/System.Net.Http.dll)
D:               System.Net.WebSockets.Client.dll (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.net.websockets.client/4.3.2/lib/net46/System.Net.WebSockets.Client.dll)
D:               System.Net.WebSockets.dll (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.net.websockets/4.3.0/lib/net46/System.Net.WebSockets.dll)
D:               System.Numerics.Vectors.dll (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.numerics.vectors/4.5.0/lib/net46/System.Numerics.Vectors.dll)
D:               System.Runtime.dll (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.runtime/4.3.1/lib/net462/System.Runtime.dll)
D:               System.Runtime.Serialization.Primitives.dll (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.runtime.serialization.primitives/4.3.0/lib/net46/System.Runtime.Serialization.Primitives.dll)
D:               System.Security.Cryptography.Algorithms.dll (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.security.cryptography.algorithms/4.3.1/lib/net463/System.Security.Cryptography.Algorithms.dll)
D:               System.Security.Cryptography.Algorithms.dll (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.security.cryptography.algorithms/4.3.1/runtimes/osx/lib/netstandard1.6/System.Security.Cryptography.Algorithms.dll)
D:               System.Security.Cryptography.Encoding.dll (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.security.cryptography.encoding/4.3.0/lib/net46/System.Security.Cryptography.Encoding.dll)
D:               System.Security.Cryptography.Primitives.dll (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.security.cryptography.primitives/4.3.0/lib/net46/System.Security.Cryptography.Primitives.dll)
D:               System.Security.Cryptography.X509Certificates.dll (/Users/mh/Library/Application Support/RemObjects Software/EBuild/Packages/NuGet/system.security.cryptography.x509certificates/4.3.2/lib/net461/System.Security.Cryptography.X509Certificates.dll)

Thanks. I’ll try with my solutions later on.

1 Like

20191202-181007-elements-develop, once done in ~1h, will have the change. extensive testing/feedback against any NuGet-using projects would be appreciated.

1 Like

Up now.

Unfortunately it breaks a lot of things.

This is an example with 4 .net standard projects. It looks like its not resolving

https://github.com/mosh/Moshine.MessagePipeline nuget references

If you create a new .net console app and add Newtonsoft.Json as a reference, its not seeing any of the namespaces in the referenced assembly.ConsoleApplication3.zip (33.8 KB)
Like this

Yeah, I was afraid this was to easy and clean. Will investigate further…

Yep. Netwonsoft.Json has only a lib folder no ref. Jesus, is there any rhyme or reason to how these are structured? :frowning: