无法使用 polly 解析 IReadOnlyPolicyRegistry

Cannot resolve IReadOnlyPolicyRegistry with polly

我有一个网站 api,其中包含对 polly

的所有正确引用

我的一个服务有一个带有构造器的构造器

public MyService(IReadOnlyPolicyRegistry<string> policyRegistry)

无法解析类型 'Polly.Registry.IReadOnlyPolicyRegistry`1[System.String]'

的服务

我该如何解决这个问题?该信息根本没有帮助

我正在使用下面的 Polly 包

    <PackageReference Include="Polly" Version="7.2.1" />
    <PackageReference Include="Polly.Caching.Distributed" Version="3.0.1" />
    <PackageReference Include="Polly.Caching.Memory" Version="3.0.2" />
    <PackageReference Include="Polly.Contrib.DuplicateRequestCollapser" Version="0.2.1" />

保罗

Polly 本身不会使 IReadOnlyPolicyRegistry<string> 可用于您的依赖注入容器,因此除非您手动添加它并由 PolicyRegistry 具体类型提供其实现,否则它将不可用.

但是,如果您使用 Microsoft.Extensions.Http.Polly package in ASP.NET Core with IHttpClientFactory then it will be added automatically by the AddPolicyRegistry() extension method

您需要添加额外的包然后调用该方法,或者手动将依赖项注册到您的服务集合,例如:

services.AddSingleton<IReadOnlyPolicyRegistry<string>, PolicyRegistry>();