{"id":28328424,"url":"https://github.com/nosratifarhad/httpclient_bestpractice_dotnet_8","last_synced_at":"2026-04-15T16:08:24.311Z","repository":{"id":214652208,"uuid":"736611634","full_name":"nosratifarhad/HttpClient_BestPractice_DotNet_8","owner":"nosratifarhad","description":"Simple implementation example of 4 types of implementation in the HttpClientFactory","archived":false,"fork":false,"pushed_at":"2024-05-27T17:50:40.000Z","size":19,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-24T08:43:58.890Z","etag":null,"topics":["csharp","dotnet","dotnet-core","dotnetcore","http-client","httpclientfactory","refit"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nosratifarhad.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-12-28T11:27:13.000Z","updated_at":"2024-05-27T17:50:43.000Z","dependencies_parsed_at":"2023-12-29T17:34:19.814Z","dependency_job_id":null,"html_url":"https://github.com/nosratifarhad/HttpClient_BestPractice_DotNet_8","commit_stats":null,"previous_names":["nosratifarhad/httpclient_bestpractice_dotnet_8"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/nosratifarhad/HttpClient_BestPractice_DotNet_8","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nosratifarhad%2FHttpClient_BestPractice_DotNet_8","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nosratifarhad%2FHttpClient_BestPractice_DotNet_8/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nosratifarhad%2FHttpClient_BestPractice_DotNet_8/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nosratifarhad%2FHttpClient_BestPractice_DotNet_8/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nosratifarhad","download_url":"https://codeload.github.com/nosratifarhad/HttpClient_BestPractice_DotNet_8/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nosratifarhad%2FHttpClient_BestPractice_DotNet_8/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267780333,"owners_count":24143202,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-29T02:00:12.549Z","response_time":2574,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","dotnet","dotnet-core","dotnetcore","http-client","httpclientfactory","refit"],"created_at":"2025-05-26T06:16:58.304Z","updated_at":"2026-04-15T16:08:19.285Z","avatar_url":"https://github.com/nosratifarhad.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# HttpClient Best Practices Implementations\n\nThis repository contains implementations of HttpClient in 4 different ways:\n\n1. Basic Usage\n2. Named Clients\n3. Typed Clients\n4. Generated Clients\n\n## Installation\n\nNo additional packages need to be installed for implementing Basic Usage, Named Clients, and Typed Clients.\n\n## Usage\n\n### Basic Usage\n\nTo implement Basic Usage, add the following service to your program file:\n\n```csharp\n#region Basic usage\n\nbuilder.Services.AddHttpClient();\n\n#endregion Basic usage\n```\n\n## You can use the HttpClient by following the method below:\n```csharp\n    public async Task Send(NotificationModel productDto)\n    {\n        var httpRequestMessage = new HttpRequestMessage(\n            HttpMethod.Post,\n            \"https://api.github.com/repos/dotnet/AspNetCore.Docs/branches\")\n        {\n            Headers =\n            {\n                { HeaderNames.Accept, \"application/vnd.github.v3+json\" },\n                { HeaderNames.UserAgent, \"HttpRequestsSample\" }\n            }\n        };\n\n        var httpClient = httpClientFactory.CreateClient();\n\n        var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage);\n\n        if (!httpResponseMessage.IsSuccessStatusCode)\n            throw new Exception();\n    }\n```\n## Named Clients\n\nFor Typed Clients, you can add the httpClient service as shown below and assign it a key or name. At usage time, simply call it by its name httpClient. In this section, you can add the used configurations as follows and at usage time, only call the external service.\n\nYou can use this service as follows:\n\n```csharp\n#region Named clients\nbuilder.Services.AddHttpClient(\"GitHub\", httpClient =\u003e\n{\n    httpClient.BaseAddress = new Uri(\"https://api.github.com/\");\n\n    httpClient.DefaultRequestHeaders.Add(\n        HeaderNames.Accept, \"application/vnd.github.v3+json\");\n\n    httpClient.DefaultRequestHeaders.Add(\n        HeaderNames.UserAgent, \"HttpRequestsSample\");\n\n    // ...\n});\n#endregion Named clients\n```\n\n## You can use the HttpClient by following the method below:\nBecause you set base configes in program file , in method only used.\n\nyou must get servese from name \"GitHub\".\n```csharp\n    public async Task Send(NotificationModel productDto)\n    {\n        var httpClient = httpClientFactory.CreateClient(\"GitHub\");\n\n        using var todoItemJson = new StringContent(JsonSerializer.Serialize(productDto), Encoding.UTF8, Application.Json);\n\n        var httpResponseMessage = await httpClient.PostAsync(\n            \"repos/dotnet/AspNetCore.Docs/branches\", todoItemJson);\n\n        if (!httpResponseMessage.IsSuccessStatusCode)\n            throw new Exception();\n    }\n```\n\n## Typed Clients\n\nIn the Typed Clients model, your class does not need an interface, and only one class is sufficient for implementation. You first need to add the httpClient service as shown below and introduce your wrapper class. Additionally, you can implement base configurations either in the configuration section or in the constructor of the wrapper class, as we have done in this part.\n\n```csharp\n#region Typed clients\nbuilder.Services.AddHttpClient\u003cProductNotificationWrapper_TypedClient\u003e(httpClient =\u003e\n{\n    httpClient.BaseAddress = new Uri(\"https://api.github.com/\");\n\n    httpClient.DefaultRequestHeaders.Add(\n                HeaderNames.Accept, \"application/vnd.github.v3+json\");\n\n    httpClient.DefaultRequestHeaders.Add(\n        HeaderNames.UserAgent, \"HttpRequestsSample\");\n\n    // ...\n});\n#endregion Typed clients\n```\n\n## You can use the HttpClient by following the method below:\n\n```csharp\npublic async Task Send(NotificationModel productDto)\n{\n    using var todoItemJson = \n    new StringContent(JsonSerializer.Serialize(productDto), Encoding.UTF8, Application.Json);\n\n    await _httpClient.PostAsync(\"repos/dotnet/AspNetCore.Docs/branches\", todoItemJson);\n}\n```\n\n## Refit Integration\n\nThe next model you can use is the Refit package. First, you need to install the following 2 packages:\n\n- Refit\n- Refit.HttpClientFactory\n\nAfter installing these packages, you need to add an interface where you define the required signatures. You can specify the address to be requested and the type of method in your signature using the GET attribute, as shown below:\n\n```csharp\n#region Generated clients\nbuilder.Services.AddRefitClient\u003cIProductNotificationWrapper_GeneratedClient\u003e()\n    .ConfigureHttpClient(httpClient =\u003e\n    {\n        httpClient.BaseAddress = new Uri(\"https://api.github.com/\");\n\n        httpClient.DefaultRequestHeaders.Add(\n            HeaderNames.Accept, \"application/vnd.github.v3+json\");\n\n        httpClient.DefaultRequestHeaders.Add(\n            HeaderNames.UserAgent, \"HttpRequestsSample\");\n\n        // ...\n    });\n#endregion Generated clients\n```\nNow add your interface as the httpClient service as in the Typed Clients model, with the difference that in Typed Clients, you added a class to the service, but with Refit, it is sufficient to add the interface.\n\nNow add additional information in the configuration:\n\n```csharp\n    public interface IProductNotificationWrapper_GeneratedClient\n    {\n        [Get(\"/repos/dotnet/AspNetCore.Docs/branches\")]\n        Task Send_GeneratedClient(NotificationModel productDto);\n    }\n```\nreference : https://virgool.io/@farhadnosrati/ihttpclientfactory-aqkklgc8qpyk\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnosratifarhad%2Fhttpclient_bestpractice_dotnet_8","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnosratifarhad%2Fhttpclient_bestpractice_dotnet_8","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnosratifarhad%2Fhttpclient_bestpractice_dotnet_8/lists"}