{"id":16640278,"url":"https://github.com/yuzd/httpclientfactory","last_synced_at":"2025-03-16T22:31:21.686Z","repository":{"id":60773492,"uuid":"164209997","full_name":"yuzd/HttpClientFactory","owner":"yuzd","description":"safe HttpClient For netcore And netframework","archived":false,"fork":false,"pushed_at":"2023-06-13T13:07:11.000Z","size":16,"stargazers_count":77,"open_issues_count":1,"forks_count":8,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-27T14:57:42.587Z","etag":null,"topics":["httpclient","netcore","netframework"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yuzd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"MIT-LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-01-05T11:46:32.000Z","updated_at":"2024-08-13T08:29:13.000Z","dependencies_parsed_at":"2022-10-04T15:25:20.582Z","dependency_job_id":null,"html_url":"https://github.com/yuzd/HttpClientFactory","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuzd%2FHttpClientFactory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuzd%2FHttpClientFactory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuzd%2FHttpClientFactory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuzd%2FHttpClientFactory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yuzd","download_url":"https://codeload.github.com/yuzd/HttpClientFactory/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243830953,"owners_count":20354854,"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","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":["httpclient","netcore","netframework"],"created_at":"2024-10-12T07:08:22.305Z","updated_at":"2025-03-16T22:31:21.380Z","avatar_url":"https://github.com/yuzd.png","language":"C#","readme":"# HttpClientFactory For netcore And framework\n### NUGET Install-Package HttpClientFactory\n\nsupported netcore2.0 and framework4.5+\n\n## Why need HttpClientFactory?\n\nAs you know, HttpClient has a [trap](https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/) for using!\n\n1. if not Singleton,\neach instance of HttpClient will open a new socket connection \nand on  high traffic sites you can exhaust the available pool and receive a System.Net.Sockets.SocketException\n2. if Singleton,HttpClient doesn't respect DNS changes!\n\n\n## So how to use HttpClient correctly？\n\n```\n//useage 1： same Host use same HttpClient\nPerHostHttpClientFactory perHostHttpClientFactory = new PerHostHttpClientFactory();// can be static\n//or you can change default timeout for perFactory\nPerHostHttpClientFactory perHostHttpClientFactory = new PerHostHttpClientFactory(TimeSpan.FromSeconds(10));\nHttpClient client = perHostHttpClientFactory.GetHttpClient(\"http://www.baidu.com\");\n\n//useage 2： per url use per HttpClient\nPerUrlHttpClientFactory perUrlHttpClientFactory = new PerUrlHttpClientFactory();\n//or you can change default timeout for perFactory\nPerUrlHttpClientFactory perUrlHttpClientFactory = new PerUrlHttpClientFactory(TimeSpan.FromSeconds(10));\nHttpClient client = perUrlHttpClientFactory.GetHttpClient(\"http://www.baidu.com\");\n\n//useage 3： per proxy use per HttpClient\nPerHostHttpClientFactory perHostHttpClientFactory = new PerHostHttpClientFactory();\n//or you can change default timeout for perFactory\nPerHostHttpClientFactory perHostHttpClientFactory = new PerHostHttpClientFactory(TimeSpan.FromSeconds(10));\nHttpClient client = perUrlHttpClientFactory.GetProxiedHttpClient(\"http://127.0.0.1:8080\");\n```\n\n## Easy to implement\n```\n\tpublic class XXXHttpClientFactory : HttpClientFactoryBase\n    {\n        protected override string GetCacheKey(string key)\n        {\n            return key;\n        }\n\t\t\t\t\n\t\t\t\t\n\t\tprotected override HttpClient CreateHttpClient(HttpMessageHandler handler)\n        {\n            return new HttpClient(handler)\n            {\n                Timeout = TimeSpan.FromSeconds(20),\n            };\n        }\n\n        protected override HttpMessageHandler CreateMessageHandler()\n        {\n            var handler = new HttpClientHandler();\n            handler.Proxy = new WebProxy(\"xxx\");\n            return handler;\n        }\n    }\n```\n\n## Remark\n1. Default Timeout is TimeSpan.FromSeconds(100)\n2. Default [ConnectionLeaseExpired](http://byterot.blogspot.com/2016/07/singleton-httpclient-dns.html) is TimeSpan.FromMinutes(1)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuzd%2Fhttpclientfactory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyuzd%2Fhttpclientfactory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuzd%2Fhttpclientfactory/lists"}