iOS App Crashes only in Release using ServiceStack client in .Net MAUI

Good morning,
I have already read other answers for my same problem and tried to follow the directions given but I must have done something wrong because the problem seems to have worsened.
I have an assembly that contains all the templates I use for communication with servers. I have tried inserting

in the configuration file of the MAUI project but this causes the application to crash on start-up. I did not understand whether it is sufficient to enter the right TrimmerRootAssembly or whether there are also other operations to be performed.

I’ve been trying for days and don’t know what to do anymore.
Thank you very much,
Riccardo

iOS is not a supported platform, it’s No JIT restrictions on Release binaries breaks libraries that use reflection like serializers.

Best you can do is to not link or trim any assemblies that contain your serializable types, e.g. your ServiceModel .dll. Alternatively you could try enabling the interpreter in Release builds.

Something else you can try is using System.Text.Json serializer in JsonApiClient by configuring it in the client on Startup with:

ClientConfig.UseSystemJson = UseSystemJson.Always;

In which case your ServiceStack Server App should use Endpoint Routing so it’s also configured to use System.Text.Json APIs.

Otherwise you’d need to use libraries that specifically supports iOS/No JIT restrictions. If it only breaks in Release mode it’s because Maui uses an interpreter in development/debug builds.

Thanks for the advice and explanation.
Unfortunately nothing seems to work. Could you recommend libraries that support iOS/No JIT restrictions?

Thanks again

No don’t have any experience with Maui/iOS, but you’d basically need to look for Native AOT/reflection-free compatible libraries, something like source generation in System.Text.Json since it relies on code generation instead of runtime reflection.

Good morning,
I have tried applying all the suggestions you have written but it still doesn’t work. With some of those tips the app crashes immediately on startup.
Can you confirm that I have no choices but to use other libraries for communication between my app and the server?

Thank you for your support.

Right, you’re basically limited to libraries that don’t use reflection which typically suggests using a source-based or code-gen serializer. This thread suggests a lot of work was done in source generated System.Text.Json to make it AOT friendly:

I followed your advice and I am using RestSharp to make the connection between my MAUI app and my server. I normally use your EncryptedClient to connect. With RestSharp I can get the AuthenticateResponse correctly populated but when I then try to make a call I always get an unauthorised exception. Am I missing something? Do I need to pass the session id or token in some post parameter?

Thank you very much.

Haven’t got any chance to know what the issue is without any information about your Auth configuration, what provider you’re using to Authenticate, whether you’re using session based auth, whether session cookies are populated in subsequent requests, etc.

These are my startup configuration:

public AppHost() : base("MyService", typeof(Services).Assembly)
{
    var backEndEndPoint = ServerSettingsHelper.GetValue<string>(ServerSettingsKey.BackEnd_WebEndPoint);

    ServicePointManager.Expect100Continue = false;
    ServicePointManager.ReusePort = true;

    _rsaKeyPair = RsaUtils.CreatePublicAndPrivateKeyPair();
    GlobalPropertyServer.Instance().SymmetricKey = RandomHelper.GenerateString(12, RandomCharSet.Nums);
    _endPoints = new List<string>();
    _basePortWeb = ServerSettingsHelper.GetValue<int>(ServerSettingsKey.BasePortWeb);

    _endPoints.Add($"http://{backEndEndPoint}:{_basePortWeb}/");
    _endPoints.Add($"https://{backEndEndPoint}:{_basePortWeb + 1}/");
}

public override void Configure(Container container)
{
    Plugins.Add(new AuthFeature(() => new AuthUserSession(),
         new IAuthProvider[ ] {
         new JwtAuthProvider(AppSettings)
         {
             UseTokenCookie = false,
             SetBearerTokenOnAuthenticateResponse = true,
             RequireSecureConnection= false,
             AuthKey = AesUtils.CreateKey()
         },
         new CustomCredentialsAuthProvider()
         {
              CustomValidationFilter = customValidationFilter
         },
        })
    {
        IncludeDefaultLogin = false,

    });

    Plugins.Add(new EncryptedMessagesFeature
    {
        PrivateKeyXml = _rsaKeyPair.PrivateKey
    });

    Plugins.Add(new
        CorsFeature(allowedOrigins: "*",
        allowedMethods: CorsFeature.DefaultMethods,
        allowedHeaders: "*"
        ));
    SetConfig(new HostConfig
    {
        WriteErrorsToResponse = true,
        EnableOptimizations = true,
    });
}

If you need more information, I can write to you.

Since you’re using JWTs and you’re not using cookies are you passing in the JWT Bearer token in the Authorization HTTP Header on each request?

I am doing it in this way:

request.AddHeader(“BearerToken”, authenticateResponse.BearerToken);

is that correct?

No should be Authorization: Bearer <token>, see Rest Sharp JWT docs for how to set it.

Thanks a lot for your suggestions. It’s working.
I’m just noticing that I’m having trouble deserializing some classes when using class inheritance.
I only see this issue when using RestSharp, while it didn’t happen with ServiceStack. Is there any setting I can change to work around this?

Thanks again.

Unless you’re using a supported serializer I’d be surprised you’d be able to use serialization in iOS/NoJIT, you’d either have to live within the limitations of that serializer or derserialize it into a generic JSON document and use it to manually populate the types yourself.

Also I noticed another Native AOT compatible source generators JSON seiralization solution maintained by one of the developers of the .NET Team:

Personally I wouldn’t use .NET to build an iOS App, its NoJIT restrictions means you don’t know what .NET code or library will break in release/device builds. Flutter / Reactive Native / Kotlin Multiplatform or Swift wouldn’t suffer from these issues.