Coder Social home page Coder Social logo

Comments (4)

kbdavis07 avatar kbdavis07 commented on June 2, 2024 1

@chrismo111 Thanks again for your help! 👍

Will be giving these a try.

from gotenbergsharpapiclient.

chrismo111 avatar chrismo111 commented on June 2, 2024

TL;DR: Yes, you can configure the client with your retry settings when not using constructor injection in a netcore app .E.G. for use in a static class. Details are further down the thread below.

Question 2:

GotenbergSharpClient was built as a “typed client” for use with dependency injection. I'll make that more clear in the ReadMe. The way the client is written, setting up the Retry Policy requires D.I.

The constructor overloads for string and Uri were added to simplify the LinqPad demo code samples. I should've decorated them with the Obsolete attribute or EditorBrowsable(EditorBrowsableState.Never). The overload that accepts an instance of HttpClient shouldn't be used either (I need to remove the PublicApi attribute from that).

GotenbergSharpClient and its inner HttpClient are created and managed by HttpClientFactory behind the scenes when you call the AddGotenbergSharpClient service collection extension which in turn calls AddHttpClient and AddTypedClient. That extension also sets all the options and wires up the Polly Retry policy.

By adding the options and calling AddGotenbergSharpClient in StartUp.ConfigureServices you'll receive an instance of the client pre-configured with your settings whenever its injected or resolved through a ServiceProvider.

Take a look at these resources that outline all the benefits of DI/HttpClientFactory with Typed Clients:

Question 1:

To see the options set after the container is built just resolve them as IOptions through a service provider. E.G. sp.GetRequiredService<IOptions<GotenbergSharpClientOptions>>().Value

You could also get them in Startup like this:

var test = Configuration.GetSection(nameof(GotenbergSharpClient)).Get<GotenbergSharpClientOptions>();

When calling Configuration.GetSection alone the test.Value property is supposed to be null by design. From the MS documentation:

"When GetSection returns a matching section, Value isn't populated. A Key and Path are returned when the section exists."

See a discussion here: https://stackoverflow.com/q/46017593

from gotenbergsharpapiclient.

kbdavis07 avatar kbdavis07 commented on June 2, 2024

@chrismo111 Thanks for your reply 👍

Current situation I am in is we are using heavily static classes, project was using WebForms/WCF and ported to .Net Core, but slowly converting those to services that uses DI, but for PDF need to use it now in our static pdf helper for the time being.

So when used in a static class the Retry policy does not kick in then?

Is there a way to use a service locator in static class to get it to work?

Also still have

Question 4:

What is the best way to Unit test Gotenberg Server Errors which results in HttpResonse errors, to trigger the retry policy to kick in?

I also need to create Unit test that would cause the Retry to trigger.

Thanks again 👍

from gotenbergsharpapiclient.

chrismo111 avatar chrismo111 commented on June 2, 2024

If you're willing to use a service locator that'll work.

You'll still need to add the options and call services.AddGotenbergSharpClient to have your settings applied. When the service locator resolves an instance it'll be configured with those settings.

YMMV but I whipped together a little proof of concept where everything worked, including retries, in both netcore app2 and 3x. My service locator was based on this example.

Within a static class you'll be able to retrieve the client like this:
var sharpClient = ServiceLocator.ServiceProvider.GetService<GotenbergSharpClient>();

Testing the retry policy?
See Unit Test and Mock Typed HttpClient using HttpClientFactory .NET Core
I'd also start by looking at how Polly Tests them & their documentation:

from gotenbergsharpapiclient.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.