Basit Bir Rest Api Client Geliştirelim 9

2023-05-14

HTTP isteklerini izlemek ve teşhis etmek için daha fazla yetenek ekleyerek, kütüphanemizi daha da geliştirebiliriz. Bu amaçla, isteklerin ve yanıtların bazı temel bilgilerini loga kaydeden bir middleware ekleyelim.

Bu özellik, HTTP isteklerinin ve yanıtlarının kaydını tutmak için HttpClient‘in DelegatingHandler sınıfını kullanır. Bu, bir HTTP isteği gönderildiğinde ve bir yanıt alındığında çalışan bir kod parçasıdır. Bu durumda, HTTP isteklerinin ve yanıtlarının bazı temel bilgilerini loga kaydeder.

Aşağıda, bu özelliği eklemek için gereken kod parçacığı bulunmaktadır:

public class LoggingHandler : DelegatingHandler
{
    private readonly ILogger<LoggingHandler> _logger;

    public LoggingHandler(ILogger<LoggingHandler> logger)
    {
        _logger = logger;
    }

    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        _logger.LogInformation($"Sending request to {request.RequestUri}.");

        var response = await base.SendAsync(request, cancellationToken);

        if (response.IsSuccessStatusCode)
        {
            _logger.LogInformation($"Received response from {request.RequestUri}.");
        }
        else
        {
            _logger.LogError($"Received HTTP {response.StatusCode} from {request.RequestUri}.");
        }

        return response;
    }
}

Bu LoggingHandler‘i kullanmak için, MyHttpClient‘in yapıcısına eklememiz gerekiyor:

public MyHttpClient(HttpClient client, IPolicyRegistry<string> policyRegistry, ILogger<MyHttpClient> logger, string baseAddress, TimeSpan? timeout = null)
{
    _client = client;
    _client.BaseAddress = new Uri(baseAddress);

    if (timeout.HasValue)
    {
        _client.Timeout = timeout.Value;
    }

    _policyRegistry = policyRegistry;
    _logger = logger;

    // Adding the LoggingHandler
    _client.MessageHandlers.Add(new LoggingHandler(logger));

    // ... other code ...
}

Bu geliştirmelerle, HTTP istemcimiz artık her bir HTTP isteğinin ve yanıtının kaydını tutuyor.



More posts like this

The SemaphoreSlim Class: Multitask-Based Programming in C#

2023-06-11 | #net #semaphoreslim

Overview The SemaphoreSlim class is a structure used in C# to control one or more threads using a specific resource or operation concurrently. SemaphoreSlim limits the number of threads that can access the resource at the same time. The use of SemaphoreSlim is often used to prevent deadlock situations in multi-threaded applications and to ensure that a specific resource is used by only one or more threads at the same time.

Continue reading 


LINQ and Optimistic Concurrency: Methods for Ensuring Data Integrity

2023-06-11 | #linq #net #optimistic-concurrency

When working with databases, it is of great importance to properly manage concurrent operations and ensure data integrity. Concurrency control is used to manage multiple users or processes accessing the same data concurrently. In this article, we will explore how to ensure data integrity using LINQ (Language Integrated Query) with optimistic concurrency. What is Optimistic Concurrency? Optimistic concurrency is a method where resources are not locked when a transaction begins, but changes are checked before the transaction is completed.

Continue reading 


LINQ and Pessimistic Concurrency: Methods for Ensuring Data Integrity

2023-06-11 | #linq #net #pessimistic-concurrency

When working with databases, managing concurrent operations and ensuring data integrity are of great importance. Concurrency control is used to manage multiple users or processes accessing the same data concurrently. In this article, we will explore how to use LINQ (Language Integrated Query) with pessimistic concurrency to maintain data integrity. What is Pessimistic Concurrency? Pessimistic concurrency is based on the principle of locking the relevant data when a transaction begins and maintaining the lock until the transaction is completed.

Continue reading 