<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>httpclient asp net core &#8211; ASP.NET Hosting Reviews and Guides</title>
	<atom:link href="https://topreviewhostingasp.net/tag/httpclient-asp-net-core/feed/" rel="self" type="application/rss+xml" />
	<link>https://topreviewhostingasp.net</link>
	<description>ASP.NET Hosting &#124; Reviews &#124; Tips &#38; Tutorial</description>
	<lastBuildDate>Tue, 23 Feb 2021 03:17:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://topreviewhostingasp.net/wp-content/uploads/2017/01/cropped-trhaico-32x32.png</url>
	<title>httpclient asp net core &#8211; ASP.NET Hosting Reviews and Guides</title>
	<link>https://topreviewhostingasp.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Using IHttpClientFactory in ASP.NET Core</title>
		<link>https://topreviewhostingasp.net/using-ihttpclientfactory-in-asp-net-core/</link>
					<comments>https://topreviewhostingasp.net/using-ihttpclientfactory-in-asp-net-core/#respond</comments>
		
		<dc:creator><![CDATA[Jacques Hunt]]></dc:creator>
		<pubDate>Tue, 23 Feb 2021 03:16:00 +0000</pubDate>
				<category><![CDATA[Hosting Tips]]></category>
		<category><![CDATA[asp.net core]]></category>
		<category><![CDATA[asp.net core tips]]></category>
		<category><![CDATA[asp.net core tutorial]]></category>
		<category><![CDATA[asp.net tips]]></category>
		<category><![CDATA[asp.net tutorial]]></category>
		<category><![CDATA[httpclient asp net core]]></category>
		<guid isPermaLink="false">https://topreviewhostingasp.net/?p=2928</guid>

					<description><![CDATA[In today’s post, we will see how to create named HTTPClient requests using HTTPClientFactory in .NET Core or ASP.NET Core. Create a Named HTTPClient Create an ASP.NET Core Project Let’s look at step by step to understand and create named HTTPClient approach where we shall be creating HTTPClient request object using HTTPClientFactory. Interface IHTTPClientFactory  can be injected in Controller [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>In today’s post, we will see how to create named HTTPClient requests using <strong>HTTPClientFactory </strong>in .NET Core or ASP.NET Core.</p>



<h2 class="wp-block-heading">Create a Named HTTPClient</h2>



<p><strong>Create an ASP.NET Core Project</strong></p>



<figure class="wp-block-image size-large"><img decoding="async" class="wp-image-2929" src="https://topreviewhostingasp.net/wp-content/uploads/2021/02/image_1.jpeg" alt="" /></figure>



<p>Let’s look at step by step to understand and create named HTTPClient approach where we shall be creating HTTPClient request object using HTTPClientFactory.</p>



<p>Interface <strong>IHTTPClientFactory  </strong>can be injected in Controller or any other class as needed using Constructor injection as below,</p>



<figure class="wp-block-image size-large"><img decoding="async" class="wp-image-2930" src="https://topreviewhostingasp.net/wp-content/uploads/2021/02/image_2.jpeg" alt="" /></figure>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><em>Add using namespace ‘System.Net.Http’ in the code to access HTTPClient and IHTTPClientFactory </em></p>
</blockquote>



<p>Please update the <strong>ConfigureServices </strong>method in Startup.cs as below,</p>



<p>Here in below example, we are creating two types of Named client,</p>



<ul>
<li><strong>AccountClient</strong> – This client is configured for AccountSpecific request and accept ‘<strong>application/json</strong>‘ as <strong>Content-type</strong></li>
<li><strong>PayBillClient</strong> – This client is configured for Payment and Bill request and accept ‘<strong>application/xml</strong>‘ as <strong>Content-type</strong>. It also configured with security using the <strong>BasicAuthentication </strong>scheme.</li>
</ul>



<pre class="wp-block-code"><code>public void ConfigureServices(IServiceCollection services)
       {
           services.AddControllers();
           services.AddHttpClient("AccountClient", c =&gt;
           {
               c.BaseAddress = new Uri(Configuration.GetValue&lt;string&gt;("AccountURL"));
               // Account API ContentType
               c.DefaultRequestHeaders.Add("Accept", "application/json");
           });
           services.AddHttpClient("PayBillClient", c =&gt;
           {
               c.BaseAddress = new Uri(Configuration.GetValue&lt;string&gt;("PayBillURL"));
               // Account API ContentType
               c.DefaultRequestHeaders.Add("Accept", "application/xml");
           });
       }</code></pre>



<p>Below is the client-side code base for named HTTPClient.</p>



<p>Here we specify the name of the HTTPClient request using an overloaded method <strong>CreateClient(“client name”)</strong>.</p>



<pre class="wp-block-code"><code>[HttpGet]
        public async Task&lt;IActionResult&gt; OnGet()
        {
            var uri = new Uri("https://localhost:44364/account");
            var client = _clientFactory.CreateClient("AccountClient");
            var response = await client.GetAsync(uri);
            if (response.IsSuccessStatusCode)
            {
                return Ok(response.Content.ReadAsStreamAsync().Result);
            }
            else
            {
                return StatusCode(500, "Somthing Went Wrong! Error Occured");
            }
        }</code></pre>



<p>Similarly, other named <strong>HTTPClient </strong>for “<strong>PayBillClient</strong>” can be created as below,</p>



<pre class="wp-block-code"><code>var client = _clientFactory.CreateClient("PayBillClient");</code></pre>



<p>As we understood above HTTPClientFactory lets you DI inject the HTTPClient objects using an explicit Dependency Injection principle(DI).</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><em>This technique can be used configure multiple HTTPClients request with custom configuration of </em><strong>Policy, Security </strong><em>and </em><strong>delegates </strong><em>as required.</em></p>
</blockquote>



<p>This technique lets you control the lifetime management of HTTPClient instances through the API pipeline itself.</p>



<p>With this technique, we are able to centralize multiple clients with specific configurations like using network credentials, specific headers or security tokens as needed.</p>



<p>That’s All, Happy coding !!. Please sound off your comments below.</p>



<h2 class="wp-block-heading">Summary</h2>



<p>In this article, we looked at how to use HTTPClientFactory for creating <strong>Named HTTPClient </strong>request object to invoke HTTP services in ASP.NET Core. This technique also lets you control custom clients based on the required policy and delegates.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://topreviewhostingasp.net/using-ihttpclientfactory-in-asp-net-core/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
