Xamarin Forms UWP - Can't Compile For .NET Native tool chain

1.5k views Asked by At

We have an app that compiles fine without the .NET Native tool chain, but when we click this checkbox, we get these compilation errors (Debug or Release):

*Severity   Code    Description Project File    Line    Suppression State
Error       at SerializationAssemblyGenerator.Program.GenerateDataContractSerializerHelper(IEnumerable`1 contracts, IEnumerable`1 jsonContracts, GeneratorSettings settings, String intermediateAssembly, IEnumerable`1 wcfSerializers) Adapt.Presentation.Helpdesk.UWP C:\Users\chris\.nuget\packages\microsoft.net.native.compiler\1.6.2\tools\Microsoft.NetNative.targets    691 
Error       at SerializationAssemblyGenerator.Program.Main(String[] args)   Adapt.Presentation.Helpdesk.UWP C:\Users\chris\.nuget\packages\microsoft.net.native.compiler\1.6.2\tools\Microsoft.NetNative.targets    691 
Error       ILT0032: Failed to compile serialization code. See the build log for error details. Adapt.Presentation.Helpdesk.UWP C:\Users\chris\.nuget\packages\microsoft.net.native.compiler\1.6.2\tools\Microsoft.NetNative.targets    691 
Error       at SerializationAssemblyGenerator.Program.GenerateDataContractSerializerHelperCode(IEnumerable`1 contracts, IEnumerable`1 jsonContracts, IEnumerable`1 wcfSerializers)  Adapt.Presentation.Helpdesk.UWP C:\Users\chris\.nuget\packages\microsoft.net.native.compiler\1.6.2\tools\Microsoft.NetNative.targets    691 
Error       at SerializationAssemblyGenerator.Program.AddKnownContractsLists(McgCodeTypeDeclaration container, ContractTables tables)   Adapt.Presentation.Helpdesk.UWP C:\Users\chris\.nuget\packages\microsoft.net.native.compiler\1.6.2\tools\Microsoft.NetNative.targets    691 
Error       at System.Collections.Generic.Dictionary`2.get_Item(TKey key)   Adapt.Presentation.Helpdesk.UWP C:\Users\chris\.nuget\packages\microsoft.net.native.compiler\1.6.2\tools\Microsoft.NetNative.targets    691 
Error       System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.   Adapt.Presentation.Helpdesk.UWP C:\Users\chris\.nuget\packages\microsoft.net.native.compiler\1.6.2\tools\Microsoft.NetNative.targets    691*    

There are a lot of errors in the output window. There's far too much to post here, but here are a couple of hints:

*1>  C:\AdaptSource\Xivic\Adapt.Presentation.Helpdesk.UWP\obj\x86\Debug\ilc\in\System.Reflection.Extensions.dll
1>  The assembly 'System.Reflection.Metadata.dll' is found in more than one folder.
1>  file:///C:/AdaptSource/Xivic/Adapt.Presentation.Helpdesk.UWP/obj/x86/Debug/ilc/in/System.Reflection.Metadata.dll*

I thought that perhaps there were DLLs floating around and it was getting confused about which one to use, but this still occurred even after a full Git clean.

This is the Default.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. -->


  </Application>
</Directives>

From everything I've come to understand, this should include all the types in our solution. Is this assumption correct?

I have a feeling that it has something to with specifying metadata for the types that exist in our project. There is an article here that hints at this: https://learn.microsoft.com/en-us/dotnet/framework/net-native/getting-started-with-net-native. But, I don't really understand what it is asking me to do, so I'm stuck.

4

There are 4 answers

4
Nico Zhu On

If your UWP app is referencing multiple assemblies (for example third party control libraries, or your application itself is split into multiple PCLs), Xamarin.Forms may be unable to load objects from those assemblies (such as custom renderers).

This might occur when using the Compile with .NET Native tool chain which is an option for UWP apps in the Properties > Build > General window for the project.

You can fix this by using a UWP-specific overload of the Forms.Init call in App.xaml.cs as shown in the code below (you should replace ClassInOtherAssembly with an actual class your code references):

// you'll need to add `using System.Reflection;`
List<Assembly> assembliesToInclude = new List<Assembly>();

//Now, add in all the assemblies your app uses
assembliesToInclude.Add(typeof (ClassInOtherAssembly).GetTypeInfo().Assembly);

//Also do this for all your other 3rd party libraries

Xamarin.Forms.Forms.Init(e, assembliesToInclude);
// replaces Xamarin.Forms.Forms.Init(e);

Add a reference to each assembly that is referenced by the app. For more you could refer to Target Invocation Exception.

0
Wilson Vargas On

This might occur when using the Compile with .NET Native tool chain which is an option for UWP apps in the Properties > Build > General window for the project.

Try to uncheck this option.

What Nico says should work according to this article.

0
dmc On

This has probably been resolved, but is your app by any chance doing some serialization using serialization classes like DataContractJsonSerializer or XmlSerializer?

I had similar build errors and I had to add these in my Dafault.rd.xml:

<Namespace Name="My.Namespace.Model" DataContractJsonSerializer="Required All" />
<Namespace Name="My.Namespace.Sql" XmlSerializer="Required All" />

For the uwp specific override on Forms.Init I didn't have to include any other assemblies except plugins assemblies like below (but this wasn't causing any build/compilation errors without it - only runtime issues like missing font-icons):

List<Assembly> assembliesToInclude = new List<Assembly>();
assembliesToInclude.Add(typeof(FormsPlugin.Iconize.IconButton).GetTypeInfo().Assembly);
assembliesToInclude.Add(typeof(FormsPlugin.Iconize.UWP.IconControls).GetTypeInfo().Assembly);
Xamarin.Forms.Forms.Init(e, assembliesToInclude);
0
Pavlo Datsiuk On

I got the issue with new VS19(in VS17 works fine) when I preparing my app to publish to Windows Store.

Release builds are making successfully but not for windows store.

Helps only git clean -xfd

I guess there is an issue in VS19 with clearing some files in the preparing build functionality for Windows Store.