本文介绍了Xamarin.Forms的Prism上的AddRefitClient DryIoc和IServiceProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试关注DryIoc and IServiceProvider on Prism for Xamarin.Forms (DryIoc.Microsoft.DependencyInjection),但是我正在使用RefitClientIHttpClientFactory

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)));
            });

我已添加

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

当我尝试使用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

推荐答案

我使用的是Unity而不是Dryloc,但解决方案是相同的。

关键似乎在很大程度上取决于安装正确的包。仅安装这些:

棱镜表单扩展

Prism.Unity.Extensions

本帖子还提供了部分内容:

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

这篇关于Xamarin.Forms的Prism上的AddRefitClient DryIoc和IServiceProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 17:09