title: Xamarin background disconnected signalr
body: <p>I am making an application in Xamarin Forms that uses the Signalr service to implement a chat. The chat works perfectly in the UWP version and in the Android emulator, so it does when I am debugging on my phone (Android), but when I disconnect the phone from the PC the chaos begins. The problem is that I think that when the application goes to the background, it disconnects from the Signalr server.</p>
  
  <p>I have tried automatic reconnection and even changing the times of ServerTimeout and KeepAliveInterval. But I have not been successful. It should be noted that where I live additionally there are major connectivity problems, but still my theory is when the application goes to Background.</p>
  
  <p>This is my code where I initialize my service (Im using a Singleton services).</p>
  
  <pre><code>       hubConnection = new HubConnectionBuilder()
          .WithUrl(URL, options =&gt;
          {
              options.AccessTokenProvider = () =&gt; Task.FromResult(_myAccessToken);
          })
          .WithAutomaticReconnect()
          //.WithAutomaticReconnect(new[] { TimeSpan.Zero, TimeSpan.Zero, TimeSpan.FromSeconds(10) })
          //.WithAutomaticReconnect(new RandomRetryPolicy())
          .Build();
  </code></pre>
  
  <p>This is my code to connect when the connection is closed</p>
  
  <pre><code>            hubConnection.Closed += async (error) =&gt;
              {
                  OnConnectionClosed?.Invoke(this, new MessageEventArgs("Connection closed...", 
                  string.Empty));
                  IsConnected = false;
  
                  await Task.Delay(new Random().Next(0, 5) * 1000);
                  try { await ReConnectAsync(); } catch (Exception ex) { Debug.WriteLine(ex); }
              };
  
  </code></pre>
  
  <p>This is my code to connect</p>
  
  <pre><code>        public async Task ReConnectAsync()
          {
              await ConnectAsync();
          }
  
          public async Task ConnectAsync()
          {
              if (IsConnected)
              {
                  return;
              }
  
              Debug.WriteLine(hubConnection.State);
  
              if (hubConnection.State== HubConnectionState.Disconnected)
              {
                  await hubConnection.StartAsync();
  
                  //CancellationToken cancellationToken = new CancellationToken();
                  //await ConnectWithRetryAsync(hubConnection, cancellationToken);
              }
  
              IsConnected = true;
          }
  </code></pre>
  
  <p>What else could I try to prevent it from disconnecting on Android or what will I be doing wrong in my code?</p>