Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我想将IHttpClientFactory与Prism一起使用_C# - Fatal编程技术网

C# 我想将IHttpClientFactory与Prism一起使用

C# 我想将IHttpClientFactory与Prism一起使用,c#,C#,在使用Prism(.NET Core3.1,DryIoc)的WPF应用程序中,我希望在主屏幕的ViewModel构造函数中定义并获取IHttpClientFactory,但在主屏幕的View构造函数的InitializeComponent()方法中出现以下异常。出现 内部例外情况1: ContainerResolutionException:解析“SamplePrismDryIOApp.ViewModels.MainWindowViewModel”时发生意外错误 内部例外情况2: Contai

在使用Prism(.NET Core3.1,DryIoc)的WPF应用程序中,我希望在主屏幕的ViewModel构造函数中定义并获取IHttpClientFactory,但在主屏幕的View构造函数的InitializeComponent()方法中出现以下异常。出现


内部例外情况1: ContainerResolutionException:解析“SamplePrismDryIOApp.ViewModels.MainWindowViewModel”时发生意外错误

内部例外情况2: ContainerException:代码:Error.UnableToResolveUnknownService; 消息:无法将Microsoft.Extensions.DependencyInjection.IServiceScopeFactory解析为参数“scopeFactory”(IsSingleton或DependencyOfSingleton) 解析中的根Singleton Microsoft.Extensions.Http.DefaultHttpClientFactory{DryIoc.IfUnresolved.ReturnDefaultIfNotRegistered}FactoryId=168(IsSingleton或DependencyOfSingleton,IsResolutionCall) 从没有范围的容器


APP类定义如下,但是我应该如何处理上面“IServiceScopeFactory”的容器定义


公共部分类应用程序:PrismApplication
{
受保护的重写窗口CreateShell()
{
返回Container.Resolve();
}
受保护的覆盖无效注册表类型(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterServices(s=>{
s、 AddHttpClient();
});
}
}

安装的软件包如下所示

-Microsoft.Extensions.Http(5.0.0)

-Microsoft.Extensions.Http,Polly(5.0.1)

-Prism.Container.Extensions(8.0.62)


-Prism.DryIoc(8.0.0.1909)

采取以下措施作为对策

--使用服务提供程序属性定义POJO类。
--在App类中定义一个静态方法,用于为HttpClientFactory创建服务提供程序。
--在RegisterTypes方法中注册GetServiceControl方法的返回值。
受保护的覆盖无效注册表类型(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterSingleton(GetServiceControl);
}

--从要使用的ViewModel的构造函数中获取ServiceControl的实例,并使用IHttpClientFactory,如下所示。
(
...
服务控制服务控制,
...
)
IHttpClientFactory httpClientFactory=serviceControl.SvcProvider.GetRequiredService();


通过以上操作,可以使用IHttpClientFactory。

Prism.Container.Extensions
Prism+DryIoc+WPF
不支持的

我有一个包装器库,它将IServiceCollection转换为相应的容器,并在诸如Prism之类的环境中注册它,在该环境中容器设置无法更改

我做了一个样品


HttpClient
位于
System.Net.Http
中,您可能需要添加它。感谢您的建议
基本框架的规范信息已泄漏。具体如下--Microsoft.NETCore.App--Microsoft.WindowsDesktop.App.WPF上述“Microsoft.NET Core.App”框架包括“System.NET.Http”。因此,我认为这不是你指出的问题。
public partial class App: PrismApplication
{
    protected override Window CreateShell ()
    {
        return Container.Resolve <MainWindow> ();
    }

    protected override void RegisterTypes (IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterServices (s => {
            s.AddHttpClient ();
        });
    }
}
using Microsoft.Extensions.DependencyInjection;

public class ServiceControl
{
    public ServiceProvider SvcProvider {get; private set;}

    public ServiceControl (ServiceProvider serviceProvider)
    {
        this.SvcProvider
        = serviceProvider;
    }
}
public static GenServiceControl GetServiceControl ()
{
    IServiceCollection services = new ServiceCollection ();
    services.AddHttpClient ();
    var serviceProvider = services.BuildServiceProvider ();
    
    return new ServiceControl (serviceProvider);
}
protected override void RegisterTypes (IContainerRegistry containerRegistry)
{

    containerRegistry.RegisterSingleton <ServiceControl> (GetServiceControl);

}
(
...
ServiceControl serviceControl,
...
)

IHttpClientFactory httpClientFactory = serviceControl.SvcProvider.GetRequiredService <IHttpClientFactory> ();