GithubHelp home page GithubHelp logo

httpclientfactory's Introduction

HttpClientFactory For netcore And framework

NUGET Install-Package HttpClientFactory

supported netcore2.0 and framework4.5+

Why need HttpClientFactory?

As you know, HttpClient has a trap for using!

  1. if not Singleton, each instance of HttpClient will open a new socket connection and on high traffic sites you can exhaust the available pool and receive a System.Net.Sockets.SocketException
  2. if Singleton,HttpClient doesn't respect DNS changes!

So how to use HttpClient correctly?

//useage 1: same Host use same HttpClient
PerHostHttpClientFactory perHostHttpClientFactory = new PerHostHttpClientFactory();// can be static
//or you can change default timeout for perFactory
PerHostHttpClientFactory perHostHttpClientFactory = new PerHostHttpClientFactory(TimeSpan.FromSeconds(10));
HttpClient client = perHostHttpClientFactory.GetHttpClient("http://www.baidu.com");

//useage 2: per url use per HttpClient
PerUrlHttpClientFactory perUrlHttpClientFactory = new PerUrlHttpClientFactory();
//or you can change default timeout for perFactory
PerUrlHttpClientFactory perUrlHttpClientFactory = new PerUrlHttpClientFactory(TimeSpan.FromSeconds(10));
HttpClient client = perUrlHttpClientFactory.GetHttpClient("http://www.baidu.com");

//useage 3: per proxy use per HttpClient
PerHostHttpClientFactory perHostHttpClientFactory = new PerHostHttpClientFactory();
//or you can change default timeout for perFactory
PerHostHttpClientFactory perHostHttpClientFactory = new PerHostHttpClientFactory(TimeSpan.FromSeconds(10));
HttpClient client = perUrlHttpClientFactory.GetProxiedHttpClient("http://127.0.0.1:8080");

Easy to implement

	public class XXXHttpClientFactory : HttpClientFactoryBase
    {
        protected override string GetCacheKey(string key)
        {
            return key;
        }
				
				
		protected override HttpClient CreateHttpClient(HttpMessageHandler handler)
        {
            return new HttpClient(handler)
            {
                Timeout = TimeSpan.FromSeconds(20),
            };
        }

        protected override HttpMessageHandler CreateMessageHandler()
        {
            var handler = new HttpClientHandler();
            handler.Proxy = new WebProxy("xxx");
            return handler;
        }
    }

Remark

  1. Default Timeout is TimeSpan.FromSeconds(100)
  2. Default ConnectionLeaseExpired is TimeSpan.FromMinutes(1)

httpclientfactory's People

Contributors

yuzd avatar qrdemole avatar

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.