-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to load assembly in an application that publishes as a single file? #45309
Comments
Tagging subscribers to this area: @vitek-karas, @agocke, @CoffeeFlux Issue DetailsDescriptionHi, I have an application that uses Mono.Cecil and AssemblyDefinition. I take the loaded assembly (System.Reflection.Assembly) and create a definition by Assembly.Location. Unfortunately, the PublishSingleFile mode loads references from resources instead of a disk. In this case the Assembly.Location is null and I can't found any other way to create the definition. The Mono's AssemblyDefinition can be created from the stream, but the NetCore\Net5 has no BinnaryFormatter. I've used the internal method Assembly.GetRawBytes and it also removed from the new version of the framework. Could you give me a way how can I get bytes[]/stream/location for loaded assembly? Configuration
Regression?Other information
|
You can try to use |
Hi, @jkotas. Thanks for a super quick answer. It makes my day. But I faced with the BadImageFormatException when I getting MetadaReader. Please take a look code. What I do wrong? private static unsafe void ReadAssembly()
{
var asm = typeof(JsonConvert).Assembly; //from Newtonsoft.Json package
asm.TryGetRawMetadata(out byte* blob, out int length);
byte[] buffer = new byte[length];
Marshal.Copy((IntPtr)blob, buffer, 0, length);
using var stream = new MemoryStream(buffer);
var reader = new PEReader(stream);
var meta = reader.GetMetadataReader(); // throws System.BadImageFormatException: 'Image is too small.'
} The StackTrace: |
Try this:
|
It works great. Thank you a lot! |
Hi, @jkotas the Mono.Cecil's AssemblyDefinition needs the whole assembly instead of metadata only. With DOSHeader, PEFileHeader, and other info. Is there any way to get the whole assembly's bytes? And how the references' assemblies are distributed in the "SingleFile" mode? I mean, I can load assembly myself if it shipped in resources of the application or any other known place. |
Yes, you can add the reference assemblies as resources yourself. |
Thank you. But I can't do it, could you advise another solution\workaround? We develop a third party that is used by other developers. In the projects of our clients, there are NuGet packages to our components. Some assemblies of these packages will be patched in runtime by our code (I told about it in the first message). The new behavior of .NET 5 when the location of assembly is null - critical breaking change for our feature. |
You can set Also, we are likely to introduce more optimized form factors going forward that may be missing the IL or metadata completely. https://github.com/dotnet/designs/blob/main/accepted/2020/form-factors.md talks about it in more details. If your feature depends on IL and metadata to be present and patched at runtime, it will not be compatible with these form factors. |
Thank you, Jan. You really help me. Also, we'll research a new approach of form factors to improve our code in the future. |
Description
Hi, I have an application that uses Mono.Cecil and AssemblyDefinition. I take the loaded assembly (System.Reflection.Assembly) and create a definition by Assembly.Location. Unfortunately, the PublishSingleFile mode loads references from resources instead of a disk. In this case the Assembly.Location is null and I can't found any other way to create the definition. The Mono's AssemblyDefinition can be created from the stream, but the NetCore\Net5 has no BinnaryFormatter. I've used the internal method Assembly.GetRawBytes and it also removed from the new version of the framework. Could you give me a way how can I get bytes[]/stream/location for loaded assembly?
Configuration
Regression?
Other information
The text was updated successfully, but these errors were encountered: