In my Xamarin app I get an exception on app startup on both Android and iOS.
"Cannot access a disposed object. Object name: 'MobileAuthenticatedStream'"
This seems to be happening after a background fetch has been run.
I am initializing my httpclientfactory like so
static void Init()
{
var host = new HostBuilder()
.ConfigureHostConfiguration(c =>
{
c.AddCommandLine(new string[] { $"ContentRoot={FileSystem.AppDataDirectory}" });
})
.ConfigureServices((c, x) =>
{
ConfigureServices(c, x);
})
.Build();
ServiceProvider = host.Services;
}
static void ConfigureServices(HostBuilderContext ctx, IServiceCollection services)
{
services.AddSingleton<Helpers.ClientService>();
services.AddHttpClient("webClient", client =>
{
client.DefaultRequestHeaders.Authorization = GenerateAuthHeader();
})
.AddPolicyHandler(GetRetryPolicy());
}
Then getting the client for use like
public class ClientService
{
IHttpClientFactory _httpClientFactory;
public ClientService(IHttpClientFactory factory)
{
_httpClientFactory = factory;
}
public HttpClient GetClient()
{
return _httpClientFactory.CreateClient(App.ClientName);
}
}
Helpers.ClientService service = App.ServiceProvider.GetService<Helpers.ClientService>();
HttpClient client = service.GetClient();
HttpResponseMessage response = await client.GetAsync(url);
On app start and resume I am re-running Init method.
Is this re-initializing the factory correctly, if not how should I implement this functionality