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

[WinUI3.0 Desktop(App and ClassLibrary)] A UserControl in a ClassLibrary can't be dynamically loaded by a custom AssemblyLoadContext class #3888

Closed
SunnyWang1012 opened this issue Jan 5, 2021 · 14 comments
Labels
product-winui3 WinUI 3 issues

Comments

@SunnyWang1012
Copy link

I create several projects and the relations are as following:
structure

In short, I want to use WinUI 3.0 Desktop App to dynamically load a ClassLibrary named HelloPlugin.dll.

  • Both App and HelloPlugin.dll add reference to an interface "ICommand" in PluginBase.dll
  • HelloPlugin.dll implements the "ICommand" interface which includes a field of UserControl.
  • HelloPlugin.dll add reference to another ClassLibrary named DataProvider.dll
  • I implement a custom AssemblyLoadContext in App to dynamically load HelloPlugin.dll and code snippets is as following
    code

[Bug]
I can successfully load HelloPlugin.dll in App and activate as ICommand and some fields leveraging DataProvider.dll are also loaded successfully.
However, when I invoke to get that UserControl field, it crashes at UserControl's constructor show as followings
error1
error2

Can you help to see if I miss anything in order to load resource of UserControl?

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Jan 5, 2021
@ranjeshj ranjeshj added product-winui3 WinUI 3 issues and removed needs-triage Issue needs to be triaged by the area owners labels Jan 6, 2021
@ranjeshj
Copy link
Contributor

ranjeshj commented Jan 6, 2021

@jevansaks @MikeHillberg any thoughts ?

@jevansaks
Copy link
Member

The error message in the output is "NamedResource Not found", coming from MRM.dll. That sounds like there's a path to an asset or something that's being requested which can't be resolved by MRT.

@SunnyWang1012 look closer at the HelloUserControl.xaml (or share more of the project for us to take a look at) to see where there might be a problem with an ms-appx or ms-resource URI in that file.

@SunnyWang1012
Copy link
Author

SunnyWang1012 commented Jan 6, 2021

@jevansaks The following screenshots shows the details of HelloUserControl.xaml, it actually doesn't have any content yet. I create that WinUI 3.0 UserControl by "Add New Item" to HelloPlugin project. Do you know if there is any setting I need to do in order to let App load this UserControl correctly?
UserControlCS
HelloControlXaml

@ValerCheck
Copy link

ValerCheck commented Jan 6, 2021

@SunnyWang1012 possibly I have addressed similar issue for dotnet/wpf repository. I may be wrong assuming this one is the same, but I had resource not found exception when was trying to load views from different plugins. Issue was closed and the answer was that it should be fixed in the scope of dotnet/wpf but not dotnet/runtime. The issue was found for views shared between multiple plugins.

@SunnyWang1012
Copy link
Author

SunnyWang1012 commented Jan 7, 2021

Thanks @ValerCheck for your information. In my case, I think the dll assembly loaded successfully except the resource (xaml..etc) stuffs.
I disabled "JustMyCode" setting and found the exception occurs inside "this.InitializeComponent()" of HelloUserControl.xaml.cs. The following is the screenshot:
error

I try to add HelloPlugin as reference project to App project, then it will be no problem. It seems that when dynamically loading plugins, I miss some "operation" in order to load the related resource as well...

@jevansaks
Copy link
Member

That makes sense ... but without a repro project we can't help too much. In general assets should get carried through <ProjectReference .../> in csproj/vcxproj but there may be some more knobs you need to set.

@SunnyWang1012
Copy link
Author

@jevansaks
I have uploaded my projects. For your reference. thanks.
https://github.com/SunnyWang1012/WinUILoadPlugin/blob/main/README.md

@SunnyWang1012
Copy link
Author

@MikeHillberg @jevansaks Any thought?

@SunnyWang1012
Copy link
Author

SunnyWang1012 commented Jan 18, 2021

Some updates...

  • I add HelloPlugin as a reference for WinUILoadApp, then the UserControl can be loaded successfully.
  • I do another POC named WpfLoadDll project to implement the same way in .NET framework. It can successfully load the UserControl. I also upload this project to my repository: link

@MikeHillberg
Copy link
Contributor

@stevenbrix, could this be an issue that when dynamically loading the DLL we're not finding the user control's XBF?

@stevenbrix
Copy link
Contributor

@SunnyWang1012 if you disable mixed mode debugging, does this error still persist? I'm wondering if this is the same issue as #3893

@stevenbrix
Copy link
Contributor

@alwu for FYI

@SunnyWang1012
Copy link
Author

SunnyWang1012 commented Jan 21, 2021

@stevenbrix After reading the issue #3893, I try to disable winrt::hresult_error in the exception setting and finally the UI control can be loaded successfully. Thank you so much :)

@stevenbrix
Copy link
Contributor

Awesome, I'm glad to hear you are unblocked! I'll close this as a dupe of #3893 then :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
product-winui3 WinUI 3 issues
Projects
None yet
Development

No branches or pull requests

6 participants