Search code examples
asp.netreact-nativeasp.net-coresignalr

React-Native Signalr connection works in debug mode, fails in Release mode


I have a ASP.NET 6.0 Core Web Api with Signalr published to IIS on a Windows Server 2017. Then I have a React-Native mobile app that's using @microsoft/signalr to connect to the SignalR hub. When the mobile app is running in DEBUG mode both via the android emulator and my android phone, it can connect to the Signalr with no issues. Everything works perfectly. Now when I try running the same application in release mode, it doesn't work and it can't connect. Please any ideas/help would be appreciated.

Program.cs Main


            builder.Services.AddCors(options =>
            {
                options.AddDefaultPolicy(builder =>
                {
                    builder.AllowAnyOrigin()
                           .AllowAnyMethod()
                           .AllowAnyHeader();
                           //.AllowCredentials()
                           //.SetIsOriginAllowed((host) => true);
                });
            }
            );

            // Add services to the container.
            builder.Services.AddControllers()
                            .AddJsonOptions(options =>
                            {
                                options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
                                //options.JsonSerializerOptions.PropertyNamingPolicy = null;
                            });


            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();
            builder.Services.AddSignalR();
            builder.Logging.ClearProviders();
            Serilog.ILogger logger = new AppLogger(builder.Configuration).Logger;
            builder.Logging.AddSerilog(logger, true);

            var app = builder.Build();
            
            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment() || app.Environment.IsProduction())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            //app.UseHttpsRedirection();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseCors();
            app.UseWebSockets();

            app.MapControllers();
            app.MapHub<FacilityHub>("/facility-hub");

            app.Run();

I ran the following commands see the logs while the app is running in release mode.

npx react-native log-android

npx react-native run-android --mode release

Here is the error in the logs when mobile app is running is release mode. Release mode error

Here is the error in the logs when mobile app is running is debug mode. Development mode error


Solution

  • In Release mode, Android has a limit on insecure http requests. We just have to add some configuration to the configuration file.

    AndroidManifest.xml file:

    1. Make sure you have added permission to use the network.

      <uses-permission android:name="android.permission.INTERNET" />
      
    2. Set the following properties in the application tag:

      <application
          ...
          android:usesCleartextTraffic="true"
          ...>
          ...
       </application>
      
    3. If you want to use the deprecated HttpClient in your native code to send http requests, you need to add the following tags:

      <uses-library android:name="org.apache.http.legacy" android:required="false"/>
      

    Related link: Behavior changes: apps targeting API level 28+