Mvvmlight Practice of UWP Development VI: Missingmetadataexception Solutions (Default.rd.xml configuration issues under. Net native)

Source: Internet
Author: User

Recently completed a UWP app, in the phone-side test release (release) when the application of strange forced shutdown, and the same application package on the PC side a little problem, and debug version in two platforms no problem, but the phone's release version has a problem. There is no way to log only the log of each step, by looking at the log found to be Suspensionmanager DataContractSerializer serialization throws a ArgumentNullException exception.

Common. NET Native Throw Exceptions:

Example 1:

Example 2:
HResult: -2146233088 TypeName:Newtonsoft.Json.JsonException, Newtonsoft.json, version=8.0.0.0, Culture=neutral, Publickeytoken=30ad4fe6b2a6aeed Message:Newtonsoft.Json.JsonException:Error creating ' nestedobjectjsonconverter '. ---> System.NullReferenceException:Object reference not set to a instance of an Object. At Newtonsoft.json.serialization.jsontypereflector.<>c.b__18_1 (Object param) at
例3:
Exception thrown: ‘System.AggregateException‘ in System.Private.Threading.dllException thrown: ‘System.ArgumentException‘ in System.Linq.Expressions.dllException thrown: ‘System.ArgumentException‘ in System.Linq.Expressions.dllException thrown: ‘System.ArgumentException‘ in System.Linq.Expressions.dllThe thread 0x2a30 has exited with code 0 (0x0).Exception thrown: ‘System.Reflection.MissingMetadataException‘ in System.Private.Reflection.Core.dllAdditional information: ‘Microsoft.Extensions.Caching.Memory.MemoryCacheOptions‘ is missing
Reason:
Because the mobile release version uses. NET native to compile the. NET native when compiling the source code into native code no longer has intermediate language (IL). This is also why the exception that occurs in the code compiled by. NET native does not appear in JIT-compiled code.
The. NET Native compilation Does the following things:
    Detailed Introduction to reference msdn:https://msdn.microsoft.com/zh-cn/library/dn807190 (v=vs.110). aspx
    • For some code paths, it replaces the code that relies on reflection and metadata with static native code.

    • If possible, it will attempt to eliminate all meta data.

    • It only contains implementation code that is actually called by the application in the final application set. This in particular has an impact on code in third-party libraries and. NET Framework class libraries. As a result, applications no longer rely on third-party libraries or complete. NET Framework class libraries; instead, the code in the current third-party and. NET Framework class libraries is local to the application.

    • It replaces the complete CLR with the refactoring runtime, which primarily contains the garbage collector. The refactoring runtime is in the application named Mrt100_app.dll Local library, and its size is only several hundred bytes. This may be because static links no longer require the common language runtime to perform multiple services.

Frequently Asked Questions action:

Because. NET native will link to the application only when it learns that the application actually called the implementation code, the application may not contain the metadata or implementation code that is required in the following operations. If the required metadata or implementation code is missing at run time, the application runs with Missingmetadataexception, Missingruntimeartifactexception, or Missinginteropdataexception exception.

    • Reflection.

    • Dynamic or late-bound invocation.

    • serialization and deserialization.

    • COM Interop.

Workaround:

In order for. NET native to compile without deleting the metadata or implementing code that we need, you need to configure the Default.rd.xml (runtime Directives) file to specify the metadata that the program requires.

File address such as:

The default configuration is:

<directives xmlns="Http://schemas.microsoft.com/netfx/2013/01/metadata"> <Application> <!--An Assembly element with Name="*application*"Applies to all assembliesinchThe application package.    The asterisks is not wildcards. -<assembly Name="*application*"Dynamic="Required All"/> <!--Add your application specific runtime directives here. -</Application></Directives>

In order to properly configure the recommended use of this tool: Missingmetadataexception Troubleshooter (http://dotnet.github.io/native/troubleshooter/type.html#)

such as configuring Suspensionmanager suspend interrupt Processing (DataContractSerializer serialization)

<directives xmlns="Http://schemas.microsoft.com/netfx/2013/01/metadata"> <Application> <!--An Assembly element with Name="*application*"Applies to all assembliesinchThe application package.    The asterisks is not wildcards. -<assembly Name="*application*"Dynamic="Required All"/><type name="testdemo.studentmodel " datacontractserializer= " Requiredpublic" /></Application></Directives>

MSDN Introduction Configuration

<directives xmlns="Http://schemas.microsoft.com/netfx/2013/01/metadata"><Application> <namespace name="Contoso.Cloud.AppServices"Serialize="Required Public"/> <namespace name="Contosoclient.viewmodels"Serialize="Required Public"/> <namespace name="Contosoclient.datamodel"Serialize="Required Public"/> <namespace name="Contoso.Reader.UtilityLib"Serialize="Required Public"/> <namespace name="System.Collections.ObjectModel"> <typeinstantiation name="ObservableCollection"Arguments="ContosoClient.DataModel.ProductItem"Serialize=" Public"/> <typeinstantiation name="readonlyobservablecollection"Arguments="ContosoClient.DataModel.ProductGroup"Serialize=" Public"/> </Namespace></Application></Directives>

Detailed node description and Configuration reference MSDN: Runtime directives (rd.xml) configuration https://msdn.microsoft.com/zh-cn/library/dn600639 (v=vs.110). aspx

Note the point:

DataContractSerializer, DataContractJsonSerializer, and XmlSerializer classes are somewhat special, some situations need to be configured in some cases without the need to configure Default.rd.xml files.
No configuration conditions are required: (the. Net native compiler automatically processes when using typeof to specify a type in a constructor)
New DataContractSerializer (typeof(T));

Configuration required: (Must be configured in Default.rd.xml file when specifying type using typeof outside of constructor)

typeofnew XmlSerializer (t);

or Suspensionmanager suspend interrupt processing.

New DataContractSerializer (typeof(dictionary<stringobject>), _knowntypes);

Or

New XmlSerializer (typeof(Teacher),                             newtypeof(Student),                                           typeof (Course),                                           typeof (location)});

Note:

Migrating Windows Store apps to. NET native recommends that you carefully read the MSDN instructions (https://msdn.microsoft.com/zh-cn/library/dn600634 (v=vs.110). aspx).

Mvvmlight Practice of UWP Development VI: Missingmetadataexception Solutions (Default.rd.xml configuration issues under. Net native)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.