Home > front end >  How to use Microsoft.Extensions.Http.Polly in netcoreapp3.1 worker service project
How to use Microsoft.Extensions.Http.Polly in netcoreapp3.1 worker service project

Time:10-24

I was trying to impliment the following articale: enter image description here

What did I miss ?

CodePudding user response:

You're missing that package or assembly name isn't the same as the namespace(s) the code lives in. Although it's often convention that a package named "Foo" will occupy the namespace "Foo" and related namespaces will be something like "Foo.Bar", etc. it's not the only way to do this.

You can find out what namespaces and classes a library makes available to you by using the Object Browser:

  1. In Visual Studio, select "View"
  2. Select "Object Browser"
  3. You will see a list of assemblies that are referenced by your solution
  4. Scroll down to "Microsoft.Extensions.Http.Polly" and expand it

enter image description here

  1. You can see that it declares code in 4 namespaces:
    • Microsoft.Extensions.DependencyInjection
    • Microsoft.Extensions.Http
    • Microsoft.Extensions.Internal
    • Polly

Clicking on "PollyHttpClientBuilderExtensions" shows the methods made available by the class:

enter image description here

And, as the name suggests, the method signature displayed in the lower pane indicates that the selected method is an extension method on IHttpClientBuilder, so you have to use it in conjunction with services.AddHttpClient(). It seems likely that this is the one you want to access in your code. Therefore, you should include the following using:

using Microsoft.Extensions.DependencyInjection;
  • Related