Skip to content
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

BamlLocalizer throws exception reading BAML streams in .NET Core #7245

Open
ericcdub opened this issue Nov 1, 2022 · 5 comments
Open

BamlLocalizer throws exception reading BAML streams in .NET Core #7245

ericcdub opened this issue Nov 1, 2022 · 5 comments
Labels
issue-type-tracking-external To track dependencies with other teams or repos

Comments

@ericcdub
Copy link

ericcdub commented Nov 1, 2022

  • .NET Core Version: 6.0
  • Win 10 22H2
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: No

Problem description: I migrated a .NET Framework 4.8 class library to .NET 6.0 using the .NET upgrade assistant. The library uses BamlLocalizer to extract BAML streams from .NET Framework Assemblies which use WPF. After the update, the library can't read BAML streams in .NET 6 assemblies any more as BamlLocalizer throws a FileNotFoundException.

Actual behavior: a FileNotFoundException upon instantiation of BamlLocalizer:

  at System.Reflection.RuntimeAssembly.InternalLoad(ObjectHandleOnStack assemblyName, ObjectHandleOnStack requestingAssembly, StackCrawlMarkHandle stackMark, Boolean throwOnFileNotFound, ObjectHandleOnStack assemblyLoadContext, ObjectHandleOnStack retAssembly)
   at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, RuntimeAssembly requestingAssembly, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, AssemblyLoadContext assemblyLoadContext) in /_/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs:line 347
   at System.Reflection.Assembly.Load(String assemblyString) in /_/src/coreclr/System.Private.CoreLib/src/System/Reflection/Assembly.CoreCLR.cs:line 21
   at System.Windows.Markup.BamlReader.ReadAssemblyInfoRecord()
   at System.Windows.Markup.BamlReader.ReadNextRecord()
   at System.Windows.Markup.BamlReader.Read()
   at MS.Internal.Globalization.BamlResourceDeserializer.LoadBamlImp(Stream bamlSteam)
   at System.Windows.Markup.Localizer.BamlLocalizer..ctor(Stream source, BamlLocalizabilityResolver resolver, TextReader comments)

Expected behavior: BamlLocalizer instantiates correctly and I am able to extract a BamlLocalizationDictionary from the BamlLocalizer instance.

Minimal repro: Create a .NET 6 project which loads an assembly containing BAML steams, and supply one of those streams when instantiating BamlLocalizer. An exception will be thrown.

I have read elsewhere that the LocBaml utility doesn't work either when upgraded to .NET 6 https://tomaskohl.com/code/2020-06-24/localizing-wpf-app-on-net-core/ and there is an open issue here to port it to .NET core here microsoft/WPF-Samples#85 but it would be good to hear this from the horse's mouth as it were.

@lindexi
Copy link
Contributor

lindexi commented Nov 2, 2022

Reference: #1480 #6477

@ericcdub
Copy link
Author

ericcdub commented Nov 8, 2022

In the end, I managed to extract BAML from an assembly by calling AssemblyLoadContext.EnterContextualReflection(myAssembly);

This will cause the runtime to use the assembly's own load context as the reflection-only context, meaning BamlLocalizer will try to load the assembly into that same context. I'm not sure if this would have unintended side effects, but so far so good.

It would also tally with what I read here.

@Kuldeep-MS Kuldeep-MS self-assigned this Nov 9, 2022
@Kuldeep-MS
Copy link
Contributor

Kuldeep-MS commented Nov 9, 2022

@ericcdub - Can you please attach the repro for the issue?

@Kuldeep-MS Kuldeep-MS added needs more information Not enough information has been provided. Please share more detail as requested and removed Investigate Requires further investigation by the WPF team. labels Nov 9, 2022
@ericcdub
Copy link
Author

ericcdub commented Nov 10, 2022

@ericcdub - Can you please attach the repro for the issue?

Hi @Kuldeep-MS

The issue reproduces when using the LocBaml tool as described in this help page https://learn.microsoft.com/en-us/dotnet/desktop/wpf/advanced/how-to-localize-an-application.

LocBaml uses BinaryLocalizer in ResourceGenerator.GenerateBamlStream, but is not AssemblyLoadContext-aware so it throws the same exception

The LocBaml project is still targeting .NET Framework, so either create a new .NET project and copy the source in or else use the .NET Upgrade assistant to migrate it.

Let me know if you need any more info.

Thanks

@harshit7962
Copy link
Member

This issue is actually blocked on unavailability of several methods from ModuleBuilder and AssemblyBuilder which prevents us to create methods that can actually emit the correct target assemblies.

@harshit7962 harshit7962 added issue-type-tracking-external To track dependencies with other teams or repos and removed needs more information Not enough information has been provided. Please share more detail as requested labels Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue-type-tracking-external To track dependencies with other teams or repos
Projects
None yet
Development

No branches or pull requests

6 participants