<?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>How to fix - TheCodeBuzz</title>
	<atom:link href="https://thecodebuzz.com/category/how-to-fix/feed/" rel="self" type="application/rss+xml" />
	<link>https://thecodebuzz.com</link>
	<description>Best Practices for Software Development</description>
	<lastBuildDate>Sat, 09 Mar 2024 22:08:58 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://thecodebuzz.com/wp-content/uploads/2022/11/cropped-android-chrome-512x512-1-1-51x51.jpg</url>
	<title>How to fix - TheCodeBuzz</title>
	<link>https://thecodebuzz.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Resolved- Unable to resolve service for type while attempting to activate</title>
		<link>https://thecodebuzz.com/unable-to-resolve-service-for-type-while-attempting-to-activate/</link>
					<comments>https://thecodebuzz.com/unable-to-resolve-service-for-type-while-attempting-to-activate/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Mon, 20 Nov 2023 01:13:06 +0000</pubDate>
				<category><![CDATA[How to fix]]></category>
		<category><![CDATA[Unable to resolve service for type while attempting to activate]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=28949</guid>

					<description><![CDATA[<p>Resolved- Unable to resolve service for type while attempting to activate Issue description: While using .NET API project and attempting to run application, it gives below error, InvalidOperationException: Unable to resolve service for type 'IEmployeeRepository ' while attempting to activate 'EmployeeController'. Sampel code which produces this error Please refer this article for complete code Repository [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/unable-to-resolve-service-for-type-while-attempting-to-activate/">Resolved- Unable to resolve service for type while attempting to activate</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">Resolved- Unable to resolve service for type while attempting to activate</h1>



<h2 class="wp-block-heading"><strong>Issue description:</strong></h2>



<p></p>



<p>While using .NET API project and attempting to run application, it gives below error,</p>



<p></p>



<pre class="wp-block-preformatted has-vivid-red-color has-text-color has-link-color wp-elements-8a78085755d5e8d5f8cfc2553f00a816">InvalidOperationException: Unable to resolve service for type 'IEmployeeRepository ' while attempting to activate 'EmployeeController'.</pre>



<p></p>



<h3 class="wp-block-heading">Sampel code which produces this error</h3>



<p></p>



<p>Please refer <a href="https://www.thecodebuzz.com/entity-framework-repository-implementation-efcore-net-core/" target="_blank" rel="noopener" title="">this </a>article for complete code </p>



<p></p>



<pre class="wp-block-code"><code>    &#91;Route("api/&#91;controller]")]
    &#91;ApiController]
    public class EmployeeController : ControllerBase
    {
        private readonly IEmployeeRepository _employeeRepository;
        public EmployeeController(IEmployeeRepository employeeRepository)
        {
            _employeeRepository = employeeRepository;
        }
        // GET: api/Employee
        &#91;HttpGet]
        public ActionResult&lt;IEnumerable&lt;Employee&gt;&gt; Get()
        {
            return Ok(_employeeRepository.GetEmployees());
        }
        // GET: api/Employee/5
        &#91;HttpGet("{id}")]
        public ActionResult Get(int id)
        {
            return Ok(_employeeRepository.GetEmployeeByID(id));
        }</code></pre>



<p></p>



<p>Repository <strong>Interfaces</strong> was be defined as below,</p>



<p></p>



<pre class="wp-block-code"><code>public interface IEmployeeRepository
{
    IEnumerable&lt;Employee&gt; GetEmployees();
    Employee GetEmployeeByID(int employeeID);
}</code></pre>



<p></p>



<h2 class="wp-block-heading"><strong>Root cause and Issue resolution </strong></h2>



<p></p>



<p>The main reason for issue was found to be not registering the instance type correctly in the IOC contaier of API pipeline or middleware..</p>



<p></p>



<p>After adding the type and interface registration in the Container as below, the issue got resolved,</p>



<p></p>



<pre class="wp-block-code"><code><em>services.AddScoped&lt;IEmployeeRepository, EmployeeRepository&gt;();</em></code></pre>



<p></p>



<p>Below is the complete sample code,</p>



<pre class="wp-block-code"><code>
public void ConfigureServices(IServiceCollection services)
       {
           services.AddControllers();
           <em>services.AddScoped&lt;IEmployeeRepository, EmployeeRepository&gt;();</em>
           services.AddDbContext&lt;EmployeeContext&gt;(options =&gt;
           {
               options.UseSqlServer(Configuration.GetConnectionString("EmployeeDB"),
                sqlServerOptionsAction: sqlOptions =&gt;
                {
                    sqlOptions.EnableRetryOnFailure();
                });
           });
       }</code></pre>



<p></p>



<p>If you are using .NET 6 and above framework please define the type as below,</p>



<p></p>



<pre class="wp-block-code"><code>  <em>builder</em>.<em>services.AddScoped&lt;IEmployeeRepository, EmployeeRepository&gt;();</em></code></pre>



<p></p>



<p><a href="https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection#service-lifetimes" target="_blank" rel="noopener" title="">Service lifetime</a> can be defined using any the below approaches as below,</p>



<p></p>



<ul class="wp-block-list">
<li>Transient</li>



<li>Scoped</li>



<li>Singleton</li>
</ul>



<p></p>



<h2 class="wp-block-heading">Other possible root cause</h2>



<p></p>



<p>Please ensure that you are injecting <strong><em>Interface </em></strong>in Controller and not the class type from Controller or other module where you want to use that type or service.</p>



<p></p>



<p></p>



<h3 class="wp-block-heading">Example: Failed </h3>



<pre class="wp-block-code"><code>        private readonly IEmployeeRepository _employeeRepository;
        public EmployeeController(EmployeeRepository employeeRepository)
        {
            _employeeRepository = employeeRepository;
        }</code></pre>



<p></p>



<h3 class="wp-block-heading">Example: Working</h3>



<pre class="wp-block-code"><code>        private readonly IEmployeeRepository _employeeRepository;
        public EmployeeController(IEmployeeRepository employeeRepository)
        {
            _employeeRepository = employeeRepository;
        }
</code></pre>



<p></p>



<p>That&#8217;s all! Happy coding!</p>



<p></p>



<p>Does this help you fix your issue? </p>



<p></p>



<p>Do you have any better solutions or suggestions? Please sound off your comments below.</p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/unable-to-resolve-service-for-type-while-attempting-to-activate/">Resolved- Unable to resolve service for type while attempting to activate</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/unable-to-resolve-service-for-type-while-attempting-to-activate/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>C# .NET &#8211; Compare Two Different Files line-by-line</title>
		<link>https://thecodebuzz.com/c-sharp-dot-net-core-compare-two-different-files-line-by-line/</link>
					<comments>https://thecodebuzz.com/c-sharp-dot-net-core-compare-two-different-files-line-by-line/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sat, 11 Mar 2023 02:15:56 +0000</pubDate>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[How to fix]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=26231</guid>

					<description><![CDATA[<p>C# .NET &#8211; Compare Two Different Files line-by-line Today in this article, we will learn a simple and easy approach In C# .NET &#8211; Compare Two Different Files line-by-line. You might find need to compare two files line by line. The file can be compared in a variety of methods to identify differences or common [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/c-sharp-dot-net-core-compare-two-different-files-line-by-line/">C# .NET – Compare Two Different Files line-by-line</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading"><strong>C# .NET &#8211; Compare Two Different Files line-by-line</strong></h1>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="634" height="539" src="https://www.thecodebuzz.com/wp-content/uploads/2023/03/c-File-Compare.jpg" alt="C# .NET - Compare Two Different Files line-by-line " class="wp-image-26247" srcset="https://thecodebuzz.com/wp-content/uploads/2023/03/c-File-Compare.jpg 634w, https://thecodebuzz.com/wp-content/uploads/2023/03/c-File-Compare-300x255.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/03/c-File-Compare-612x520.jpg 612w" sizes="(max-width: 634px) 100vw, 634px" /></figure>



<p>Today in this article, we will learn a simple and easy approach In C# .NET &#8211; Compare Two Different Files line-by-line. You might find need to compare two files line by line.</p>



<p></p>



<p>The file can be compared in a variety of methods to identify differences or common lines.</p>



<p></p>



<p>This piece walks readers through the process of comparing two files to determine whether their contents differ and how to create a file of differences if they do.</p>



<p></p>



<p>This comparison examines the contents of the two files line by line, checking each line for any order that may or may not correspond to other files&#8217; names, locations, times, or other characteristics.</p>



<p></p>



<p>Multiple modules can be used in C# to identify variations or commonalities.</p>



<p></p>



<p><strong>File A</strong> &#8211; We have the below file with the content.</p>



<p></p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="350" src="https://www.thecodebuzz.com/wp-content/uploads/2023/03/Compare-two-Files-line-by-line-in-Python-2-1024x350.jpg" alt="" class="wp-image-26244" srcset="https://thecodebuzz.com/wp-content/uploads/2023/03/Compare-two-Files-line-by-line-in-Python-2-1024x350.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/03/Compare-two-Files-line-by-line-in-Python-2-300x102.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/03/Compare-two-Files-line-by-line-in-Python-2-768x262.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/03/Compare-two-Files-line-by-line-in-Python-2-1536x524.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2023/03/Compare-two-Files-line-by-line-in-Python-2-785x268.jpg 785w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">#image_title</figcaption></figure>



<p></p>



<p><strong>File B </strong>&#8211; We have the below file with the content different from the previous file as highlighted.  </p>



<p></p>



<p></p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="367" src="https://www.thecodebuzz.com/wp-content/uploads/2023/03/Compare-two-different-files-line-by-line-in-python-2-1024x367.jpg" alt="" class="wp-image-26245" srcset="https://thecodebuzz.com/wp-content/uploads/2023/03/Compare-two-different-files-line-by-line-in-python-2-1024x367.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/03/Compare-two-different-files-line-by-line-in-python-2-300x108.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/03/Compare-two-different-files-line-by-line-in-python-2-768x275.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/03/Compare-two-different-files-line-by-line-in-python-2-1536x551.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2023/03/Compare-two-different-files-line-by-line-in-python-2-785x281.jpg 785w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">#image_title</figcaption></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-approach-1-using-linq-contains-method">Approach 1 – File Line compare using Linq Contains method</h2>



<p></p>



<p>Using LInq Contains method determines whether a sequence contains a specified element by using the default <strong>equality </strong>comparer.</p>



<p></p>



<p></p>



<pre class="wp-block-preformatted">var listA = File.ReadAllLines("DataA");
 
var listExceptAB = File.ReadAllLines("DataB")
.Where(line =>!listA.Contains(line));</pre>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-approach-2-using-linq-except">Approach 2 – File Line compare using Linq Except method</h2>



<p></p>



<pre class="wp-block-preformatted">var listA = File.ReadAllLines("DataA");
 
var listExceptAB = File.ReadAllLines("DataB")
.Except(listA );</pre>



<p></p>



<p>We can use a linq <strong>Except </strong>for method which helps produce a set difference of two file line by line by using the default equality comparer to compare values.</p>



<p></p>



<p>Except works fine on any <strong>IEnumerable </strong>properties as explained below</p>



<p></p>



<h2 class="wp-block-heading">Output</h2>



<p></p>



<p>Once the code is executed, you shall see the file is generated with only delta changes having the differences i.e return all the elements which exist in File A but don&#8217;t in File B.</p>



<p></p>



<pre class="wp-block-preformatted">27 APOSTROPHE A7 SECTION SIGN
23 NUMBER SIGN A3 POUND SIGN</pre>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/c-sharp-dot-net-core-compare-two-different-files-line-by-line/">C# .NET – Compare Two Different Files line-by-line</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/c-sharp-dot-net-core-compare-two-different-files-line-by-line/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Send HTTP POST request in .NET</title>
		<link>https://thecodebuzz.com/send-http-post-request-in-net-core-csharp/</link>
					<comments>https://thecodebuzz.com/send-http-post-request-in-net-core-csharp/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Thu, 29 Sep 2022 19:41:00 +0000</pubDate>
				<category><![CDATA[How to fix]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=23750</guid>

					<description><![CDATA[<p>Send HTTP POST request in .NET In today’s post, we will write client-side code to Send HTTP POST requests in .NET. We will see the technique of regular HTTPClient and also use HttpClientFactory (recommended) examples Let’s look at a few examples to invoke HTTP POST methods using HttpClientfactory. As discussed in the previous article I [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/send-http-post-request-in-net-core-csharp/">Send HTTP POST request in .NET</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading"><strong>Send HTTP POST request in .NET</strong></h1>



<figure class="wp-block-image size-large"><a href="https://www.thecodebuzz.com/using-httpclient-best-practices-and-anti-patterns/" target="_blank" rel="noopener"><img loading="lazy" decoding="async" width="1024" height="512" src="https://www.thecodebuzz.com/wp-content/uploads/2022/10/Send-HTTP-POST-request-in-NET_-1024x512.jpg" alt="" class="wp-image-23753" srcset="https://thecodebuzz.com/wp-content/uploads/2022/10/Send-HTTP-POST-request-in-NET_-1024x512.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2022/10/Send-HTTP-POST-request-in-NET_-300x150.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/10/Send-HTTP-POST-request-in-NET_-768x384.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2022/10/Send-HTTP-POST-request-in-NET_-1536x768.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2022/10/Send-HTTP-POST-request-in-NET_-785x393.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2022/10/Send-HTTP-POST-request-in-NET_.jpg 1830w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure>



<p>In today’s post, we will write client-side code to Send HTTP POST requests in .NET. </p>



<p></p>



<p>We will see the technique of regular HTTPClient and also use HttpClientFactory (recommended) examples </p>



<p></p>



<p>Let’s look at a few examples to invoke HTTP POST methods using <strong>HttpClientfactory</strong>.</p>



<p></p>



<p>As discussed in the previous article I have already setup the <a href="https://www.thecodebuzz.com/create-named-httpclient-ihttpclientfactory-asp-net-core/" target="_blank" rel="noreferrer noopener">named HttpClientclientFactory</a> to create the required HttpClient instance as below,</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="975" height="277" src="https://www.thecodebuzz.com/wp-content/uploads/2020/04/Post-example-httpclientfactory-.net-core-csharp.jpg" alt="HttpClientFactory GET, POST, PUT and DELETE example " class="wp-image-9581" srcset="https://thecodebuzz.com/wp-content/uploads/2020/04/Post-example-httpclientfactory-.net-core-csharp.jpg 975w, https://thecodebuzz.com/wp-content/uploads/2020/04/Post-example-httpclientfactory-.net-core-csharp-300x85.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/04/Post-example-httpclientfactory-.net-core-csharp-768x218.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/04/Post-example-httpclientfactory-.net-core-csharp-785x223.jpg 785w" sizes="auto, (max-width: 975px) 100vw, 975px" /></figure>



<p></p>



<h2 class="wp-block-heading">Send HTTP POST request in .NET</h2>



<p></p>



<p>Below is a sample <strong>POST </strong>method example. </p>



<p></p>



<p>Below we are creating an instance of <strong>HttpClient </strong>using <strong>HttpClientFactory </strong>and invoking the POST method asynchronously. </p>



<p></p>



<p>One can create a HttpClient instance using a static instance or named or typed instances as needed.</p>



<p></p>



<p><strong>Note: Below samples are shown using direct usage of Post call in Controller. </strong></p>



<p></p>



<p><strong>Ideally please structure your code considering the separation of concerns using Repository pattern etc.</strong></p>



<p></p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>Ideally, the POST method should also return the location of newly created resources as per REST guidelines.</p>



<p></p>
</blockquote>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
        &#x5B;HttpPost]
        public async Task&amp;lt;IActionResult&gt; Post(&#x5B;FromBody]Customer customer)
        {

            HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(customer)
                , Encoding.UTF8, &quot;application/json&quot;);

            HttpResponseMessage response = await client.PostAsync(&quot;api/acounts&quot;, httpContent);

          

            if (response.IsSuccessStatusCode)
            {
                return Ok(response.Content.ReadAsStreamAsync().Result);
            }

            else
            {
                return StatusCode(500, &quot;Something Went Wrong! Error Occured&quot;);
            }

        }

</pre></div>


<p></p>



<p>The above-discussed HttpClient object can be created using any of the below methods mentioned and can be initialized in the Controller constructor,</p>



<p></p>



<p> <li><a rel="noreferrer noopener" href="https://www.thecodebuzz.com/httpclient-using-httpclientfactory-asp-net-core/" target="_blank"><strong><em>Basic HTTPClient</em></strong></a></li></p>



<p><li><a rel="noreferrer noopener" href="https://www.thecodebuzz.com/create-named-httpclient-ihttpclientfactory-asp-net-core/" target="_blank"><strong><em>Named HTTPClient</em></strong></a></li></p>



<p><li><a rel="noreferrer noopener" href="https://www.thecodebuzz.com/typed-httpclient-using-httpclientfactory-in-asp-net-core/" target="_blank"><strong><em>Typed HTTPClient</em></strong></a></li></p>



<p></p>



<h2 class="wp-block-heading">HttpClientfactory alternative ?</h2>



<p></p>



<p>If you are not using the HttpClientfactory instance then please see below the way to initialize the HttpClient instance,</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2022/10/static-singleton-HTTPClient-in-NET-Core-Best-Practices-1024x301.jpg" alt="Send HTTP POST request in .NET" class="wp-image-23752" width="560" height="164" srcset="https://thecodebuzz.com/wp-content/uploads/2022/10/static-singleton-HTTPClient-in-NET-Core-Best-Practices-1024x301.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2022/10/static-singleton-HTTPClient-in-NET-Core-Best-Practices-300x88.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/10/static-singleton-HTTPClient-in-NET-Core-Best-Practices-768x226.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2022/10/static-singleton-HTTPClient-in-NET-Core-Best-Practices-1536x452.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2022/10/static-singleton-HTTPClient-in-NET-Core-Best-Practices-785x231.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2022/10/static-singleton-HTTPClient-in-NET-Core-Best-Practices.jpg 1571w" sizes="auto, (max-width: 560px) 100vw, 560px" /></figure>



<p></p>



<p><strong><em>References</em></strong>:</p>



<p></p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/using-httpclient-best-practices-and-anti-patterns/" target="_blank" rel="noreferrer noopener"><strong><em>Using HTTPClient Best Practices and Anti-Patterns</em></strong></a></li>
</ul>



<p></p>



<p></p>



<p>That&#8217;s all! Happy coding!</p>



<p></p>



<p>Does this help you fix your issue? </p>



<p></p>



<p>Do you have any better solutions or suggestions? Please sound off your comments below.</p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/send-http-post-request-in-net-core-csharp/">Send HTTP POST request in .NET</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/send-http-post-request-in-net-core-csharp/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Inject service in AutoMapper profile class</title>
		<link>https://thecodebuzz.com/how-to-inject-service-in-automapper-profile-class/</link>
					<comments>https://thecodebuzz.com/how-to-inject-service-in-automapper-profile-class/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 19 Jun 2022 01:11:00 +0000</pubDate>
				<category><![CDATA[How to fix]]></category>
		<category><![CDATA[automapper constructservicesusing]]></category>
		<category><![CDATA[automapper dependency injection]]></category>
		<category><![CDATA[automapper inject service into profile]]></category>
		<category><![CDATA[automapper resolveusing]]></category>
		<category><![CDATA[automapper type converter dependency injection automapper dependency injection net 6]]></category>
		<category><![CDATA[automapper unity registration]]></category>
		<category><![CDATA[automapper without dependency injection]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=22913</guid>

					<description><![CDATA[<p>How to Inject service in AutoMapper profile class Today in this article, we will see how to Inject service in AutoMapper profile class. While setting up the Automapper profile during Startup services bootstrap you might want to use services within in Automapper profile classes. Today in this article, we will cover below aspects, It&#8217;s easy [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/how-to-inject-service-in-automapper-profile-class/">How to Inject service in AutoMapper profile class</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">How to Inject service in AutoMapper profile class</h1>



<figure class="wp-block-image size-large is-resized"><a href="https://www.thecodebuzz.com/how-to-inject-service-in-automapper-profile-class/" target="_blank" rel="noopener"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2022/07/Inject-service-in-AutoMapper-profile-class-1024x320.jpg" alt="how-inject-service-in-automapper-profile-class" class="wp-image-22918" width="840" height="262" srcset="https://thecodebuzz.com/wp-content/uploads/2022/07/Inject-service-in-AutoMapper-profile-class-1024x320.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2022/07/Inject-service-in-AutoMapper-profile-class-300x94.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/07/Inject-service-in-AutoMapper-profile-class-768x240.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2022/07/Inject-service-in-AutoMapper-profile-class-1536x480.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2022/07/Inject-service-in-AutoMapper-profile-class-785x245.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2022/07/Inject-service-in-AutoMapper-profile-class.jpg 1855w" sizes="auto, (max-width: 840px) 100vw, 840px" /></a></figure>



<p>Today in this article, we will see how to Inject service in AutoMapper profile class. While setting up the Automapper profile during Startup services bootstrap you might want to use services within in Automapper profile classes. </p>



<p>Today in this article, we will cover below aspects,</p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-define-the-automapper-profile-with-service-dependency-injection">Define the Automapper Profile with service Dependency Injection</a></li><li><a href="#aioseo-define-configservices-in-startup">Define ConfigServices in Startup</a></li></ul></div>



<p>It&#8217;s easy to configure services using the explicit dependency injection principle.</p>



<p></p>



<p>Service injection into the AutoMapper profile can be done and it&#8217;s done very rarely. But if you have valid use cases and want to perform some conditional mapping or want to consumer values of injected services from other modules or middleware then the below approach is very simple.</p>



<p></p>



<p></p>



<p></p>



<p>As we already discussed in our last article,</p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p><strong><em>AutoMapper&nbsp;</em></strong>is an object mapper that helps you transform one object of one type into an output object of a different type. This type of transformation is often needed when you work on business logic.</p></blockquote>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Automapper helps to&nbsp;<strong><em>centralize your repeated</em></strong>&nbsp;boring mapping logic, address the&nbsp;<strong><em>separation of concerns</em></strong>, provide&nbsp;<strong><em>better control on mapping</em></strong>&nbsp;making your&nbsp;<strong><em>code clean and easy to maintain</em></strong>.</p></blockquote>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Here the idea with Automapper usage is simple, You have&nbsp;<strong>source&nbsp;</strong>type and&nbsp;<strong>destination&nbsp;</strong>type. You just transform object values from one source to the destination object. All you need is two types and profile registration. We will see what is profile registration in detail below.</p></blockquote>



<p></p>



<p>For more details on dependency Injection of the <strong>IMapper </strong>interface is explained here already with more details,</p>



<p></p>



<ul class="wp-block-list"><li><a href="https://www.thecodebuzz.com/configure-automapper-asp-net-core-profile-map-object/" target="_blank" rel="noreferrer noopener"><strong><em>Configure Automapper in ASP.NET Core – Getting Started</em></strong></a></li></ul>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-define-the-automapper-profile-with-service-dependency-injection">Define the Automapper Profile with service Dependency Injection</h2>



<p></p>



<p>Automapper Profile with service Dependency Injection can be defined as below.</p>



<p></p>



<p>Below example &#8220;<strong><em>SourceMappingProfile</em></strong>&#8221; is a profile class with constructor injection of service instance type i.e <strong><em>IEmployeeType</em></strong>.</p>



<p></p>



<p>Below I am defining the conditional profile for Automapper depending on the IEmployeeType.</p>



<p></p>



<p><strong><em>Example </em></strong></p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; highlight: [3,9]; title: ; notranslate">
    public class SourceMappingProfile : Profile
    {
        private IEmployeeType _employeeType;

        public SourceMappingProfile()
        {
        }

        public SourceMappingProfile(IEmployeeType employeeType)
        {
            _employeeType = employeeType;

            if (_employeeType.empType == EmpType.Contract)
            {
                CreateMap&lt;Source, Destination&gt;()
                    .ForMember(dest =&gt; dest.Name, o =&gt; o.MapFrom(src =&gt; src.FirstName))
                    .ForMember(dest =&gt; dest.Location, o =&gt; o.MapFrom(src =&gt; src.Address))
                    .ForMember(dest =&gt; dest.Employee, o =&gt; o.MapFrom(src =&gt; src.Employee))
                    .ForMember(dest =&gt; dest.Identity, opt =&gt;
                    {
                        opt.PreCondition(src =&gt; (src.Id != null));
                        opt.MapFrom(src =&gt; src.Id);
                    });
            }

            if (_employeeType.empType == EmpType.Permanent)
            {
                CreateMap&lt;Source, Destination&gt;()
                    .ForMember(dest =&gt; dest.Name, o =&gt; o.MapFrom(src =&gt; src.FirstName));
            }
        }
    }
</pre></div>


<p></p>



<h2 class="wp-block-heading" id="aioseo-define-configservices-in-startup">Define ConfigServices in Startup </h2>



<p></p>



<p>I have defined the  Define ConfigServices in Startup  as below,</p>



<p></p>



<p>Example</p>



<p></p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
            services.AddScoped&lt;ValidateInputAttribute&lt;string&gt;&gt;();
            services.AddScoped&lt;IUniqueIdService, UniqueIdService&gt;();
            services.AddScoped&lt;IEmployeeType,EmployeeType&gt;();

            services.AddSingleton(provider =&gt; new MapperConfiguration(cfg =&gt;
            {
                cfg.AddProfile(new SourceMappingProfile(provider.CreateScope().ServiceProvider.GetService&lt;IEmployeeType&gt;()));

            }).CreateMapper());

        }
</pre></div>


<p></p>



<p>Above we are using scoped service instance.  While scope service instances are created once per request it is often recommended they are resolved using a scoped instance of Application Builder. Then followed by using a service provider one can resolve the dependencies. </p>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p>



<p></p>



<p></p>



<p></p>



<p></p>



<p></p><p>The post <a href="https://thecodebuzz.com/how-to-inject-service-in-automapper-profile-class/">How to Inject service in AutoMapper profile class</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/how-to-inject-service-in-automapper-profile-class/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>MongoDB ObjectId Data Type &#8211; Guidelines</title>
		<link>https://thecodebuzz.com/what-is-mongodb-objectid-data-type-guidelines/</link>
					<comments>https://thecodebuzz.com/what-is-mongodb-objectid-data-type-guidelines/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Mon, 06 Jun 2022 03:50:00 +0000</pubDate>
				<category><![CDATA[How to fix]]></category>
		<category><![CDATA[generate mongodb id javascript]]></category>
		<category><![CDATA[mongodb new objectid]]></category>
		<category><![CDATA[mongodb objectid example]]></category>
		<category><![CDATA[mongodb objectid generator]]></category>
		<category><![CDATA[mongodb objectid to string how to get objectid in mongodb using java]]></category>
		<category><![CDATA[mongodb objectid to timestamp]]></category>
		<category><![CDATA[pymongo objectid]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=22603</guid>

					<description><![CDATA[<p>MongoDB ObjectId Data Type &#8211; Guidelines Today in this article, we will see more details about MongoDB ObjectId Data type. ObjectId is the 12-byte element consisting of a timestamp value, a random value, and an incremental counter. Today in this article, we will cover below aspects, MongoDB _id field as ObjectId MongoDB uses the _id field [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/what-is-mongodb-objectid-data-type-guidelines/">MongoDB ObjectId Data Type – Guidelines</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">MongoDB ObjectId Data Type &#8211; Guidelines</h1>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2022/06/MongoDB-using-ObjectId-1024x292.jpg" alt="" class="wp-image-22613" width="654" height="186" srcset="https://thecodebuzz.com/wp-content/uploads/2022/06/MongoDB-using-ObjectId-1024x292.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2022/06/MongoDB-using-ObjectId-300x86.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/06/MongoDB-using-ObjectId-768x219.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2022/06/MongoDB-using-ObjectId-1536x438.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2022/06/MongoDB-using-ObjectId-785x224.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2022/06/MongoDB-using-ObjectId.jpg 1633w" sizes="auto, (max-width: 654px) 100vw, 654px" /></figure>



<p>Today in this article, we will see more details about MongoDB ObjectId Data type.</p>



<p><strong><em>ObjectId </em></strong>is the <strong><em>12-byte</em></strong> element consisting of a timestamp value, a random value, and an incremental counter.</p>



<p>Today in this article, we will cover below aspects,</p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-mongodb-_id-field-as-objectid">MongoDB _id field as ObjectId</a></li><li><a href="#aioseo-mongodb-objectid-fields-details">MongoDB ObjectId fields details</a></li></ul></div>



<h2 class="wp-block-heading" id="aioseo-mongodb-_id-field-as-objectid">MongoDB _id field as ObjectId  </h2>



<p></p>



<p>MongoDB uses the <strong><em>_id </em></strong><em>f</em>ield as <strong><em>ObjectId </em></strong>this Id has few behavioral characteristics and it is explained below, A <strong><em>4-byte</em></strong> timestamp is a UNIX timestamp.  </p>



<ul class="wp-block-list"><li> MongoDB database automatically generates&nbsp;_id&nbsp;the field for insertion.</li></ul>



<ul class="wp-block-list"><li>This field is unique for every document inserted into the collection. </li></ul>



<ul class="wp-block-list"><li><strong><em>_id </em></strong>field is&nbsp;automatically indexed&nbsp;and the&nbsp;index is unique.</li></ul>



<p></p>



<p>It represents the <strong><em>ObjectId&#8217;s </em></strong>creation, measured in seconds since the Unix epoch. for more details, please visit <a href="https://www.mongodb.com/docs/manual/reference/bson-types/" target="_blank" rel="noreferrer noopener"><strong><em>here</em></strong></a>.</p>



<figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2022/06/MongoDB-ObjectId-datatype.jpg" alt="" class="wp-image-22616" width="429" height="213" srcset="https://thecodebuzz.com/wp-content/uploads/2022/06/MongoDB-ObjectId-datatype.jpg 702w, https://thecodebuzz.com/wp-content/uploads/2022/06/MongoDB-ObjectId-datatype-300x149.jpg 300w" sizes="auto, (max-width: 429px) 100vw, 429px" /></figure>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-mongodb-objectid-fields-details">MongoDB ObjectId fields details</h2>



<p></p>



<p><strong><em>ObjectId </em></strong>is the <strong><em>12-byte</em></strong>&nbsp;element consisting of a timestamp value, a random value, and an incremental counter.</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="538" src="https://www.thecodebuzz.com/wp-content/uploads/2022/06/MongoDB-ObjectId-1024x538.jpg" alt="MongoDB ObjectId " class="wp-image-22604" srcset="https://thecodebuzz.com/wp-content/uploads/2022/06/MongoDB-ObjectId-1024x538.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2022/06/MongoDB-ObjectId-300x158.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/06/MongoDB-ObjectId-768x403.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2022/06/MongoDB-ObjectId-1536x807.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2022/06/MongoDB-ObjectId-2048x1075.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2022/06/MongoDB-ObjectId-785x412.jpg 785w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<ul class="wp-block-list"><li>A <strong><em>4-byte</em></strong> timestamp is a UNIX timestamp. It represents the ObjectId&#8217;s creation timestamp, measured in seconds since the Unix epoch. </li></ul>



<p></p>



<p>If interested to know how to get timestamp using these 4 bytes, please visit the below article,</p>



<p></p>



<ul class="wp-block-list"><li><a href="https://www.thecodebuzz.com/query-mongodb-objectid-by-date-mongoshell-compass/" target="_blank" rel="noreferrer noopener" title="Query MongoDB using ObjectId by Date -MongoShell"><em><strong>Query MongoDB using ObjectId by Date -MongoShell</strong></em></a></li></ul>



<p></p>



<p></p>



<ul class="wp-block-list"><li>A <strong><em>5-byte</em></strong> random value is generated once per process. This random value is unique to the machine and process.</li></ul>



<p></p>



<ul class="wp-block-list"><li>A<strong><em> 3-byte</em></strong> incrementing counter, initialized to a random value.</li></ul>



<p></p>



<p><strong>References: </strong></p>



<p></p>



<ul class="wp-block-list"><li><a href="https://www.thecodebuzz.com/mongo-db-naming-conventions-standards-guidelines/" target="_blank" rel="noreferrer noopener" title="MongoDB Naming Standards and Guidelines"><em><strong>MongoDB Naming Standards and Guidelines</strong></em></a></li></ul>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p>



<p></p>



<p></p>



<p></p>



<p></p><p>The post <a href="https://thecodebuzz.com/what-is-mongodb-objectid-data-type-guidelines/">MongoDB ObjectId Data Type – Guidelines</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/what-is-mongodb-objectid-data-type-guidelines/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
