Search code examples
c#entity-frameworkxamarinxamarin.iosxamarin-linker

Xamarin iOS Linker Issue


I have an application that has an android and ios project with shared code. The android application works perfectly and uses the SDK Only linker. I also have a Linker file to enforce linker not removing used functions - Which is a fairly large file and I've also tried adding a bunch of preserve attributes. The same situation in the iOS project, however, doing the same thing doesn't work. It removes something from EF and throws this exception:

"The type initializer for 'Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions' threw an exception. ---> System.InvalidOperationException"

This whole codebase works perfectly fine without the linking. I've added a bunch of mtouch arguments to try and skip it but that ain't working either.

--linkskip=Microsoft.EntityFrameworkCore 
--linkskip=Microsoft.EntityFrameworkCore.Design 
--linkskip=Microsoft.EntityFrameworkCore.Sqlite 
--linkskip=Microsoft.EntityFrameworkCore.Tools 
--linkskip=Interactive.Async 
--linkskip=Remotion.Linq 
--linkskip=System.Data 
--linkskip=System.Collections.Immutable 
--linkskip=System.Diagnostics.DiagnosticSource 
--linkskip=Microsoft.Extensions.Logging 
--linkskip=System.Interactive.Async 
--linkskip=Microsoft.EntityFrameworkCore.Abstractions 
--linkskip=Microsoft.Extensions.DependencyInjection 
--linkskip=Microsoft.Extensions.Caching.Memory 
--linkskip=System.ComponentModel.Annotations

I'm about to add every DLL in the NuGet repository which will make the Linker useless. Is this a bug for the iOS Linker or what am I missing here? I've read the documentation https://learn.microsoft.com/en-us/xamarin/ios/deploy-test/linker?tabs=windows and searched everywhere on the internet and nothing comes up. Any help is appreciated.

Thanks,


Solution

  • If you are using "Link SDK assemblies only" option on iOS project, then all of those --linkskip arguments won't do anything as only the SDK assemblies are being linked. So the EntityFrameworkCore might be using something that is being linked away that is in the Xamarin.iOS SDK assemblies. System.Linq is part of the SDK and adds extension methods for many database types.

    So I think you may be hitting this issue: https://github.com/xamarin/xamarin-macios/issues/3394

    If so, try putting:

    [assembly: Preserve (typeof (System.Linq.Queryable), AllMembers = true)]
    

    in the Main.cs file of the iOS project outside of the namespace, e.g.:

    using UIKit;
    
    [assembly: Preserve (typeof (System.Linq.Queryable), AllMembers = true)]
    namespace
    {
        ...
    }