User3250
User3250

Reputation: 3421

'IServiceCollection' does not contain a definition for 'AddPolicyHandler'

I am getting error in my .NET 5.0 framework Azure durable function project in VS2019

'IServiceCollection' does not contain a definition for 'AddPolicyHandler' and the best extension method overload 'PollyHttpClientBuilderExtensions.AddPolicyHandler(IHttpClientBuilder, IAsyncPolicy)' requires a receiver of type 'IHttpClientBuilder'

.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="IdentityModel" Version="4.2.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.15.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="2.5.1" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="4.3.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.0" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="3.1.0" />
    <PackageReference Include="Microsoft.Extensions.Http.Polly" Version="3.1.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.13" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

Startup.cs

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Polly;
using Polly.Extensions.Http;
using System;
using System.Net.Http;
namespace Itel.DeliveryOrchestration
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddHttpClient()
                .AddPolicyHandler(GetRetryPolicy());
        }

        private IAsyncPolicy<HttpResponseMessage> GetRetryPolicy()
        {
            return HttpPolicyExtensions 
                .HandleTransientHttpError()
                .OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound)
                .WaitAndRetryAsync(2, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
        }

    }
}

Upvotes: 2

Views: 3951

Answers (2)

Peter Csala
Peter Csala

Reputation: 22829

As I can see (based on the provided code sample) you have used the wrong Polly - HttpClient integration nuget package

using Polly.Extensions.Http;

builder.Services.AddHttpClient()
                .AddPolicyHandler(GetRetryPolicy());

Upvotes: 4

SaiSakethGuduru
SaiSakethGuduru

Reputation: 2440

Posting suggestion provided by @Nkosi in the comments as an answer so that it will be helpful for other community members who face similar kind of issues.

AddHttpClient with no arguments returns IServiceCollection while AddPolicyHandler applies to IHttpClientBuilder. You will need to use a named client

public static IServiceCollection AddHttpClient(this IServiceCollection services)
{
    if (services == null)
    {
        throw new ArgumentNullException(nameof(services));
    }

    //...

Referenced source code HttpClientFactoryServiceCollectionExtensions

Upvotes: 2

Related Questions