You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to use Polly to handle the retries in some cases.
By the moment, I have a class library with the gRPC client, is this:
public class MyClient : IClient
{
IClientService _client;
public MyClient(string paramAddress, int paramPort, string? paramTokenJwt = null)
{
SocketsHttpHandler miHttpHandler = new SocketsHttpHandler();
//To validate self sifned certificate from the server. By the moment it returns only true for test purposes.
miHttpHandler.SslOptions.RemoteCertificateValidationCallback = ValidateServerCertificate;
//To can add credentials or JWT token.
//is there a better option to add credentials with a factory???
HttpClient miHttpClient = new HttpClient(miHttpHandler);
//The options of the channel. It will have the JWT token for authentication and another connection settings.
GrpcChannelOptions myChannelOptions = new GrpcChannelOptions()
{
MaxReceiveMessageSize = 62914560,
MaxSendMessageSize = 62914560,
HttpClient = httpClient,
};
//I add the token to the channel if it is given. If not, the channel will can be used to call the login
//method of the service, that doesn't need authentication.
if (string.IsNullOrWhiteSpace(paramStrTokenJwt) == false)
{
CallCredentials myCredentials = CallCredentials.FromInterceptor((c, m) =>
{
m.Add(HeaderNames.Authorization, $"Bearer {paramStrTokenJwt}");
return Task.CompletedTask;
});
myChannelOptions.Credentials = ChannelCredentials.Create(new SslCredentials(), myCredentials);
}
//Create the channel
Uri miUri = new Uri("{paramAddress}:{paramPort.ToString()}");
GrpcChannel myChannel = GrpcChannel.ForAddress(miUri, myChannelOptions);
//Create the client
_client = GrpcClientFactory.CreateGrpcService<IMyService>(myChannel);
}
}
This is a code that I have seen to use Polly:
var retryPolicy = HttpPolicyExtensions
.HandleTransientHttpError() // HttpRequestException, 5XX and 408
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(retryAttempt));
services.AddHttpClient<ISuperHeroService, SuperHeroService>(o =>
o.BaseAddress = new Uri(Configuration["SuperHeroApiConfig:BaseUrl"]))
.AddPolicyHandler(retryPolicy);
They are using Services.AddHttpClient to register the client. Services, if I am not wrong, it is availalble at application level (console application, WPF... and so on) but it is not available at class library level, that it is where I am creating the client.
I have seen in the documentation that I can create a policy in each method and the use the method Excute() to run the method:
But it seems that it is needed to repeat more code and int less transparent.
If I don't misundertand the example code in which is used the serices.AddHttpClient, just the policy is added to the AddHttpClient and every method of this client will use the policy, it is not needed to wrap the method in the Execute() method.
So I was wondering if it is possible to use policies in a way that is not needed to wrap the method in the Excute() method.
Thanks.
The text was updated successfully, but these errors were encountered:
As I can see you are explicitly creating an HttpClient instance inside the MyClient. Here you can provide multiple handlers by chaining them (via InnerHandler).
Polly exposes a handler called PolicyHttpMessageHandler which allows you register any policy
(Side note: The AddPolicyHandler uses exactly this handler)
I would like to use Polly to handle the retries in some cases.
By the moment, I have a class library with the gRPC client, is this:
This is a code that I have seen to use Polly:
They are using Services.AddHttpClient to register the client. Services, if I am not wrong, it is availalble at application level (console application, WPF... and so on) but it is not available at class library level, that it is where I am creating the client.
I have seen in the documentation that I can create a policy in each method and the use the method Excute() to run the method:
But it seems that it is needed to repeat more code and int less transparent.
If I don't misundertand the example code in which is used the serices.AddHttpClient, just the policy is added to the AddHttpClient and every method of this client will use the policy, it is not needed to wrap the method in the Execute() method.
So I was wondering if it is possible to use policies in a way that is not needed to wrap the method in the Excute() method.
Thanks.
The text was updated successfully, but these errors were encountered: