用于 Xamarin.Forms 的 Prism 上的 DryIoc 和 IServiceProvider (DryIoc.Microsoft.DependencyInjection) [英] DryIoc and IServiceProvider on Prism for Xamarin.Forms (DryIoc.Microsoft.DependencyInjection)

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

问题描述

我有一个以 DryIoc 作为容器的 Prism 应用程序.我希望 IHttpClientFactory 为我的 类型的客户端 提供 HttpClients,它们是这样的:

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.
    }
}

App.xaml.cs 中,我注册了我的类型化客户端,以便它们可以使用以下内容注入到视图模型中:

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

那是在尝试使用 IHttpClientFactory 之前.

And that's before trying to use IHttpClientFactory.

现在,要添加它,我们应该在 IServiceCollection AddHttpClient().那就是我认为需要 DryIoc.Microsoft.DependencyInjection 的地方,所以,我仍然在 App.xaml.cs 中写了以下内容:

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

问题在于,在我的 ExampleService 中,我获得了具有以下规范的客户端:

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

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

虽然我希望 BaseAddresshttps://api.example.com/,因此 REST API 调用失败.

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

在将 Prism 用于 Xamarin.Forms 和 DryIoc 时,使用 IServiceProvider 的正确模式是什么?不幸的是,没有关于以下问题的文档或开源代码,我有点迷茫.

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.

更新 #1

根据 Dan S. 指南,DryIoc.Microsoft.DependencyInjection 是已卸载,因此项目在尝试使用 IServiceCollection 依赖项(在我的情况下为 IHttpClientFactory)之前恢复到其状态,然后我安装了 Prism.Forms.Extended 及更高版本 Prism.DryIoc.Extensions.
之后 App.xaml.cs 中的 CreateContainerExtension() 变成了:

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

containerRegistry.Register(); 已从 RegisterTypes() 中删除.

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

现在 ExampleService 终于注入了它的 HttpClient 并且一切正常.

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

更新 #2

我使用的与 Prism 相关的唯一软件包是 Prism.DryIoc.FormsPrism.DryIoc.Extensions.我完全删除了 App.xaml.cs 中对 CreateContainerExtension() 的覆盖,并将 RegisterTypes() 重构为

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

这样我就会被抛出一个 NotImplementedException.
但是,通过使用以下内容覆盖 CreateContainerExtension():

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

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

一切终于恢复正常了!

推荐答案

如果您想使用 IServiceCollection 扩展,例如 AddHttpClient,我建议您使用 Prism Container Extensions.在您的情况下,它将是 Prism.DryIoc.Extensions.容器扩展提供了许多额外的支持,包括支持通过服务集合扩展注册服务.

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.

您可以安装 Prism.Forms.Extended 并且一切正常,或者您可以按如下方式更新您的应用程序:

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;

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆