I’m developing an UWP that works perfect in Debug mode, when I compiled it in Release Mode it compiles without an error, but when I run the app, I have a process the uses a function in a .Net Standard 2.0 library that throws a System.IO.FileNotFoundException System.Security. No metadata found for this assembly.
This is my rd.xml
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
<Application>
<!--
An Assembly element with Name="*Application*" applies to all assemblies in
the application package. The asterisks are not wildcards.
-->
<Assembly Name="*Application*" Dynamic="Required All" />
<!-- Add your application specific runtime directives here. -->
<Assembly Name="System.Runtime" Dynamic="Required All" />
<Assembly Name="System.Security" Dynamic="Required All" />
<Type Name="Windows.Foundation.TypedEventHandler{Microsoft.UI.Xaml.Controls.NavigationView,Microsoft.UI.Xaml.Controls.NavigationViewItemInvokedEventArgs}" MarshalObject="Public" />
<Type Name="Microsoft.UI.Xaml.Controls.NavigationView">
<Event Name="ItemInvoked" Dynamic="Required"/>
</Type>
<Type Name="System.Configuration.AppSettingsSection" Serialize="Required Public" />
</Application>
</Directives>
And the line that throws the error is this one.
Assembly System_Security_Assembly = Assembly.Load("System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
Even though this one works fine.
Assembly cripXmlAssembly = Assembly.Load("System.Security.Cryptography.Xml");
I’ve read about adding a directive to an RD.xml files but I think this applies to UWP, and in this case the Exception is being thrown by the Net Standard dll. Could someone please shed some light on this?
Here is a repro.
By discussion, we found that dynamic loading of assemblies is not supported in CoreRT(which is the runtime used when an app is compiled in Release with .NET native toolchain):
We know that .NET native is used for UWP apps when building in Release mode.
From this document:
.NET Native uses CoreRT in conjunction with the UTC compiler to provide native compilation for UWP apps.
And from this thread :
Unsupported features: Dynamic loading (e.g. Assembly.LoadFile), dynamic code generation (e.g. System.Reflection.Emit), Windows-specific interop (e.g. COM, WinRT)
Here also mentions currently this is not possible in CoreRT.
In fact, the only assemblies that can be "loaded" are those that are statically linked into the running native process.