Issue using WPF control library with 1.6-preview5

112 views
Skip to first unread message

nate_work barish

unread,
Aug 10, 2022, 4:30:07 PM8/10/22
to Excel-DNA
In testing ExcelDna 1.6-preview5 building for net6 I ran into an issue that referenced WPF control libraries fail to load with an assembly not found exception.

In this example project you get the error when clicking "Show Custom Pane" in the toolbar.

The example works fine if you revert to 1.5.1 and net48.

Govert van Drimmelen

unread,
Aug 10, 2022, 5:09:47 PM8/10/22
to exce...@googlegroups.com

--------------------------------------------------

Excel-DNA is now registered on GitHub Sponsors.

By signing up for a monthly contribution you encourage further development and support.

--------------------------------------------------

 

This is a case where the .NET libraries don’t deal well with the “AssemblyLoadContexts” that were introduced in .NET Core. When the baml content from WPF is compiled at runtime, the assembly resolution goes wrong unless you set up the “Contextual Reflection” context in advance.

 

To deal with this you need to wrap such critical places like this:

 

        public MyUserControl()

        {

            _control = new ElementHost();

            _control.Location = new System.Drawing.Point(0, 0);

            _control.Dock = DockStyle.Fill;

           

            using (var ctx = System.Runtime.Loader.AssemblyLoadContext.EnterContextualReflection(this.GetType().Assembly))

            {

                var control = new MyWpfControl();

                _control.Child = control;           

                Controls.Add(_control);

            }

        }

 

The .NET developers have decided not to change the requirement to insert this magic into your code in places that can trigger some code loading calls at runtime. And you cannot know what code inside another library might require this. This is one way in which .NET 6 provides significantly less isolation than .NET Framework, by design.

 

More reading here:

runtime/AssemblyLoadContext.ContextualReflection.md at main · dotnet/runtime (github.com)

AssemblyLoadContext: requiring full cooperation to stay "inside" is fundamentally flawed · Issue #45285 · dotnet/runtime (github.com)

Developers can create scopes to support load/unload scenarios because AssemblyLoadContext is supported throughout .NET · Issue #43544 · dotnet/runtime (github.com)

Returning Assembly from collectible LoadAssemblyContext via AssemblyResolve event handler · Issue #49132 · dotnet/runtime (github.com)

 

-Govert

--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/0fbb9a59-399c-426d-8101-66314d9f5bc3n%40googlegroups.com.

nate_work barish

unread,
Aug 10, 2022, 11:11:47 PM8/10/22
to Excel-DNA
Thanks Govert worked perfect.

The removal of AppDomain in core looks to have certainly made things difficult.

I very much appreciate all your work on  ExcelDna.  Going to ask my work to buy a sponsorship.

Reply all
Reply to author
Forward
0 new messages