<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Xhanti Mda</title>
    <description>The latest articles on DEV Community by Xhanti Mda (@xhantimda).</description>
    <link>https://dev.to/xhantimda</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F24871%2Fa3d45397-8362-42dd-93e0-9ea8a6f9cd46.jpg</url>
      <title>DEV Community: Xhanti Mda</title>
      <link>https://dev.to/xhantimda</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/xhantimda"/>
    <language>en</language>
    <item>
      <title>Introducing the HttpClientFactory</title>
      <dc:creator>Xhanti Mda</dc:creator>
      <pubDate>Mon, 13 May 2019 07:10:39 +0000</pubDate>
      <link>https://dev.to/xhantimda/introducing-the-httpclientfactory-3ege</link>
      <guid>https://dev.to/xhantimda/introducing-the-httpclientfactory-3ege</guid>
      <description>&lt;p&gt;A few years ago, I came across a blog post by the ASP.NET Monsters which was titled : &lt;a href="https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/" rel="noopener noreferrer"&gt;YOU'RE USING HTTPCLIENT WRONG AND IT IS DESTABILIZING YOUR SOFTWARE&lt;/a&gt;. This post shared a trick that I've used numerous times to avoid socket exhaustion by not disposing of the HttpClient. &lt;/p&gt;

&lt;h2&gt;
  
  
  To dispose, or not to dispose, that is the question.
&lt;/h2&gt;

&lt;p&gt;Creating a HttpClient instance, internally creates a HttpMessageHandler chain. It's the handler chain that will be responsible for establishing a connection.&lt;br&gt;
So when we dispose of the HttpClient, we actually dispose of the HttpMessageHandler which will eventually close the connection.&lt;br&gt;
The risk of this is that the connection doesn't get closed as soon as the client is disposed of.&lt;br&gt;
When we actively close a connection, the port is not immediately released, it goes into a TIME_WAIT state.&lt;br&gt;
If this continues to happen, it will lead to socket exhaustion.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff5kyld2pc67jjwen2bot.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff5kyld2pc67jjwen2bot.png" alt="TIME_WAIT"&gt;&lt;/a&gt;&lt;/p&gt;
An example of connections in the TIME_WAIT state.



&lt;p&gt;A solution to the above issue was to have a single instance of a HttpClient that would be shared across the application.&lt;br&gt;
The problem with the above solution is that connections would be kept open and reused forever and any DNS changes made on the remote services wouldn't be honoured.&lt;br&gt;
This would lead to applications pointing to offline server instances or incorrect environments.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Introducing the HttpClientFactory
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The HttpClientFactory was introduced with .NETCore 2.1. &lt;/li&gt;
&lt;li&gt;The HttpClientFactory gives us a central place to configure and create HttpClients.&lt;/li&gt;
&lt;li&gt;It manages the lifetime of the underlying HttpMessageHandler chains. It does this by managing a pool of these MessageHandlers and keeping them open until they expire.&lt;/li&gt;
&lt;li&gt;The MessageHandlers are kept alive for a maximum of 2 minutes (by default).&lt;/li&gt;
&lt;li&gt;This gives us the best of both, holding on to connections long enough while still honouring DNS changes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Patterns of consuming the HttpClientFactory
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Direct Usage
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Simple usage of the HttpClientFactory requires minimal code changes in your application.&lt;/li&gt;
&lt;li&gt;Install the Microsoft.Extensions.Http nuget package if your application does not reference the Microsoft.AspNetCore.App metapackage.&lt;/li&gt;
&lt;li&gt;Just by calling the AddHttpClient extension method on the IServiceCollection gives you the goodies of instance management out of the box.&lt;/li&gt;
&lt;li&gt;It allows you to inject the HttpClientFactory anywhere in your application, which you can use to create HttpClient instances with.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwphrr3hvmtsvf12n61i0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwphrr3hvmtsvf12n61i0.png" alt="AddHttpClient"&gt;&lt;/a&gt;&lt;/p&gt;
An example of the direct usage of the HttpClientFactory.



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhbnqgxn00hmjnh6vclp2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhbnqgxn00hmjnh6vclp2.png" alt="InjectClient"&gt;&lt;/a&gt;&lt;/p&gt;
An example of injecting HttpClientFactory.



&lt;h3&gt;
  
  
  Named Clients
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Named clients gives us the ability to create logical configurations for different services that our application communicates with.&lt;/li&gt;
&lt;li&gt;Configuring a named client is as simple as just calling the AddHttpClient method, passing in the name of the client and a delegate which takes in a HttpClient. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F09wdgaug596b0sxxt0a9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F09wdgaug596b0sxxt0a9.png" alt="ConfigureNamedClient"&gt;&lt;/a&gt;&lt;/p&gt;
An example of configuring a named HttpClient.



&lt;ul&gt;
&lt;li&gt;To access a named HttpClient, you just have to pass the name of the configured client to the CreateClient method on IHttpClientFactory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F78tzol5yybor68tlpsb7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F78tzol5yybor68tlpsb7.png" alt="ConsumeNamedClient"&gt;&lt;/a&gt;&lt;/p&gt;
An example of consuming a named HttpClient.



&lt;h3&gt;
  
  
  Typed Clients
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Typed clients are the recommended (by Microsoft) way of consuming HttpClient.&lt;/li&gt;
&lt;li&gt;A typed client is just a class that accepts a HttpClient as a constructor parameter.&lt;/li&gt;
&lt;li&gt;Typed clients give us the benefit of encapsulating service-specific code into a single class.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxxxdzpa7kgqrnc65xrkh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxxxdzpa7kgqrnc65xrkh.png" alt="CreateTypedClient"&gt;&lt;/a&gt;&lt;/p&gt;
An example of creating a typed HttpClient.



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzltlpmy4ya6queha5ecw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzltlpmy4ya6queha5ecw.png" alt="ConfigureTypedClient"&gt;&lt;/a&gt;&lt;/p&gt;
An example of configuring a typed HttpClient.



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fchgs9mb2rntfzyhu27fb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fchgs9mb2rntfzyhu27fb.png" alt="ConsumingTypedClient"&gt;&lt;/a&gt;&lt;/p&gt;
An example of consuming a typed HttpClient.



&lt;h3&gt;
  
  
  Bonus
&lt;/h3&gt;

&lt;p&gt;You may have noticed the FromServices Attribute used in the GetMorty action and asked yourself WTF! &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The FromServices attribute gets the action parameter from the container. &lt;/li&gt;
&lt;li&gt;This happens during the ModelBinding stage in the MVC request lifecycle after the ControllerActionInvoker finds the matching action in the controller, based on the request. &lt;/li&gt;
&lt;li&gt;An instance of the type is bound to the parameter of the action just before it gets invoked.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F354dtpsxtcn35wj3dsh0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F354dtpsxtcn35wj3dsh0.png" alt="ConsumingFromServices"&gt;&lt;/a&gt;&lt;/p&gt;
An example of consuming the FromServices attribute.



&lt;h3&gt;
  
  
  Final words
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use the HttpClientFactory for more resilient HTTP calls.&lt;/li&gt;
&lt;li&gt;You can override the default HttpMessageHandler lifetime by chaining the SetLifeTime method to the AddClient method when configuring HttpClient. &lt;/li&gt;
&lt;li&gt;As recommended, use Typed Clients were possible.&lt;/li&gt;
&lt;li&gt;Access sample source code &lt;a href="https://github.com/XhantiMda/RickAndMortyApi" rel="noopener noreferrer"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>aspnetcore</category>
      <category>csharp</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>Filters with Dependency Injection in ASP.NET CORE</title>
      <dc:creator>Xhanti Mda</dc:creator>
      <pubDate>Mon, 18 Mar 2019 06:18:36 +0000</pubDate>
      <link>https://dev.to/xhantimda/filters-with-dependency-injection-in-aspnet-core-1l8c</link>
      <guid>https://dev.to/xhantimda/filters-with-dependency-injection-in-aspnet-core-1l8c</guid>
      <description>

&lt;p&gt;Recently I wanted to add a logger to a custom authorization filter(which was not the best idea) but I didn't want to new up an instance inside the filter, I wanted DI to handle all of that for me.  &lt;/p&gt;

&lt;p&gt;I did some digging and came across the &lt;strong&gt;TypeFilterAttribute&lt;/strong&gt;, &lt;strong&gt;ServiceFilterAttribute&lt;/strong&gt; and the &lt;strong&gt;IFilterFactory&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;IFilterFactory&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;IFilterFactory interface allows you to create an instance of your filter by implementing the CreateInstance method inside your filter.&lt;br&gt;
When MVC invokes a filter, it first tries to cast it to an IFilterFactory. If that cast succeeds, it then calls the CreateInstance method.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;  &lt;/div&gt;
&lt;br&gt;
  An example of a filter implementing IFilterFactory

&lt;h2&gt;
  
  
  &lt;strong&gt;TypeFilter &amp;amp; ServiceFilter&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;ServiceFilterAttribute&lt;/strong&gt; implements the IFilterFactory. IFilterFactory exposes the CreateInstance method for creating an IFilterMetadata instance. The CreateInstance method loads the specified type from the container.&lt;br&gt;
Filters used with the ServiceFilterAttribute need to be registered with the container.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;  &lt;/div&gt;

An example of registering a filter.


&lt;div class="ltag_gist-liquid-tag"&gt;  &lt;/div&gt;

An example of how the ServiceFilterAttribute can be used

&lt;p&gt;The &lt;strong&gt;TypeFilterAttribute&lt;/strong&gt;  is similar to the &lt;strong&gt;ServiceFilterAttribute&lt;/strong&gt;, but its type isn't resolved directly from the DI container. It instantiates the type by using Microsoft.Extensions.DependencyInjection.ObjectFactory.&lt;/p&gt;

&lt;p&gt;This means that types referenced using the &lt;strong&gt;TypeFilterAttribute&lt;/strong&gt; don't need to be registered with the container first and the &lt;strong&gt;TypeFilterAttribute&lt;/strong&gt; can optionally accept constructor arguments for the type.&lt;/p&gt;

&lt;p&gt;When using the &lt;strong&gt;TypeFilterAttribute&lt;/strong&gt;, setting IsReusable is a hint that the filter instance may be reused outside of the request scope it was created within. The framework provides no guarantees that a single instance of the filter will be created. Avoid using IsReusable when using a filter that depends on services with a lifetime other than singleton.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;  &lt;/div&gt;

An example of how the TypeFilterAttribute can be used

&lt;p&gt;The sample code can be found &lt;a href="https://github.com/XhantiMda/FilterDI"&gt;here&lt;/a&gt;&lt;/p&gt;


</description>
      <category>aspnetcore</category>
      <category>csharp</category>
      <category>dotnet</category>
    </item>
  </channel>
</rss>
