<?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: Aman Singh Parihar</title>
    <description>The latest articles on DEV Community by Aman Singh Parihar (@amansinghparihar).</description>
    <link>https://dev.to/amansinghparihar</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%2F62699%2F82407195-393d-4381-b0c6-11c36a48b737.png</url>
      <title>DEV Community: Aman Singh Parihar</title>
      <link>https://dev.to/amansinghparihar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amansinghparihar"/>
    <language>en</language>
    <item>
      <title>Garbage Collection C#</title>
      <dc:creator>Aman Singh Parihar</dc:creator>
      <pubDate>Tue, 10 Jan 2023 11:24:42 +0000</pubDate>
      <link>https://dev.to/amansinghparihar/garbage-collection-c-ja9</link>
      <guid>https://dev.to/amansinghparihar/garbage-collection-c-ja9</guid>
      <description>&lt;h1&gt;
  
  
  Garbage Collector
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Glossary
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Heap
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Portion of memory where dynamically allocated objects resides.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Managed Heap
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Whenever we start any process or run our .NET code, it 
reserves some space in the memory which is contiguous and 
it's called managed heap.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;There were the days when the developers would be required to release the memory which they have allocated for the objects.&lt;/p&gt;

&lt;p&gt;It's the developers responsibility to manage the memory, and this brings some issues during development.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If we create a new object but forget to delete it, it will persist in the memory for a long time even if we didn't want to use it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If our variable is referencing an object in the memory, and later that variable started referencing another object in the memory, then the previous object will persist in memory even if no variable in referencing this object. It's called memory leak. Object is not referenced by any variable and also not required but still in memory.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To overcome these scenarios, .NET came up with the garbage collector, which will take care of deleting the objects from the memory.&lt;/p&gt;

&lt;p&gt;But, the garbage collector is limited to allocate or release the memory of managed code only, it does not allocate or release the unmanaged memory.&lt;/p&gt;

&lt;p&gt;Managed Code is the code which runs under the .NET environment. .NET takes care of its memory management.&lt;br&gt;
But the code which is outside of the .NET environment called the unmanaged code. .NET garbage collector doesn't know how to release the memory of unmanaged code.&lt;/p&gt;

&lt;p&gt;Managed code written in the .NET might access unmanaged code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;for ex:- Connecting with database using SQL, Oracle classes, Network calls made from managed code, accessing files.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even though we are writing the managed code, but this code is using the unmanaged code. So as a developer it's our responsibility to release the memory once the work of these objects are completed.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do we release the memory then?
&lt;/h3&gt;

&lt;p&gt;.NET provides the IDisposable interface for the types (example:- class) which are using the unmanaged code, for ex, network calls, database connection etc.&lt;/p&gt;

&lt;p&gt;Those types needs to implement IDisposable interface and provides the implementation of the Dispose() method and write their own logic to release the memory.&lt;/p&gt;

&lt;p&gt;And our task as a developer is that once we are done with these objects, we should call the dispose method on these objects.&lt;/p&gt;

&lt;p&gt;.NET also provides a Finalize method, in case developer forgets to call the dispose method on this object.&lt;/p&gt;

&lt;p&gt;GC releases the memory even if we forget to call the dispose method, if that class also implements the finalize, but the crux here is that finalize in indeterministic and dispose is deterministic.&lt;/p&gt;

&lt;p&gt;It means we have control over the dispose method, when to call. But that finalize will be in hand of GC, when it calls.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>garbagecollection</category>
    </item>
    <item>
      <title>Authentication and Authorization in dotnet core</title>
      <dc:creator>Aman Singh Parihar</dc:creator>
      <pubDate>Tue, 11 May 2021 16:20:24 +0000</pubDate>
      <link>https://dev.to/amansinghparihar/authentication-and-authorization-in-dotnet-core-o2i</link>
      <guid>https://dev.to/amansinghparihar/authentication-and-authorization-in-dotnet-core-o2i</guid>
      <description>&lt;p&gt;I have created a asp.net core empty web application using asp.net core 5.0 version.&lt;/p&gt;

&lt;p&gt;Here we will learn very simple way of authentication and authorization.&lt;/p&gt;

&lt;p&gt;Authentication means "who are you?" and authorization means "are you allowed?"&lt;/p&gt;

&lt;p&gt;So before we ask anyone "are you allowed?", we have to ask "who are you?".&lt;/p&gt;

&lt;p&gt;For ex:- If 10 persons standing outside of the main gate of college, and 8 of them showed their college id card (authentication) and entered the college as the college is configured in such a way that it will allow anyone who has the college id card, i.e 8 persons are authorized to enter the college.&lt;/p&gt;

&lt;p&gt;2 of them showed their driving license, but since the college is not configured in such a way that it will allow a person with driving license, they will not be authorized to enter the college.&lt;/p&gt;

&lt;p&gt;Similarly we have to configure our system to allow or deny the user on the basis of some identity.&lt;/p&gt;

&lt;p&gt;if you run your application now it will display "Hello World!" on the web page, as it empty web application.&lt;/p&gt;

&lt;p&gt;Lets create a folder "Controller" and add a controller inside this folder named "HomeController.cs" &lt;/p&gt;

&lt;p&gt;Add three methods Index(), Restricted() and Authenticate() in the controller and add their respective views and ddd [Authorize] attribute  on the Restricted() method.&lt;/p&gt;

&lt;p&gt;Currently the code in "HomeController.cs" will be like this.&lt;/p&gt;

&lt;h2&gt;
  
  
  HomeController.cs
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Simple.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        [Authorize]
        public IActionResult Restricted() 
        {
            return View();
        }
        public IActionResult Authenticate() 
        {
            return View();
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets modify the code of the startup.cs class, modified code in the startup.cs will be.&lt;/p&gt;

&lt;h2&gt;
  
  
  Startup.cs
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Simple
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =&amp;gt;
            {
                endpoints.MapDefaultControllerRoute();
            });
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can clearly see that we have removed "Hello World" code and added code required to show view with controller.&lt;/p&gt;

&lt;p&gt;Now if we run the code and try to access Home/Restricted it will give an error.&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%2Fpsa9bua3ltwdrl2fvxpp.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%2Fpsa9bua3ltwdrl2fvxpp.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Its simple right, we haven't configured our pipeline for checking authorization so need to add this method app.UseAuthorization() in configure method. &lt;/p&gt;

&lt;p&gt;Lets add this code and run the application again.&lt;/p&gt;

&lt;p&gt;But again we get an error.&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%2Fx925yltcptvd40bh54m4.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%2Fx925yltcptvd40bh54m4.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lets understand this error, we haven't defined any policy to check, i.e system is asking "Are you allowed?" but it doesn't know "Who are you?"&lt;/p&gt;

&lt;p&gt;so we need to configure our system for validating the user first.&lt;/p&gt;

&lt;p&gt;Lets add authentication scheme in "ConfigureServices" method of "Startup" class.&lt;/p&gt;

&lt;h2&gt;
  
  
  Startup.cs
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            services.AddAuthentication("College-Id")
                .AddCookie("College-Id", configureOptions =&amp;gt;
                {
                    configureOptions.Cookie.Name = "College-Id.Cookie";
                });
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have configured our system to look for the college-Id.Cookie, if it found then the user is authenticated.&lt;/p&gt;

&lt;p&gt;Lets run the code now.&lt;/p&gt;

&lt;p&gt;This time we did not get any error, but check the url now. We are trying to access the Restricted() method of the HomeController and system throws us default page Account/Login to first authenticate our self then we will be able to access the Restricted() method of the HomeController.&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%2Fmwat3ykvrcryltu7nf5r.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%2Fmwat3ykvrcryltu7nf5r.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lets write some code in the Authenticate Method of the HomeController.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public IActionResult Authenticate() 
        {
            var claims = new List&amp;lt;Claim&amp;gt;
            {
                new Claim(ClaimTypes.Name,"authenticatedUser"),
                new Claim("College-Id","YES")
            };

            var identity = new ClaimsIdentity(claims,"College Identity");

            var userPrincipal = new ClaimsPrincipal(new [] { identity });

            HttpContext.SignInAsync(userPrincipal);

            return View();
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we will define claims, identity and user principal.&lt;/p&gt;

&lt;p&gt;Claims are basically, what the user is claiming, for instance the college id card.&lt;/p&gt;

&lt;p&gt;Identity means, college authority is trusting the person having the college id card.&lt;/p&gt;

&lt;p&gt;On the basis of the identity we have created user principal and signed in the user.&lt;/p&gt;

&lt;p&gt;Now run this code you will see index page,but try to access "Home/Restricted" you will not be able to access this, as you are not authenticated.&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%2Fdll37nynrfbe5eeebvq0.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%2Fdll37nynrfbe5eeebvq0.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now lets go to route Home/Authenticate in the url, cookie will be generated.&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%2Ft45q27edhjgzfe4eduk0.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%2Ft45q27edhjgzfe4eduk0.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now try to access Home/Restricted in the url, you will be able to access the page, as you are not authenticated and authorized to access this page.&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%2Fxyzhdkbtrxzsjo1euofv.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%2Fxyzhdkbtrxzsjo1euofv.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnetcore</category>
      <category>authentication</category>
      <category>authorization</category>
    </item>
    <item>
      <title>Sequence of object creation in .NET</title>
      <dc:creator>Aman Singh Parihar</dc:creator>
      <pubDate>Sat, 01 May 2021 12:26:56 +0000</pubDate>
      <link>https://dev.to/amansinghparihar/sequence-of-object-creation-in-net-2foh</link>
      <guid>https://dev.to/amansinghparihar/sequence-of-object-creation-in-net-2foh</guid>
      <description>&lt;p&gt;We are going to learn some basics of how the object is created in c# .net, most importantly the sequence of how they are created when the classes are following inheritance.&lt;/p&gt;

&lt;p&gt;This is very simple topic but confuses many developers, even I was confused when I first learnt this.&lt;/p&gt;

&lt;p&gt;Lets try to understand this.&lt;/p&gt;

&lt;p&gt;In this example we will take two classes "Parent" and "Child" to make things simple.&lt;/p&gt;

&lt;p&gt;Below is the code for "Parent" class.&lt;/p&gt;


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


&lt;p&gt;Here we can see that we have two constructor, default and parameterized.&lt;/p&gt;

&lt;p&gt;Now lets see the code of child class. We will discuss two cases here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lets see 1st case:
&lt;/h2&gt;

&lt;p&gt;Here is the code of "Child" class.&lt;/p&gt;


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


&lt;p&gt;Now when we create the object of Child class, like this.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Child c = new Child();&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Default constructor of the parent class will be called first, and then the child class constructor will be called.&lt;/p&gt;

&lt;p&gt;Output of the above execution will be something like this.&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%2F1yal7i0aukim1itt63u2.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%2F1yal7i0aukim1itt63u2.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Now lets see 2nd case:
&lt;/h2&gt;

&lt;p&gt;Here we want to explicitly call the "Parent" class parameterized constructor.&lt;/p&gt;

&lt;p&gt;For this we have to make some tweak in the "Child" class.&lt;/p&gt;

&lt;p&gt;Here is the code of the Child class.&lt;/p&gt;


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


&lt;p&gt;We can see that we have used a "base" keyword here, this will execute the parameterized constructor of the "Parent" class.&lt;/p&gt;

&lt;p&gt;Now when we create the object of Child class, like this, by passing any number as a parameter.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Child c = new Child(1);&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Parameterized constructor of "Parent" class will be executed first, then the constructor of the "Child" class.&lt;/p&gt;

&lt;p&gt;Output of the code will be something like this.&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%2Fy08ui6xxfcu05eim4s41.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%2Fy08ui6xxfcu05eim4s41.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>oop</category>
    </item>
    <item>
      <title>Envrionment in .NET Core</title>
      <dc:creator>Aman Singh Parihar</dc:creator>
      <pubDate>Mon, 26 Apr 2021 16:20:08 +0000</pubDate>
      <link>https://dev.to/amansinghparihar/envrionment-in-net-core-1lpn</link>
      <guid>https://dev.to/amansinghparihar/envrionment-in-net-core-1lpn</guid>
      <description>&lt;p&gt;We can setup different environments while developing an application in .net core.&lt;/p&gt;

&lt;p&gt;When we first create an application in .net core using the web api template, we have two different profiles available in the launchSettings.json.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;IIS Express&lt;/li&gt;
&lt;li&gt;{Name of the project}&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Note: launchSettings.json file is only available at the time of development.&lt;/strong&gt;&lt;/p&gt;


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


&lt;p&gt;Lets try to understand this file.&lt;/p&gt;

&lt;p&gt;In the profile section we have two profiles available.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;IIS Express&lt;br&gt;
As we can see commandName is set to IISExpress, and in the environment varialble section we have ASPNETCORE_ENVIRONMENT variable set to Development.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;{Name of the Project}&lt;br&gt;
Here we can see commandName is set to Project, and in the environment varialble section we have ASPNETCORE_ENVIRONMENT variable set to Development.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We can select any of these profile to run our application.&lt;/p&gt;

&lt;p&gt;If commandName is set to IISExpress, then the application will be launched using IISExpress and if the commandName will be set to Project, then the application will be launched using kestrel server along with a reverse proxy server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FuFyvSV5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t8suak9iblbw2ni0w37n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FuFyvSV5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t8suak9iblbw2ni0w37n.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yLTwjMeh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uikiohmltu5azfbarlry.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yLTwjMeh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uikiohmltu5azfbarlry.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And as in both the cases ASPNETCORE_ENVIRONMENT set to development, so the environment will be development. We can check for different environment in the startup.cs file using various extension methods.&lt;/p&gt;


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


&lt;p&gt;And if no value is set for ASPNETCORE_ENVIRONMENT  variable in launchSettings.json, then it will look in the System Environment Variable and if it is also not set there, then the value will be production by default.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>dotnetcore</category>
      <category>csharp</category>
      <category>environment</category>
    </item>
    <item>
      <title>Automapper in .NET Core</title>
      <dc:creator>Aman Singh Parihar</dc:creator>
      <pubDate>Sat, 24 Apr 2021 06:51:11 +0000</pubDate>
      <link>https://dev.to/amansinghparihar/automapper-in-net-core-1952</link>
      <guid>https://dev.to/amansinghparihar/automapper-in-net-core-1952</guid>
      <description>&lt;p&gt;GitHub link for the project:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&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%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/aman-singh-parihar" rel="noopener noreferrer"&gt;
        aman-singh-parihar
      &lt;/a&gt; / &lt;a href="https://github.com/aman-singh-parihar/Automapper" rel="noopener noreferrer"&gt;
        Automapper
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Automapper in .NET Core
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;We will create a project in .NET Core using dotnet cli and will use visual studio code as the IDE.&lt;/p&gt;

&lt;p&gt;Go to any directory where you want to create this project. &lt;/p&gt;

&lt;p&gt;We are using powershell to setup this project.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;PS E:/VSCODE/NETProjects&amp;gt;mkdir Automapper&lt;/code&gt;&lt;br&gt;
&lt;code&gt;PS E:/VSCODE/NETProjects&amp;gt;cd Automapper&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lets create a solution in Automapper folder.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;PS E:/VSCODE/NETProjects&amp;gt;Automapper&amp;gt;dotnet new sln&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lets create a project in the Automapper folder.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;PS E:/VSCODE/NETProjects&amp;gt;Automapper&amp;gt;dotnet new webapi -o Automapper.API&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lets add the project in the solution file because as of now they are disconnected to each other.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;dotnet sln add Automapper.API/Automapper.API.csproj&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now lets open Automapper folder in vscode.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;PS E:/VSCODE/NETProjects&amp;gt;Automapper&amp;gt;code .&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Switch to project folder and install automapper.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;PS E:/VSCODE/NETProjects&amp;gt;Automapper&amp;gt;cd Automapper.API&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;PS E:/VSCODE/NETProjects&amp;gt;Automapper&amp;gt;Automapper.API&amp;gt;dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjection&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We setup our project, now lets write some code. First of all delete the weather api controller and weather api class as we don't need this.&lt;/p&gt;

&lt;p&gt;Lets create two folders Models and DTO.&lt;br&gt;
Models will have classes that will exactly match with the database tables and DTO will have classes which need to pass to the client.&lt;/p&gt;

&lt;p&gt;In our case we are using Employee class in Models folder and EmployeeDTO in DTO folders.&lt;/p&gt;
&lt;h2&gt;
  
  
  Employee.cs
&lt;/h2&gt;


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


&lt;h2&gt;
  
  
  EmployeeDTO.cs
&lt;/h2&gt;


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


&lt;p&gt;You can see that we are not passing employee salary to the client.&lt;/p&gt;

&lt;p&gt;Now we will create profile which will map from Employee to EmployeeDTO.&lt;/p&gt;

&lt;p&gt;Lets create a folder AutoMapperProfile in the project and add a class AutomapperProfile.cs.&lt;/p&gt;

&lt;h2&gt;
  
  
  AutomapperProfile.cs
&lt;/h2&gt;


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


&lt;p&gt;Lets setup the ConfigureServices method of startup class for automapper.&lt;/p&gt;

&lt;h2&gt;
  
  
  Startup.cs
&lt;/h2&gt;


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


&lt;p&gt;Now lets create a EmployeeController in Controllers folder.&lt;/p&gt;

&lt;h2&gt;
  
  
  EmployeeController.cs
&lt;/h2&gt;


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


&lt;p&gt;We are almost done with this, lets run this project and see the output.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;br&gt;
&lt;code&gt;PS E:/VSCODE/NETProjects&amp;gt;Automapper&amp;gt;Automapper.API&amp;gt;dotnet run&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Type this url in the browser to view the output: &lt;a href="https://localhost:5001/api/Employee" rel="noopener noreferrer"&gt;https://localhost:5001/api/Employee&lt;/a&gt;&lt;/p&gt;


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


</description>
      <category>automapper</category>
      <category>dotnetcore</category>
      <category>csharp</category>
      <category>dotnet</category>
    </item>
  </channel>
</rss>
