AddRefitClient DryIoc and IServiceProvider on Prism for Xamarin.Forms

864 Views Asked by At

I am trying to follow DryIoc and IServiceProvider on Prism for Xamarin.Forms (DryIoc.Microsoft.DependencyInjection) however I am using RefitClient for IHttpClientFactory

containerRegistry.RegisterServices(services =>
            {
                services.AddTransient<HttpLoggingHandler>();
                services.AddTransient<AuthorizationDelegatingHandler>();

                services.AddRefitClient<IMyApi>()
                    .ConfigureHttpClient(c =>
                        c.BaseAddress =
                            new Uri(apiBaseUrl))
                    .AddHttpMessageHandler<AuthorizationDelegatingHandler>()
                    .AddHttpMessageHandler<HttpLoggingHandler>()
                    .AddTransientHttpErrorPolicy(builder => builder.WaitAndRetryAsync(new[]
                    {
                        TimeSpan.FromMilliseconds(300),
                        TimeSpan.FromSeconds(600),
                        TimeSpan.FromSeconds(800)
                    }))
                    .AddTransientHttpErrorPolicy(
                        p => p.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30)));
            });

I have added

protected override IContainerExtension CreateContainerExtension() => PrismContainerExtension.Current;

When I try to make a request with IMyApi.

BaseAddress must be set on the HttpClient instance
  at Refit.RequestBuilderImplementation+<>c__DisplayClass14_0`2[T,TBody].<BuildCancellableTaskFuncForMethod>b__0 (System.Net.Http.HttpClient client, System.Threading.CancellationToken ct, System.Object[] paramList) [0x00030] in /_/Refit/RequestBuilderImplementation.cs:236
2

There are 2 best solutions below

0
On

I'm using Unity rather than Dryloc but the solution is the same.

The key seems very much to depend on installing the correct packages. Install ONLY these:

Prism.Forms.Extended

Prism.Unity.Extensions

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    //Omitted Code

    containerRegistry.RegisterServices(serviceCollection =>
    {
        serviceCollection.AddHttpClient<IApiService, ApiService>(client =>
        {
            client.BaseAddress = new Uri("Your Address Here");
        });
    });
}
public class ApiService : IApiService
{
    Func<IApi> _createClient;

    public ApiService(HttpClient client)
    {
        _createClient = () => 
        {
            return RestService.For<IApi>(client, new RefitSettings
            {
                ContentSerializer = new NewtonsoftJsonContentSerializer()
            });
        }
    }

    public IApi GetApi()
    {
        return new Lazy<IApi>(() => _createClient()).Value;
    }
}

In part also provided by this post:

https://xamgirl.com/consuming-restful-web-service-xamarin-forms-using-refit-part-2/

0
On

I personally had to deal with all of that and ended up by creating Apizr where auth and logging handlers are built in, policies are resolved from registry and many more features like connectivity test, caching or prioritizing. If it could help.