DryIoc and IServiceProvider on Prism for Xamarin.Forms (DryIoc.Microsoft.DependencyInjection)

青春壹個敷衍的年華 提交于 2020-12-13 12:04:05

问题


I've got a Prism application with DryIoc as container. I'd like IHttpClientFactory to provide HttpClients to my typed clients, which are like this:

public class ExampleService : IExampleService
{
    private readonly HttpClient _httpClient;

    public RepoService(HttpClient client)
    {
        _httpClient = client;
    }

    public async Task<IEnumerable<string>> GetExamplesAsync()
    {
        // Code deleted for brevity.
    }
}

In App.xaml.cs I register my typed client so they can be injected in viewmodels with the following:

public partial class App

// ...

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    // Code deleted for brevity.
    containerRegistry.Register<IExampleService, ExampleService>();
}

And that's before trying to use IHttpClientFactory.

Now, to add it, we should AddHttpClient() on IServiceCollection. That's where I thought DryIoc.Microsoft.DependencyInjection was needed, so, still in App.xaml.cs, I wrote the following:

public partial class App

// ...

protected override IContainerExtension CreateContainerExtension()
{
    var services = new ServiceCollection();

    services.AddHttpClient<IExampleService, ExampleService>(c =>
    {
        c.BaseAddress = new Uri("https://api.example.com/");
    });

    var container = new Container(CreateContainerRules())
        .WithDependencyInjectionAdapter(services);

    return new DryIocContainerExtension(container);
}

The problem is that in my ExampleService I'm getting client with the following specs:

{
   "DefaultRequestHeaders":[
   ],
   "BaseAddress":null,
   "Timeout":"00:01:40",
   "MaxResponseContentBufferSize":2147483647
}

whilst I expected BaseAddress to be https://api.example.com/, so the REST API call fails.

What is the correct pattern to use IServiceProvider when using Prism for Xamarin.Forms with DryIoc? Unfortunately there's no documentation or open source code available on the following matter, and I am kind of lost.

Thanks you, and have a great day.

UPDATE #1

As per kind Dan S. guidance, DryIoc.Microsoft.DependencyInjection was uninstalled so the project came back at its state before trying to use IServiceCollection dependencies (in my case, IHttpClientFactory), then I installed Prism.Forms.Extended and later Prism.DryIoc.Extensions.
After that CreateContainerExtension() in App.xaml.cs became:

protected override IContainerExtension CreateContainerExtension()
{
    var containerExtension = PrismContainerExtension.Current;
    containerExtension.RegisterServices(s =>
    {
        s.AddHttpClient<IExampleService, ExampleService>(c =>
        {
            c.BaseAddress = new Uri("https://api.example.com/");
        });
    });
    return containerExtension;
}

and containerRegistry.Register<IExampleService, ExampleService>(); was removed from RegisterTypes().

Now ExampleService finally gets its HttpClient injected and everything is working.

UPDATE #2

The only packages related to Prism I am using are Prism.DryIoc.Forms and Prism.DryIoc.Extensions. I completely removed the override of CreateContainerExtension() in App.xaml.cs and refactored RegisterTypes() to

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    // Code deleted for brevity.
    containerRegistry.RegisterServices(s =>
    {
        s.AddHttpClient<IExampleService, ExampleService>(c =>
        {
            c.BaseAddress = new Uri("https://api.example.com/");
        });
    });
}  

This way I get thrown a NotImplementedException.
However, by overriding CreateContainerExtension() with the following:

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

Everything is finally back to working!


回答1:


If you want to use IServiceCollection extensions such as AddHttpClient I would suggest that you use the Prism Container Extensions. In your case it would be Prism.DryIoc.Extensions. The Container Extensions provide a lot of additional support including support for registering services with via the Service Collection extensions.

You can either install Prism.Forms.Extended and it will all just work, or you can update your App as follows:

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


来源:https://stackoverflow.com/questions/60078277/dryioc-and-iserviceprovider-on-prism-for-xamarin-forms-dryioc-microsoft-depende

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!