{
    "componentChunkName": "component---src-templates-blog-tsx",
    "path": "/blog/index.php/2018/08/08/restclient-3-3-0-gets-ihttpclientfactory-support/",
    "result": {"data":{"mdx":{"body":"var _excluded = [\"components\"];\n\nfunction _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsxRuntime classic */\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"RestClient 3.3.0 Gets IHttpClientFactory Support\",\n  \"date\": \"2018-08-08T00:00:00.000Z\",\n  \"image\": \"./image.jpg\",\n  \"banner\": \"./banner.jpg\",\n  \"description\": \"Update we made to our C# Rest Client to support IHttpClientFactory.\"\n};\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, _excluded);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"p\", null, \"Prior to .NET Core 2.1 anyone who has tried to use HttpClient at scale (maybe from a microservice) may have run into the \", mdx(\"a\", {\n    href: \"https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/\"\n  }, \"dreaded socket exhaustion issue\"), \".\"), mdx(\"p\", null, \"Socket exhaustion happens because HttpClient is designed to be instantiated once and re-used throughout the life of an application,\\nif you make HTTP calls in a server application for every request you will run out of sockets (because sockets get left in \", mdx(\"a\", {\n    href: \"https://superuser.com/questions/173535/what-are-close-wait-and-time-wait-states\"\n  }, \"TIME_WAIT\"), \" state for 240 seconds by default).\"), mdx(\"blockquote\", null, mdx(\"a\", {\n    href: \"https://tech.just-eat.com/2018/06/14/aspnet-core-21-supercharging-our-applications/\"\n  }, mdx(\"strong\", null, \"Using IHttpClientFactory can improve performance by 43%!\"))), mdx(\"p\", null, \"A simple fix would be to instantiate HttpClient as a singleton, however, you might get an issue getting DNS updates, which is a real problem if you are load balancing or have cloud managed services.\"), mdx(\"p\", null, \"Knowing this limitation and realising that HttpClient is being used more from servers because of microservices, the .NET Core team have come up with IHttpClientFactory.\"), mdx(\"p\", null, \"An implementation of IHttpClientFactory makes use of connection pooling, and HttpClient's are disposed of after a specified time.\\nThis resolves the socket exhaustion issue as it controls how many sockets get left in the TIME_WAIT state, and because the connections do get disposed,\\nwe get DNS updates when a new HttpClient is created in the pool.\"), mdx(\"p\", null, \"If you want to learn more about IHttpClientFactory read \", mdx(\"a\", {\n    href: \"https://www.stevejgordon.co.uk/introduction-to-httpclientfactory-aspnetcore\"\n  }, \"@stevejgordon excellent blog post\"), \".\"), mdx(\"h2\", null, \"How does RestClient implement IHttpClientFactory?\"), mdx(\"p\", null, \"Now you know a bit about IHttpClientFactory, how RestClient implements IHttpClientFactory? Much the same HttpClient, but with all the flexibility and power of RestClient.\"), mdx(\"p\", null, \"Here's how to use it:\"), mdx(\"p\", null, \"To use IHttpClientFactory with RestClient, first register your RestClient as a service in Startup.cs. This is the simplest version if you only need one RestClient.\"), mdx(\"deckgo-highlight-code\", {\n    \"language\": \"csharp\",\n    \"terminal\": \"carbon\",\n    \"theme\": \"vscode\",\n    \"line-numbers\": \"true\"\n  }, \"\\n          \", mdx(\"code\", {\n    parentName: \"deckgo-highlight-code\",\n    \"slot\": \"code\"\n  }, \"public class Startup\\n{\\n    public void ConfigureServices(IServiceCollection services)\\n    {\\n        services.AddRestClient(\\\"https://api.github.com\\\");\\n    }\\n}\"), \"\\n        \"), mdx(\"p\", null, \"Then inject IRestClientFactory into your controller, which is how we support IHttpClientFactory.\"), mdx(\"deckgo-highlight-code\", {\n    \"language\": \"csharp\",\n    \"terminal\": \"carbon\",\n    \"theme\": \"vscode\",\n    \"line-numbers\": \"true\"\n  }, \"\\n          \", mdx(\"code\", {\n    parentName: \"deckgo-highlight-code\",\n    \"slot\": \"code\"\n  }, \"public class GitHubController : Controller\\n{\\n    private readonly IRestClientFactory _restClientFactory;\\n\\n    public GitHubController(IRestClientFactory restClientFactory)\\n    {\\n        _restClientFactory = restClientFactory;\\n    }\\n    \\n    [Route(\\\"github/users/dalsoft\\\"), HttpGet]\\n    public async Task<List<Repository>> CreateClient()\\n    {\\n        dynamic restClient = _restClientFactory.CreateClient();\\n    \\n        var repositories = await restClient.users.dalsoft.repos.Get();\\n    \\n        return repositories;\\n    }\\n}\"), \"\\n        \"), mdx(\"h2\", null, \"Multiple RestClient\\u2019s using IRestClientFactory\"), mdx(\"p\", null, \"If you need to support multiple RestClient\\u2019s use a named client.\"), mdx(\"p\", null, \"First register your named RestClient as a service in Startup.cs\"), mdx(\"deckgo-highlight-code\", {\n    \"language\": \"csharp\",\n    \"terminal\": \"carbon\",\n    \"theme\": \"vscode\",\n    \"line-numbers\": \"true\"\n  }, \"\\n          \", mdx(\"code\", {\n    parentName: \"deckgo-highlight-code\",\n    \"slot\": \"code\"\n  }, \"public class Startup\\n{\\n    public void ConfigureServices(IServiceCollection services)\\n    {\\n        services.AddRestClient(\\\"MyNamedGitHubClient\\\", \\\"https://api.github.com/orgs/\\\");\\n    }\\n}\"), \"\\n        \"), mdx(\"p\", null, \"Then inject IRestClientFactory into your controller (which is how we support IHttpClientFactory), and call CreateClient passing the name of your client.\"), mdx(\"deckgo-highlight-code\", {\n    \"language\": \"csharp\",\n    \"terminal\": \"carbon\",\n    \"theme\": \"vscode\",\n    \"line-numbers\": \"true\"\n  }, \"\\n          \", mdx(\"code\", {\n    parentName: \"deckgo-highlight-code\",\n    \"slot\": \"code\"\n  }, \"public class GitHubController : Controller\\n{\\n    private readonly IRestClientFactory _restClientFactory;\\n\\n    public GitHubController(IRestClientFactory restClientFactory)\\n    {\\n        _restClientFactory = restClientFactory;\\n    }\\n\\n    [Route(\\\"github/orgs/dotnet/repos\\\"), HttpGet]\\n    public async Task<List<Repository>> CreateClient()\\n    {\\n        dynamic restClient = _restClientFactory.CreateClient(\\\"MyNamedGitHubClient\\\"); //Get Client by name\\n    \\n        var repositories = await restClient.dotnet.repos.Get();\\n    \\n        return repositories;\\n    }\\n}\"), \"\\n        \"), mdx(\"blockquote\", null, mdx(\"strong\", null, \"For everything you need know about RestClient visit the RestClient site \", mdx(\"a\", {\n    href: \"https://restclient.dalsoft.io\"\n  }, \"https://restclient.dalsoft.io\"))));\n}\n;\nMDXContent.isMDXComponent = true;","frontmatter":{"title":"RestClient 3.3.0 Gets IHttpClientFactory Support","date":"08 August 2018","description":"Update we made to our C# Rest Client to support IHttpClientFactory.","banner":{"publicURL":"/static/91c53f91c60b9e9303df797ef03c582e/banner.jpg","childImageSharp":{"fluid":{"srcSet":"/static/91c53f91c60b9e9303df797ef03c582e/d34c9/banner.jpg 480w,\n/static/91c53f91c60b9e9303df797ef03c582e/f7c79/banner.jpg 960w,\n/static/91c53f91c60b9e9303df797ef03c582e/85bc9/banner.jpg 1920w,\n/static/91c53f91c60b9e9303df797ef03c582e/79b53/banner.jpg 2426w","base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAIABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAID/8QAFQEBAQAAAAAAAAAAAAAAAAAAAAH/2gAMAwEAAhADEAAAAazCRH//xAAYEAEAAwEAAAAAAAAAAAAAAAACAQMSIf/aAAgBAQABBQKrLm0wFzP/xAAVEQEBAAAAAAAAAAAAAAAAAAABEP/aAAgBAwEBPwEn/8QAFREBAQAAAAAAAAAAAAAAAAAAARD/2gAIAQIBAT8BZ//EABoQAAICAwAAAAAAAAAAAAAAAAABEUECIVH/2gAIAQEABj8CyTU8FFi1R//EABkQAQACAwAAAAAAAAAAAAAAAAEAESExYf/aAAgBAQABPyE4wW1yPoogdVH/2gAMAwEAAgADAAAAEAvv/8QAFhEBAQEAAAAAAAAAAAAAAAAAAQAR/9oACAEDAQE/EE7k3//EABYRAQEBAAAAAAAAAAAAAAAAAAEAEf/aAAgBAgEBPxAGbBf/xAAZEAEBAAMBAAAAAAAAAAAAAAABEQAhMfD/2gAIAQEAAT8Qlt7hNseLMS2PkwYjsVJ2vc//2Q==","aspectRatio":2.4,"src":"/static/91c53f91c60b9e9303df797ef03c582e/85bc9/banner.jpg","sizes":"(max-width: 1920px) 100vw, 1920px"},"id":"1f24b1bd-b347-5dc7-b99e-4ae947fe9ce6"}}}}},"pageContext":{"slug":"/blog/index.php/2018/08/08/restclient-3-3-0-gets-ihttpclientfactory-support/"}},
    "staticQueryHashes": ["1139857438","1946588481","2083862410","2213455283","2418326273","2840686334","3067102388"]}