<?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: Fernando Sonego</title>
    <description>The latest articles on DEV Community by Fernando Sonego (@fernandosonego).</description>
    <link>https://dev.to/fernandosonego</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%2F429506%2F70e82328-0146-42e7-a58d-a858971a16ce.jpg</url>
      <title>DEV Community: Fernando Sonego</title>
      <link>https://dev.to/fernandosonego</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fernandosonego"/>
    <language>en</language>
    <item>
      <title>.Net Core | User Secrets</title>
      <dc:creator>Fernando Sonego</dc:creator>
      <pubDate>Tue, 29 Sep 2020 00:17:16 +0000</pubDate>
      <link>https://dev.to/fernandosonego/net-core-user-secrets-3ic6</link>
      <guid>https://dev.to/fernandosonego/net-core-user-secrets-3ic6</guid>
      <description>&lt;p&gt;In development time .Net Core has a functionality that allows us to save configurations in our local user profile in a safe and simple way. This functionality helps us not to upload by mistake or modify the files with private information on servers or even in our source code control systems such as GitHub, Bitbucket or Team Foundation Service.&lt;/p&gt;

&lt;p&gt;In the past, in our .Net applications, we had the ability to encrypt our secret information contained in the web.config files. Sometimes it made it a bit awkward to have to deploy the applications or store this sensitive information in our repositories.&lt;/p&gt;

&lt;p&gt;We can keep sensitive information from User Secrets safely. Among the things that we can save we have: connection strings, API keys, customer data, data from any social network, or from any application. Everything that we consider as sensitive data can be stored without risking exposing it publicly.&lt;/p&gt;

&lt;p&gt;In my case, it is very useful to me when I upload examples to GitHub. I use it to upload an example without having to upload access data to an API that I use in Azure or some social network.&lt;/p&gt;

&lt;h2&gt;
  
  
  Microsoft.Extensions.SecretManager.Tools
&lt;/h2&gt;

&lt;p&gt;We must install Microsoft.Extensions.SecretManager.Tools. Secret manager stores the information outside the project and allows us not only to use it in the current project, but it also gives us the possibility to use it in several projects.&lt;/p&gt;

&lt;p&gt;We will install it from the console of the Nuget packages in our application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1i3mNLGt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200928.UserSecrets/UserSecret01.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1i3mNLGt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200928.UserSecrets/UserSecret01.png" alt="UserSecrets"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once installed, we right-click on our project and select the option "Manage User Secrets" from the menu:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vOqGJtm5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200928.UserSecrets/UserSecret02.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vOqGJtm5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200928.UserSecrets/UserSecret02.png" alt="UserSecrets"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It will open a JSON file called secrets.json. This file will save the data we need. This is created automatically and is located inside a protected folder on the system. We can find it through a folder structure similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;%APPDATA%\Microsoft\UserSecrets\user-secrets-id\secrets.json
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we want to see the User-Secrets-Id we can locate it by right-clicking on the project and selecting the option “Edit  .csproj”. This will show us the project's configuration file with the id inside:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_moSGT3y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200928.UserSecrets/UserSecret03.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_moSGT3y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200928.UserSecrets/UserSecret03.png" alt="UserSecrets"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our JSON file we will add value in order to have information to be consumed later:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"TopSecret"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"TopSecretValuePasssUserRoot"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The next step is to configure the service in our Web application. We will open the Startup.cs file and modify the constructor so that we have it available with the following lines:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RKRtVdl0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200928.UserSecrets/UserSecret04.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RKRtVdl0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200928.UserSecrets/UserSecret04.png" alt="UserSecrets"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So far we already have our UserSecrets service active, we add value in our secrets.json file and configure in the constructor to be able to access. Now we are going to consume the value from our file. We will go to the HomeController.cs and inject the IConfiguration constructor to have the value query available:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WeMviESo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200928.UserSecrets/UserSecret05.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WeMviESo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200928.UserSecrets/UserSecret05.png" alt="UserSecrets"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, we will run it and check if it is recovering the error correctly:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1RlVlXTp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200928.UserSecrets/UserSecret06.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1RlVlXTp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200928.UserSecrets/UserSecret06.png" alt="UserSecrets"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this post, we have seen what it is, how to configure it, and how to use UserSecrets. The use of this functionality does not provide a great possibility of not being obliged or by mistake to upload confidential information from our social media accounts, database connection, or any sensitive information that we consider.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
    </item>
    <item>
      <title>Asynchronous Programming in .Net Core (Part 2)</title>
      <dc:creator>Fernando Sonego</dc:creator>
      <pubDate>Mon, 21 Sep 2020 22:09:01 +0000</pubDate>
      <link>https://dev.to/fernandosonego/asynchronous-programming-in-net-core-part-2-3gc5</link>
      <guid>https://dev.to/fernandosonego/asynchronous-programming-in-net-core-part-2-3gc5</guid>
      <description>&lt;p&gt;In the previous post, we saw a small introduction to the features that .Net has to work asynchronously. In this post, we will see how to create our asynchronous methods, how to handle exceptions, and good practices when using this paradigm.&lt;/p&gt;

&lt;h2&gt;
  
  
  Our asynchronous method
&lt;/h2&gt;

&lt;p&gt;We should always think that our code should not be too long. Let's remember that our code must be as clean as possible, therefore, we are going to refactor the code from the previous post, which was the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;button_Click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;RoutedEventArgs&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;HttpClient&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"https://localhost:44356/api/customers/search/"&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
             &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;EnsureSuccessStatusCode&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
             &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadAsStringAsync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
             &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JsonConvert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DeserializeObject&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Customer&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
             &lt;span class="n"&gt;CustomersGrid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ItemsSource&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
         &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;txtOut&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will make some changes, we will create a method that we will call GetPeople (). We will make sure that our application does not modify its behavior despite being in a separate method. We will modify our code. It will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;button_Click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;RoutedEventArgs&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;GetPeople&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;GetPeople&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;HttpClient&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"https://localhost:44356/api/customers/search/"&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;try&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
             &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;EnsureSuccessStatusCode&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
             &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadAsStringAsync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
             &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JsonConvert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DeserializeObject&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Customer&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;CustomersGrid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ItemsSource&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;txtOut&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first thing we can notice is that our GetPeople () method is declared as async and Task. Note: we should not do async methods that return a void type, we should only use it when they are Event Handlers.&lt;/p&gt;

&lt;p&gt;Declaring it as Task means that the method will automatically return a reference to the operation that is in progress and at some point, it will be invoked waiting for the resolution. Visual Studio will not show any errors, this is because a method declared as an async task must explicitly return something and the compiler will take care of it.&lt;/p&gt;

&lt;p&gt;In the click event, we invoke GetPeople () and declare it with the await keyword so that the code waits. Let's see in the image that nothing has changed:&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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200921.Aync2%2F01-3.gif" 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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200921.Aync2%2F01-3.gif" alt=".Net Async"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is very easy to refactor your code by creating an asynchronous method. By doing so we made our code a bit more readable. Remember, again, that this approach can be applied in any environment of .Net, ASP.Net, console, Xamarin, etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exceptions
&lt;/h2&gt;

&lt;p&gt;Our methods can fail. Suppose our method is calling the API but it is not available at the moment. Our application will fail. For this reason, the await keyword is so important. This guarantees that the exception is handled, and if we do not use it, the application will never know that an exception has occurred.&lt;/p&gt;

&lt;p&gt;Visual Studio warns us when we have a call to a method that returns a Task and is not being invoked with the word await.&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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200921.Aync2%2F02.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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200921.Aync2%2F02.png" alt=".Net Async"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another example would be, what happens if we use a try..catch block?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;GetPeople&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Exactly the same would happen, the exception will not be handled. But if we add the await keyword we can handle the exception.&lt;/p&gt;

&lt;p&gt;Another important point, async methods should not be void. The reason is that void methods cannot be handled by their exceptions because they return nothing. By using Task we make sure that something is returned.&lt;/p&gt;

&lt;p&gt;For this reason, the await and async Task combination are very important. Thanks to this combination we can ensure that we can handle exceptions. The method will return a type and when invoked it will wait for something to happen.&lt;/p&gt;

&lt;h2&gt;
  
  
  Good Practices and Conclusions
&lt;/h2&gt;

&lt;p&gt;Where we can use these practices in .Net:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Windows Presentation Foundation&lt;/li&gt;
&lt;li&gt;Windows Forms&lt;/li&gt;
&lt;li&gt;Xamarin&lt;/li&gt;
&lt;li&gt;Console apps&lt;/li&gt;
&lt;li&gt;Asp.Net&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can use it in any type of .Net application we need. The only difference is that ASP.Net does not have a user interface but it will help us to lighten the load on the webserver.&lt;/p&gt;

&lt;p&gt;Let's summarize what is wrong and what is correct:&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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200921.Aync2%2F03-3.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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200921.Aync2%2F03-3.png" alt=".Net Async"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope that the information is useful to you and covers all the needs that arise. In future posts, I will touch on more topics on synchronous and parallel programming.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
    </item>
    <item>
      <title>Asynchronous Programming in .Net Core (Part 1)</title>
      <dc:creator>Fernando Sonego</dc:creator>
      <pubDate>Mon, 14 Sep 2020 12:40:09 +0000</pubDate>
      <link>https://dev.to/fernandosonego/asynchronous-programming-in-net-core-part-1-5553</link>
      <guid>https://dev.to/fernandosonego/asynchronous-programming-in-net-core-part-1-5553</guid>
      <description>&lt;p&gt;We always want to create fast applications with great performance. For this reason, it is very important to know the asynchronous principles that .Net offers us. We will see the main concepts, common problems, complex scenarios, and what keywords that C # gives us to be able to work asynchronously.&lt;/p&gt;

&lt;h2&gt;
  
  
  Asynchronous programming in .NET
&lt;/h2&gt;

&lt;p&gt;To begin with, it does not matter if we are not working in Asp.Net, in a console application, Windows Presentation Foundation, Windows Forms applications, or Xamarin, the asynchronous principles can be applied in any .Net environment.&lt;/p&gt;

&lt;p&gt;Before looking at how it works in detail, let's look at an application that does not take advantage of these features. We will use a Windows Presentation Foundation application to see in detail what happens when having synchronous connections.&lt;/p&gt;

&lt;p&gt;Our apps search for people by the color of their eyes. We have a TextBox and a Button to search for. Also, a grid where we will see the results, a text box to write, and a progress bar. Our application consults an API to retrieve the data. Let's see what happens.&lt;/p&gt;

&lt;p&gt;The code that is executed when pressing the search button is the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mvC-qWwv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200913.Async1/01-1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mvC-qWwv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200913.Async1/01-1.gif" alt=".Net Async"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;WebClient&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;DownloadString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"https://localhost:44356/api/customers/search/"&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JsonConvert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DeserializeObject&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Customer&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;CustomersGrid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ItemsSource&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We are using a class that is now deprecated, which is WebClient. This class has the problem that it blocks the main thread, which is where the user interface is located, causing it to crash as well.&lt;/p&gt;

&lt;p&gt;So what can we do? Well, first we will use a class that is designed to make use of asynchronous technology that is HttpClient. This class has the GetAsync method, and as its name indicates, it is an asynchronous operation, implying that it will not block the main thread where the interface is located.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;HttpClient&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"https://localhost:44356/api/customers/search/"&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadAsStringAsync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JsonConvert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DeserializeObject&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Customer&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;CustomersGrid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ItemsSource&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can see that the await keyword is found in our new code. This is the best way to indicate that we want to return to this part of the code once the data is loaded from the API we are querying. Let's see the behavior now that the code is written so as not to generate locks:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dSPnTk51--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200913.Async1/02-1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dSPnTk51--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200913.Async1/02-1.gif" alt=".Net Async"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see our UI now is not blocked when consulting a service. We can also see the progress bar running without interruptions while waiting for the resolution of the data.&lt;/p&gt;

&lt;p&gt;Actually, as you have surely noticed, with a few small changes in our application we can take advantage of the principles of asynchronism, providing a better user experience in this case.&lt;/p&gt;

&lt;h2&gt;
  
  
  Async and Await in .NET
&lt;/h2&gt;

&lt;p&gt;Let's see the execution of the code where the button's click event is declared:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;button_Click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;RoutedEventArgs&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;try&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;HttpClient&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;

            &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"https://localhost:44356/api/customers/search/"&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadAsStringAsync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JsonConvert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DeserializeObject&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Customer&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="n"&gt;CustomersGrid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ItemsSource&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next to private appears the word async, this indicates that the event will be asynchronous, but it will not be enough for us to work correctly. For this, we must use the word await, which, as we commented previously, is in charge of notifying that it must wait until it is finished executing.&lt;/p&gt;

&lt;p&gt;Suppose we only have the word async and never use await. Visual Studio will tell us that it will be waiting for the word await inside the method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S2OuijjF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200913.Async1/03-1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S2OuijjF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200913.Async1/03-1.gif" alt=".Net Async"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another point that we must take into account is the method. As we can see the method is now called GetAsync (), in fact, it returns a task of the HttpResponseMessage type, we will talk later in the post, for now, we will only say that it will make it run in a different thread.&lt;/p&gt;

&lt;p&gt;The asynchronous principles that we talk about in our applications are not only intended for Windows applications or mobile applications. These asynchronous principles are suitable for any type of I / O operations. For example, disk access, database access, memory or services as in the example.&lt;/p&gt;

&lt;p&gt;We can also apply the same principle to server-side code in ASP.NET. Let's see the code of the rest API that is being invoked, which returns the list of people.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"api/customers/search/{valor}"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;       
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IList&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Customer&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;valor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Customer&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;StreamReader&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;StreamReader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"customers.json"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadToEndAsync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JsonConvert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DeserializeObject&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Customer&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EyeColor&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;valor&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;ToList&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We have declared the controller method as asynchronous. Also, the type will be Task. This means that ASP.Net will know that there is an asynchronous operation in progress and then when all the results are available, they will be returned by the method.&lt;/p&gt;

&lt;p&gt;The benefit that it offers us within ASP.Net is to reduce the excessive load on the web servers without interrupting the normal execution, allowing it to continue serving requests while it is waiting for the resolution of data or external accesses.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the flow
&lt;/h2&gt;

&lt;p&gt;So far we have seen how to declare our methods as asynchronous, we saw the keywords as await and we understood how the operations are executed in a separate thread and not in the main thread of the applications.&lt;/p&gt;

&lt;p&gt;Await allows us to retrieve the result of an asynchronous operation when it is available. It also assures us that there are no exceptions or problems with the currently running task. This means that it is not only the best way to wait, but also to validate the current operation.&lt;/p&gt;

&lt;p&gt;This is where the term Continuation comes in. Continuation is the block of code that is executed after the await word.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xAa5pAkH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200913.Async1/04.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xAa5pAkH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200913.Async1/04.png" alt=".Net Async"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see in the image, our method called GetAsync (…) is marked with the word await, this means that what is marked in red is our Continuation block. Again, where the RadAsStringAsync () method is found, it is marked with await, this means that the next 2 lines would be our Continuation.&lt;/p&gt;

&lt;p&gt;Let's make a change to test if there is a problem, for example, that it does not find any records. Our code would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;HttpClient&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"https://localhost:44356/api/customers/search/"&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;EnsureSuccessStatusCode&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadAsStringAsync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JsonConvert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DeserializeObject&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Customer&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;CustomersGrid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ItemsSource&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;txtOut&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Our method makes the asynchronous call but will not return an error. For this, we change our Try..Catch and inside we invoke result.EnsureSuccessStatusCode (). This validates the response code, as it is not 200 it will throw an exception. Finally, it will be caught by the catch by typing the error in the text box.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TdaAdGGe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200913.Async1/01-2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TdaAdGGe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200913.Async1/01-2.gif" alt=".Net Async"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see that after the await word the code will be executed giving the feeling that we should not worry about working in another thread and that if it has a problem we can validate and retrieve the context of where we are executing our code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In our applications, it is very important that we understand and begin to implement these asynchronous patterns. Not only because of the fact that our applications will have fewer blocks and will have a better user experience, but also that these blocks can produce additional costs. If we are working in the cloud we must not forget that the more we waste resources, the more we will have to pay. This is a good way to save costs with our applications.&lt;/p&gt;

&lt;p&gt;In the next post, we will see how to create our own asynchronous methods, how to handle exceptions, and the best practices for it.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>Visual Studio Intellicode</title>
      <dc:creator>Fernando Sonego</dc:creator>
      <pubDate>Mon, 07 Sep 2020 16:17:57 +0000</pubDate>
      <link>https://dev.to/fernandosonego/visual-studio-intellicode-57bm</link>
      <guid>https://dev.to/fernandosonego/visual-studio-intellicode-57bm</guid>
      <description>&lt;p&gt;At the Build 2019 conference, Microsoft announced the release of Intellicode. This tool based on artificial intelligence will help us developers while we are developing code. Intellicode is responsible for analyzing millions of lines of code coming from public GitHub projects to provide the best options for our projects and increase their quality, also, it does not give the ability to be more productive.&lt;/p&gt;

&lt;p&gt;It supports the following languages in Visual Studio 2019:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;C#&lt;/li&gt;
&lt;li&gt;Java&lt;/li&gt;
&lt;li&gt;Python&lt;/li&gt;
&lt;li&gt;SQL Server&lt;/li&gt;
&lt;li&gt;TypeScript / Javascript&lt;/li&gt;
&lt;li&gt;XAML&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sure you wonder how it works? Intellicode analyzes millions of lines of code sourced from high-scoring public GitHub repositories. While we are writing our code, Visual Studio will show us the recommendations differentiated with a star, let's see the image:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D72YLuF0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200907.Intellisence/01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D72YLuF0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200907.Intellisence/01.gif" alt="Visual Studio IntelliCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the image we can see how when writing our code the suggestions related to the context of what we are doing are displayed. In our context, that of the example, in the recommendations we see the most used options at the beginning of the list marked with a star.&lt;/p&gt;

&lt;p&gt;This technology is based on GitHub line learning models. But we can create our own models for our projects. This allows us to customize our project adapting to our work team. The tool will analyze our projects or solutions, automatically generating recommendations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2s1gJT4B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200907.Intellisence/02.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2s1gJT4B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200907.Intellisence/02.png" alt="Visual Studio IntelliCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another interesting feature is Code Styles. This functionality allows us to have code conventions to automatically make style and format corrections. It will help us keep our code clean and organized. For this we must add the file editorconfig. This file will contain the necessary information on the basic styles that we can then configure as we wish.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OCi-HvFw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200907.Intellisence/03.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OCi-HvFw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200907.Intellisence/03.gif" alt="Visual Studio IntelliCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All the recommendations configured in these files can be seen in the error list window in Messages.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nmlZD0f5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200907.Intellisence/04.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nmlZD0f5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200907.Intellisence/04.png" alt="Visual Studio IntelliCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's do a simple test. We will change in our editorconfig file that our indentation instead of being 4 spaces is 15. We will only have to change this line.&lt;/p&gt;

&lt;p&gt;indent_size = 4 to indent_size = 15&lt;/p&gt;

&lt;p&gt;After saving our file, we will return to our code and press the Ctrl K + D keys to activate the automatic style adjustment. We will see how the indentation changes in our code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YV46aRbV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200907.Intellisence/05.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YV46aRbV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200907.Intellisence/05.gif" alt="Visual Studio IntelliCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Over time, more and more languages will be added, providing greater support to the tool. Intellicode will be a very helpful tool for programmers, being able to reduce a large number of common errors, maintain a clean, orderly and consistent code almost by the hand of an automatic refactoring.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>productivity</category>
    </item>
    <item>
      <title>StyleCop: Create code with style</title>
      <dc:creator>Fernando Sonego</dc:creator>
      <pubDate>Mon, 31 Aug 2020 13:11:39 +0000</pubDate>
      <link>https://dev.to/fernandosonego/stylecop-create-code-with-style-18jk</link>
      <guid>https://dev.to/fernandosonego/stylecop-create-code-with-style-18jk</guid>
      <description>&lt;p&gt;One of the discussions in the teams of programmers always styles that you must code. Most of the time, no two programmers write code in the same way as the other. This is because it is more a matter of taste than of a methodology itself.&lt;/p&gt;

&lt;p&gt;Let's imagine that the team agrees and we have a person who is dedicated to reviewing the coding style, quite tedious, right? This is where we can use an automation tool like StyleCop.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is StyleCop?
&lt;/h2&gt;

&lt;p&gt;What is StyleCop?&lt;br&gt;
StyleCop parses our C # code to apply a set of style and consistency rules to our code. We must bear in mind that there are 2 versions of StyleCop. Until recently, StyleCop was used, but now the recommendation is to use StyleCop Analyzer because the previous version will not have major changes or new features.&lt;/p&gt;

&lt;p&gt;Let's see an example of things that we can solve with StyleCop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The tool will be in charge of identifying all the places where the declaration of the first part of the example is found and it alerts us when compiling that there is code that does not comply with the style rules.&lt;/p&gt;

&lt;p&gt;Recall that these rules must be agreed with the team. By default, StyleCop, brings many rules, for example, quantity of white spaces, how to declare names of properties, variables, or methods, but perhaps, we do not like all of them or suit our team.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configure StyleCop
&lt;/h2&gt;

&lt;p&gt;After creating our project to add StyleCop we must do it from a Nuget package. For this we will right-click on our project, select "Nuget Package Manager" and the sale will open. In the search engine, we will write StyleCop.Analyzers and press install. You can see it in the following animation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UMajF0Q8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200831.StyleCop/01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UMajF0Q8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200831.StyleCop/01.gif" alt="StyleCop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Perfect! We already have it available in our project.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use StyleCop?
&lt;/h2&gt;

&lt;p&gt;The first thing we will do is modify our main method with the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;StyleCopDemo&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;HelloWorld1&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="nf"&gt;HelloWorld2&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="nf"&gt;HelloWorld3&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

            &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;HelloWorld1&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World! 1"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;HelloWorld2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World! 2"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;HelloWorld3&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World! 3"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We will see that in our error list window, errors that begin with the StyleCop Analyzer SA code will appear in the Warnings section.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fr93qVQ1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200831.StyleCop/02.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fr93qVQ1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200831.StyleCop/02.png" alt="StyleCop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A little trick, if we click on the code, it will take us to the website that will explain what this error is due to and the information corresponding to the rule that triggers it and how we must repair that code to eliminate the warning.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DQrXo29i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200831.StyleCop/03.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DQrXo29i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200831.StyleCop/03.png" alt="StyleCop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Configure Rules
&lt;/h2&gt;

&lt;p&gt;To configure the rules that will be applied to the code we have 2 paths. The first is to do it from the rules panel, it is accessed by right-clicking on the project and selecting properties. The second is to add a stylecop.json file to the project.&lt;/p&gt;

&lt;p&gt;To see the first panel of rules we must go to Solution Explorer, display dependencies, analyzers, finally, we select StyleCope.Analyzer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GUAzUuHg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200831.StyleCop/04.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GUAzUuHg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200831.StyleCop/04.gif" alt="StyleCop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can configure what level of severity it has between error, warning, suggestion, silence, or none.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h40r-cF---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200831.StyleCop/05.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h40r-cF---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200831.StyleCop/05.png" alt="StyleCop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each of the options has a different result, the most important:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Warning, appears in the list of errors in the warnings section.
*Error, it will be shown in the error section, and the invalid code is highlighted in red in the editor preventing the compilation of the project.
*Security, it will appear in the suggestions section.
*None, the rule will not be checked.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Another way to configure the rules is to use a stylecop.json file. We can create it automatically from a rule. When the lamp is marking us, press and select "Add StyleCop setting ...". This will create the file with the rule.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vP3uQF-4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200831.StyleCop/06.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vP3uQF-4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200831.StyleCop/06.gif" alt="StyleCop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The rule we created has to do with copyright as the head of the files. In our JSON file, in “companyName” we complete with “StyleCop DEMO”. The next step is to add the file as an additional parser file. For this, we have to go to the properties of the file and configure it in configuration actions. A ready bird when applying the rule we will see how it puts the new companyName.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zSCooIfP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200831.StyleCop/07.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zSCooIfP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200831.StyleCop/07.gif" alt="StyleCop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can see that the using is marking us. One of the rules says that all using must be declared inside the namespaces:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EAzp6ZYu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200831.StyleCop/08.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EAzp6ZYu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200831.StyleCop/08.png" alt="StyleCop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we want to change this line, and leave the using ones outside the namespace, we can add the rule in our configuration file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;orderingRules&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;usingDirectivesPlacement&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;outsideNamespace&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To see all the rules we can consult the corresponding documentation. But almost always the default rules cover all basic and standard needs.&lt;/p&gt;

&lt;p&gt;Finally, let's see one more example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Customers&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We have a class that does not respect several rules. First, we have the rule "a C# code file contains more than one unique type." executing the rule will move the type to a new c # file. Second, we have the class format rule. Third and last, it does not say that the class must be documented but suppose that we do not want to do so, we can add an exception so that it is not taken into account.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NtVB0pyu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200831.StyleCop/10.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NtVB0pyu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200831.StyleCop/10.gif" alt="StyleCop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;We must always reach an agreement with the team that regulations we want to implement and how serious they can be not respecting them. For this reason, the best way is to let StyleCop do the work for us with only an initial setup effort. This guide is quite simple, but I invite you to continue investigating a little more about this excellent tool.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>vscode</category>
      <category>codequality</category>
    </item>
    <item>
      <title>.Net Core: Multiple Environments</title>
      <dc:creator>Fernando Sonego</dc:creator>
      <pubDate>Sun, 23 Aug 2020 21:38:28 +0000</pubDate>
      <link>https://dev.to/fernandosonego/net-core-multiple-environments-1j33</link>
      <guid>https://dev.to/fernandosonego/net-core-multiple-environments-1j33</guid>
      <description>&lt;p&gt;One of the best features of .Net Core is the ability to work with multiple environments. We can use environments like development, test, and production. This configuration will allow us to modify the behavior according to the environment in which the application is running. The objective of this publication is to discuss the possible uses of this new feature together with good practices in application development.&lt;/p&gt;

&lt;p&gt;Suppose we have 3 environments where we must run or load different configurations: Development, Staging, and Production. Asp.Net Core allows us to distinguish these environments by means of variables called “Environment Variable”. the variable we are referring to is called ASPNETCORE_ENVIROMENT. This is what will allow us to configure our environment. The most common values are Development, Staging, and Production.&lt;/p&gt;

&lt;p&gt;Once this variable is configured, we can detect at runtime what the value of this variable is and in what environment we are running the application.&lt;/p&gt;

&lt;p&gt;We must bear in mind that in Linux the name is case sensitive, for this reason, the files and configurations will be taken as case sensitive as a good practice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Development
&lt;/h2&gt;

&lt;p&gt;This environment is that we will use while we are developing our application. How do we configure it? we must right-click on our project, select configuration, a window will open and select from the left tab "Debug" as we can see in the next window.&lt;/p&gt;

&lt;p&gt;Remember that when I modify this screen, the configuration is modified in the launchSessting.json file inside the properties folder. In this file we can include any environment that we want, for example, we can add several configuration profiles as we can see in the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;iisSettings&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;windowsAuthentication&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;anonymousAuthentication&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;iisExpress&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;applicationUrl&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://localhost:40088/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sslPort&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;profiles&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;IIS Express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;commandName&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;IISExpress&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;launchBrowser&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;environmentVariables&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt; 
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ASPNETCORE_ENVIRONMENT&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Development&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;IIS Express (Staging)&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;commandName&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;IISExpress&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;launchBrowser&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;environmentVariables&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ASPNETCORE_ENVIRONMENT&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Staging&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we change any of these settings in the file, the effects will not be taken automatically. We will have to restart the servers, especially Kestrel will need to restart, once it has been modified to be able to correctly detect the change.&lt;/p&gt;

&lt;h2&gt;
  
  
  Staging
&lt;/h2&gt;

&lt;p&gt;This is the environment that we will use for a test or pre-production environment. Almost always for a final test before making a move to production. This environment should almost always be a production mirror to reduce deployment impact.&lt;/p&gt;

&lt;h2&gt;
  
  
  Production
&lt;/h2&gt;

&lt;p&gt;This is the environment that is often called Alive. It is an environment where we configure security, performance, and reliability characteristics of the application. some of the characteristics that the production environment can have are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Enable cache functions&lt;/li&gt;
&lt;li&gt;Client-side file settings such as packages, shrinking js and css files or CDN (Content Delivery Network) settings&lt;/li&gt;
&lt;li&gt;Disable diagnostic functions&lt;/li&gt;
&lt;li&gt;Activate logging and monitoring settings.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The list can be longer depending on the needs of each application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up the environments
&lt;/h2&gt;

&lt;p&gt;In &lt;strong&gt;Windows&lt;/strong&gt; we can make use of the configuration by means of once our application is running:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Command line&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;set ASPNETCORE_ENVIRONMENT = »Development»&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PowerShell&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;$ Env: ASPNETCORE_ENVIRONMENT = "Development"&lt;/p&gt;

&lt;p&gt;These commands will only be validated while the window where it is being executed is open. If we close it we will lose this configuration. In case we want the value to be global we must configure it from &lt;strong&gt;Control Panel&amp;gt; System&amp;gt; Advanced System Configuration&lt;/strong&gt;, in the Advanced Options tab we must add the &lt;strong&gt;ASPNETCORE_ENVIRONMENT&lt;/strong&gt; variable as we see in the following screenshot:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0JOgobPI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.com/wp-content/uploads/2017/05/Multiples01.png%3Fresize%3D768%252C402%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0JOgobPI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.com/wp-content/uploads/2017/05/Multiples01.png%3Fresize%3D768%252C402%26ssl%3D1" alt="Enviroments"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;macOS&lt;/strong&gt; we must use the following command from bash&lt;/p&gt;

&lt;p&gt;ASPNETCORE_ENVIRONMENT = Development dotnet run&lt;/p&gt;

&lt;p&gt;At the operating system or machine level, we must configure the variables in the .bashrc or .bash.profile files. we must edit the file and add the following:&lt;/p&gt;

&lt;p&gt;export ASPNETCORE_ENVIRONMENT = Development&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;Linux&lt;/strong&gt;, we must use the export command from the console for the open session or modify the bash_profile file for the entire operating system or machine.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to verify the environment at runtime
&lt;/h2&gt;

&lt;p&gt;Asp.Net provides a service based on the IHostingEnviroment interface that is available through Dependency Injection in the main container. In the startup.cs file we can see how the env variable is injected, which will allow us to access the environment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Configure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IApplicationBuilder&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;IHostingEnvironment&lt;/span&gt; &lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;IsDevelopment&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;UseDeveloperExceptionPage&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;UseDatabaseErrorPage&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;UseBrowserLink&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;UseExceptionHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/Home/Error"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To check a specific environment while we are running the application, IHostingEnvironment provides the method IsEnviroment ("Environment name").&lt;/p&gt;

&lt;p&gt;In the code we can see that it is asking if the environment is Development through the method IsDevelopment () which will configure some options for this environment. For example, we see that app.UseBrowserLink () is configured; which is a characteristic of Visual Studio that we will not use in production.&lt;/p&gt;

&lt;p&gt;We also have the possibility to use these configurations through Tag Helper within the MVC Views. For example, we can tell you to use uncompressed css or js files in Development and in Staging and Production to use compressed files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;environment&lt;/span&gt; &lt;span class="na"&gt;include=&lt;/span&gt;&lt;span class="s"&gt;"Development"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;The effective tag is: &lt;span class="ni"&gt;&amp;amp;lt;&lt;/span&gt;environment include="Development"&lt;span class="ni"&gt;&amp;amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/environment&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;environment&lt;/span&gt; &lt;span class="na"&gt;exclude=&lt;/span&gt;&lt;span class="s"&gt;"Development"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;The effective tag is: &lt;span class="ni"&gt;&amp;amp;lt;&lt;/span&gt;environment exclude="Development"&lt;span class="ni"&gt;&amp;amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/environment&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;environment&lt;/span&gt; &lt;span class="na"&gt;include=&lt;/span&gt;&lt;span class="s"&gt;"Staging,Development,Staging_2"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
        The effective tag is:
        &lt;span class="ni"&gt;&amp;amp;lt;&lt;/span&gt;environment include="Staging,Development,Staging_2"&lt;span class="ni"&gt;&amp;amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/environment&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Asp.Net Core has a number of features that we as developers can take very good advantage of as well as great control over the environments in which we usually work. This configuration allows us to change the behavior of our application with a simple change of value in the environment variable.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>vscode</category>
    </item>
    <item>
      <title>Worker Services with Asp.Net Core</title>
      <dc:creator>Fernando Sonego</dc:creator>
      <pubDate>Sun, 16 Aug 2020 15:54:45 +0000</pubDate>
      <link>https://dev.to/fernandosonego/worker-services-with-asp-net-core-18c2</link>
      <guid>https://dev.to/fernandosonego/worker-services-with-asp-net-core-18c2</guid>
      <description>&lt;p&gt;Since the release of Asp.Net Core 3 we have a new feature called Worker Services. These services are not really a novelty, the concept has been around for a long time and you have surely used it before when our web applications needed to run some task periodically in the background, such as a notification via email.&lt;/p&gt;

&lt;p&gt;Before .Net Core 3.0 we could do them, but now from this version, we have templates available to create them. All these kinds of background tasks can also be implemented as a Windows Service or a Daemon in Linux.&lt;/p&gt;

&lt;p&gt;In this post we will see the creation of a Worker Service that will periodically log something particular and how it will be consumed. For this we will use VS Code. We can use Visual Studio 2019. Let's get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  Worker Services
&lt;/h2&gt;

&lt;p&gt;To begin we create the template project from the console with the command:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;dotnet new worker&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's open the code with the VSCode we will see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eg35u69V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200816.Worker/worker01.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eg35u69V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200816.Worker/worker01.png" alt="Worker Service"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see, after creating the project, we have 3 fundamentals files: &lt;strong&gt;appsettings.json&lt;/strong&gt;, &lt;strong&gt;programs.cs&lt;/strong&gt;, and &lt;strong&gt;worker.cs&lt;/strong&gt;. Let's start by looking at the program.cs file. Inside we will find the configuration of how the service should be hosted and the configuration of its dependency injection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="nf"&gt;CreateHostBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;Build&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;

     &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;IHostBuilder&lt;/span&gt; &lt;span class="nf"&gt;CreateHostBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
         &lt;span class="n"&gt;Host&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;CreateDefaultBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
             &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ConfigureServices&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;hostContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
             &lt;span class="p"&gt;{&lt;/span&gt;
                 &lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddHostedService&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Worker&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
             &lt;span class="p"&gt;});&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we need to add some extra service, for example, to send mail or some kind of notification, we will have to inject it from this section.&lt;/p&gt;

&lt;p&gt;Let's now look at our worker.cs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Worker&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;BackgroundService&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;ILogger&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Worker&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_logger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ILogger&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Worker&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;_logger&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;ExecuteAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CancellationToken&lt;/span&gt; &lt;span class="n"&gt;stoppingToken&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;stoppingToken&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsCancellationRequested&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;_logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogInformation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Worker running at: {time}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;DateTimeOffset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stoppingToken&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We see that our class inherits from &lt;strong&gt;BackgroundService&lt;/strong&gt; which is the one that implements &lt;strong&gt;IHostedService&lt;/strong&gt;. You must implement &lt;strong&gt;ExecuteAsync&lt;/strong&gt; which will be the method that runs in the background. We must bear in mind that we must always use the cancellation token to be able to quickly end and close the service correctly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Worker&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;BackgroundService&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;ILogger&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Worker&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_logger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ILogger&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Worker&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;_logger&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;ExecuteAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CancellationToken&lt;/span&gt; &lt;span class="n"&gt;stoppingToken&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;stoppingToken&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsCancellationRequested&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;_logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogInformation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Worker running at: {time}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;DateTimeOffset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stoppingToken&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can run it to see how the default template works from the console with dotnet new. We will see how the service is called once per second.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--toiA_-7P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200816.Worker/worker02.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--toiA_-7P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200816.Worker/worker02.gif" alt="Worker Service"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can implement service initialization and termination events to inform us of these events. For this, we will create the class &lt;strong&gt;WorkerExtend&lt;/strong&gt;.cs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WorkerExtend&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;BackgroundService&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;ILogger&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;WorkerExtend&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_logger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;WorkerExtend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ILogger&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;WorkerExtend&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;_logger&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;StartAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CancellationToken&lt;/span&gt; &lt;span class="n"&gt;cancellationToken&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;           
            &lt;span class="n"&gt;_logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogInformation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Worker Start: {time}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;DateTimeOffset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;StartAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cancellationToken&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;StopAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CancellationToken&lt;/span&gt; &lt;span class="n"&gt;cancellationToken&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;           
            &lt;span class="n"&gt;_logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogInformation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Worker Stop: {time}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;DateTimeOffset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;StopAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cancellationToken&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;ExecuteAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CancellationToken&lt;/span&gt; &lt;span class="n"&gt;stoppingToken&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;stoppingToken&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsCancellationRequested&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;_logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogInformation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Worker running at: {time}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;DateTimeOffset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stoppingToken&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Dispose&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We will execute it from the console and we will see the outputs of the corresponding events.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dzJWxjs0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200816.Worker/worker03.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dzJWxjs0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200816.Worker/worker03.gif" alt="Worker Service"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, before commenting that we can implement it as a Windows Service. To do so, we just have to add the injection to do it in the program.cs file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;IHostBuilder&lt;/span&gt; &lt;span class="nf"&gt;CreateHostBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
            &lt;span class="n"&gt;Host&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;CreateDefaultBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;UseWindowsService&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ConfigureServices&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;hostContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
                &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddHostedService&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;WorkerExtend&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
                &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then to implement it we can use the sc.exe tool in the following way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;sc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exe&lt;/span&gt; &lt;span class="n"&gt;create&lt;/span&gt; &lt;span class="n"&gt;DemoWS&lt;/span&gt; &lt;span class="n"&gt;binpath&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PathToThePublishFolder&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;YourWorkerClassName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exe&lt;/span&gt;
&lt;span class="n"&gt;sc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exe&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="n"&gt;YourWorkerClassName&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In previous versions of .Net Core we could work with Workers, let's keep that in mind. The advantage of this new template is that it allows us to have small separate services that are easy to maintain. Running these services in the background has a very low processing cost that makes them quite useful to use even if they are long running. Hope you enjoy this functionality. See you in the next post.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>Productivity with Visual Studio: Refactoring | Part 4</title>
      <dc:creator>Fernando Sonego</dc:creator>
      <pubDate>Sun, 09 Aug 2020 21:22:02 +0000</pubDate>
      <link>https://dev.to/fernandosonego/productivity-with-visual-studio-refactoring-part-4-6fh</link>
      <guid>https://dev.to/fernandosonego/productivity-with-visual-studio-refactoring-part-4-6fh</guid>
      <description>&lt;p&gt;In this last installment, we will talk about Refactoring. Many of us surely use ReSharper to do effective code refactoring. Since the 2019 release of Visual Studio, refactoring capabilities have been greatly improved making other tools unnecessary. Let's see what we got.&lt;/p&gt;

&lt;h2&gt;
  
  
  Change identifier names
&lt;/h2&gt;

&lt;p&gt;We can change the names of objects or identifiers such as variables, fields, classes, etc. To do this we will select the identifier and press Ctrl + R + R  (R 2 times). As we start typing the new identifier name, we'll see how it changes in the places where the identifier is located.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AZ3Pwr1t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/01.01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AZ3Pwr1t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/01.01.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Keep in mind that this change may not only affect the current file. We may need to rename references or rename files. Before making the final change, you can see in the preview window the changes that will be made. In the context window, we should select "Preview Changes" and press Apply.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---_unL0Ya--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/01.02.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---_unL0Ya--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/01.02.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Extract methods
&lt;/h2&gt;

&lt;p&gt;You can activate Quick Actions and Refactorings using the Ctrl + key. I think it's one of the shortcuts that we should always keep in mind, if you didn't know it until now, paste a post-it on the monitor with this command.&lt;/p&gt;

&lt;p&gt;If we want to extract a snippet to a method, we need to do the following steps: first, we select the desired lines of code, then press Ctrl +. , finally, we select  "Extract method". Once selected it will be positioned in place, it will be green so that we type the name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--b4AYKX2q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/02.01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--b4AYKX2q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/02.01.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Clean and sort
&lt;/h2&gt;

&lt;p&gt;Many times, when we refactor, maybe the using is deprecated or we add new ones quickly without having a default order. We can automatically clean and sort using from the menu Edit &amp;gt; Intellisense &amp;gt; Remove and Sort Usings.&lt;/p&gt;

&lt;p&gt;It does not have a hotkey, but we can configure from the menu Tools &amp;gt; Options &amp;gt; Keyboards.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FPeK3JQT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/03.01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FPeK3JQT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/03.01.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another way, perhaps faster, is to position ourselves on the using and press the yellow lamp on the left. We will choose the option to Remove Unnecessary Usings. The most interesting thing about this window is that down to the right we can see several options, not only can we clean the document, but we can also do in a project or in the complete solution.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IFQ6Skd6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/03.02.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IFQ6Skd6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/03.02.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Using missing when copying and pasting
&lt;/h2&gt;

&lt;p&gt;When we copy and paste code snippets, we may have errors due to the lack of the using needed for the east. To fix it, after pasting the snippet we press Ctrl + . and we choose to Add missing usings.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S2KC-lNa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/04.01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S2KC-lNa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/04.01.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Create properties from the constructor
&lt;/h2&gt;

&lt;p&gt;Suppose we have a constructor that receives some variables. Easily, we can convert all these variables into properties, and they will be self-assigned within the constructor. This functionality is very useful when working with dependency injection. We may not need properties, but, the declaration of a private variable and its assignment, we can also do so.&lt;/p&gt;

&lt;p&gt;To use the functionality we must stop over the declaration, press Ctrl +. , and the option  "Create and initialize property". If we want it to be a field, we choose "Create and initialize field".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SMzmJawp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/05.01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SMzmJawp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/05.01.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Create constructor from properties
&lt;/h2&gt;

&lt;p&gt;Like the previous one, we can generate a constructor from a set of properties. Choose the fragment with the properties, press Ctrl +. , and choose  "Generate Constructor".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VKUZHd13--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/05.02.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VKUZHd13--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/05.02.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Convert a foreach expression to Linq
&lt;/h2&gt;

&lt;p&gt;Many times, when we have one loop, within another and we have a conditional within the latter, it is possible to convert it to a simple LINQ expression. You may not be able in some cases, but for the most part, it is possible. If the pattern we see in the image is met, we can do it.&lt;/p&gt;

&lt;p&gt;Select the entire foreach block, press Ctrl + . and choose "Convert to LINQ".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HnrEmWXe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/06.01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HnrEmWXe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/06.01.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Extract Class to an Interface
&lt;/h2&gt;

&lt;p&gt;This option is for the laziest, who prefer to write the class first and then the interface. We must position ourselves in the class, press Ctrl +. and choose the option "Extract Interface". It will show you a dialog box with the options with which you will create the interface. Press Ok and automatically add inheritance to the class from which we extracted the interface and create the file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fMI_XXry--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/07.01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fMI_XXry--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/07.01.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Move to Namespaces
&lt;/h2&gt;

&lt;p&gt;When we create a new code file in C-, Visual Studio, we automatically complete the Namespaces. Let's say we create a new class, complete it, and later realize that we create it in another folder, and it has other Namespaces. We can move it, but it is left with the old Namespaces. If we position ourselves on the object and press Ctrl + . will give us the option "Move to Namespace". This option will not open a dialog box to change the Namespaces, even has a list from which we can choose the correct one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qVZ4IkUY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/08.01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qVZ4IkUY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/08.01.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Add parameter to an existing method
&lt;/h2&gt;

&lt;p&gt;Suppose we are invoking a method and realize that it is missing a parameter or overload. We can invoke the method and type the missing parameter, then select the parameter, press Ctrl + . and we chose the option "Add parameter to".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bLiTHW5f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/09.01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bLiTHW5f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/09.01.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Simplify string interpolation
&lt;/h2&gt;

&lt;p&gt;The correct path to making interpolations was always String.Format. Over time, improvements were added to C# to be done in a simpler way by means of $ and the { }.  It is possible to transform the first to the second structure. Choose the String.Format, press Ctrl +. , and choose the option  "Convert to interpolated string".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8QVuCLh_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/10.01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8QVuCLh_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200808.ProductivityVS4/10.01.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;With the advancement of time, with each new update, Visual Studio becomes an increasingly powerful tool. Since the 2019 releases it has improved in its refactoring functionalities and many more are on the way. I thank you for having accompanied me in this post series. See you in the next post.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>productivity</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>Productivity with Visual Studio: Debug | Part 3</title>
      <dc:creator>Fernando Sonego</dc:creator>
      <pubDate>Sun, 02 Aug 2020 23:44:28 +0000</pubDate>
      <link>https://dev.to/fernandosonego/tutorial-productivity-with-visual-studio-debug-part-3-1050</link>
      <guid>https://dev.to/fernandosonego/tutorial-productivity-with-visual-studio-debug-part-3-1050</guid>
      <description>&lt;p&gt;Until now, in the first part and in the second, we have seen tricks for the code editor and improving our navigation in it. Now it's time for some tricks to improve our debugging.&lt;/p&gt;

&lt;p&gt;Most of us know the basic commands like F5 to start debugging, F9 to set a breakpoint, or F11 to get into the implementation. In addition to these, there are many functionalities in the tool that we can use to make our work easier.&lt;/p&gt;

&lt;h2&gt;
  
  
  Move the execution point
&lt;/h2&gt;

&lt;p&gt;The first is to move the point where it is running. On the left side of the editor, we can find a yellow arrow, this indicates where the execution is stopped. If we need to go to another line to run from there, all we have to do is hold down the arrow and drag it to the desired place.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--emcTQ5ic--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200727.ProductivityVS3/00.01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--emcTQ5ic--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200727.ProductivityVS3/00.01.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Run up to a point
&lt;/h2&gt;

&lt;p&gt;Suppose we have a breakpoint on the first line, but we want to run up to one in particular. We can do is click on the line and press &lt;strong&gt;Ctrl + F10&lt;/strong&gt; or press right-click and select it from the Run to Cursor context menu &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7Aurt2qh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200727.ProductivityVS3/01.01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7Aurt2qh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200727.ProductivityVS3/01.01.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another path is through the green marker to the left of the code line. We position ourselves on the line to which we want to go, on the right, the green marker will appear, indicating that we can advance to that point by clicking on it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vCB1C9Un--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200727.ProductivityVS3/02.01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vCB1C9Un--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200727.ProductivityVS3/02.01.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can go to an execution line without the need to execute the middle code. Again, if we stand on the line, we see the green marker on the left side. If we press the control, we will see that it turns yellow, we click and it will go to the line without executing the intermediate code. It is for this reason that when viewing the tracking window the object is null.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--P7WKtJMG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200727.ProductivityVS3/03.01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P7WKtJMG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200727.ProductivityVS3/03.01.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Set breakpoints
&lt;/h2&gt;

&lt;p&gt;Breakpoints don't just stop because we mark them. We can configure a breakpoint by configuring different conditions or when a particular event happens. It is very useful when we are debugging and we depend on the values ​​that the objects contain.&lt;/p&gt;

&lt;p&gt;We can configure a property of an object to stop when it has changed. In the image, we can see how we should do it. We configure that when changing ProductId of the product object it stops. Notice how the line is gray.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lUCmME0o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200727.ProductivityVS3/04.01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lUCmME0o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200727.ProductivityVS3/04.01.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Suppose we need to stop execution when a property takes a specific value. For this, on the red marker of the breakpoint, we press on the configuration gear and select Conditions. We see that we have several configuration parameters. We complete it to stop when p, which is inside a loop, in the Description property is equal to “Product 2”. This will make the loop run until the condition is as desired.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JKncadr_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200727.ProductivityVS3/05.01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JKncadr_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200727.ProductivityVS3/05.01.gif" alt="Home"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now instead of stopping, we also want to see all the outputs of the loop. We can use Actions. Again, on the breakpoint, we select the gear and select Actions. Here we can write a text and then enter {} the name of the variable or object that we want to print in the output window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9lcZpK0u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200727.ProductivityVS3/06.01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9lcZpK0u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200727.ProductivityVS3/06.01.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  View function values
&lt;/h2&gt;

&lt;p&gt;Many times, we execute a method that returns a value, but we ignore it or do not assign it to a variable. This makes it not accessible at run time or debugging. The debugging time can be seen, if necessary, from the auto window. We can open it, at debugging time, from &lt;strong&gt;Debug&amp;gt; Windows&amp;gt; Auto&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8ZVduCIZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200727.ProductivityVS3/08.01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8ZVduCIZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200727.ProductivityVS3/08.01.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Verify unchanged objects from immediate window and observation
&lt;/h2&gt;

&lt;p&gt;Whenever we execute a method from the immediate window we change the value of some property or object. As we see in the image, each time we execute the method, the value in the observation window is updated, notifying us that the value has changed. To have no effect, we can use the prefix &lt;strong&gt;nse&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HLNEqeLA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200727.ProductivityVS3/10.01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HLNEqeLA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200727.ProductivityVS3/10.01.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Decompile the IL code
&lt;/h2&gt;

&lt;p&gt;Many times we create libraries to reuse in our projects. We refer to our project to the assembly that has a dll extension. In this example, we have Math.dll, it is a project that I create to use in the example.&lt;/p&gt;

&lt;p&gt;Once we referenced it, it will work as a black box, where we can't see the implementation. Visual Studio gives us the possibility of being able to do it even though it is compiled. This functionality allows us to decompile the component and have the implementation code available.&lt;/p&gt;

&lt;p&gt;To access the value we must use what is called a pseudo-variable, in this case, &lt;strong&gt;$ReturnValue&lt;/strong&gt;. As we see in the image, after executing the method, we immediately write &lt;strong&gt;$ReturnValue&lt;/strong&gt; in the window and we will see the result.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vk00vyEG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200727.ProductivityVS3/12.01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vk00vyEG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200727.ProductivityVS3/12.01.gif" alt="Productivity with Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;With all this set of functionalities and tools, we can make it easier to search for any problem that we have in our application. In the next post, we will see tricks for refactoring. I hope you find it useful and take advantage of it.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>vscode</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Productivity with Visual Studio: Navigation | Part 2</title>
      <dc:creator>Fernando Sonego</dc:creator>
      <pubDate>Sun, 26 Jul 2020 23:36:54 +0000</pubDate>
      <link>https://dev.to/fernandosonego/productivity-with-visual-studio-navigation-part-2-347l</link>
      <guid>https://dev.to/fernandosonego/productivity-with-visual-studio-navigation-part-2-347l</guid>
      <description>&lt;p&gt;In the previous post, we saw how to increase our productivity by maximizing the use of Visual Studio. Now it's up to the topic of Navigation tricks in the tool. All these functionalities that we will see will help us move more quickly within the code and go to the exact points that we want in our project or solution.&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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.00.01.JPG" 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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.00.01.JPG" alt="Home"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first thing we learn to use is the command &lt;strong&gt;ctrl + shift + f&lt;/strong&gt;, but we have much more. Fortunately, the tool offers us many functionalities to achieve better navigation in the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Search bar
&lt;/h2&gt;

&lt;p&gt;Above the title bar, we have a search bar available. Very few people see that it is there. If you work with Visual Studio Code you surely know this functionality, they are very similar in Visual Studio. This allows us to search through various objects such as Code elements, files, Visual Studio command, Settings, and much more!&lt;/p&gt;

&lt;p&gt;we can see in the image, we write, for example, IApp Visual Studio will search for everything related to interfaces, configurations, files, etc. As we continue to write, the search listing will change. Just clicking on an item in the list will take us to the desired place.&lt;/p&gt;

&lt;p&gt;If we write command, such as Go To Line, it not only shows us the shortcut but also, if we click on it, it activates the command showing us the dialog window to select the line we want to go to.&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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.01.01.gif" 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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.01.01.gif" alt="Search bar"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These search tools are very powerful, but we have even more in the next section.&lt;/p&gt;

&lt;h2&gt;
  
  
  Elements in the code
&lt;/h2&gt;

&lt;p&gt;When we must search for elements in our code the best way is to use the combination &lt;strong&gt;ctrl + t&lt;/strong&gt;. The difference with the previous one is that instead of searching the entire Visual Studio, it will allow us to search for elements related to the code: Lines, Files, Types, members, and Symbols.&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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.02.01.gif" 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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.02.01.gif" alt="Elements in the code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we write for example &lt;strong&gt;IApplication&lt;/strong&gt; again, it will search all our code elements, you can see in the image even in a test project. If we wanted to search only the interfaces or the related type we can add the filter t using it this way &lt;strong&gt;t IApplication&lt;/strong&gt;. We can learn each of the filters to do a faster search. To further refine the search, we can use &lt;strong&gt;ctrl + alt + c&lt;/strong&gt; limiting the search to the current document, or &lt;strong&gt;alt + x&lt;/strong&gt;, for the rest.&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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.02.02.gif" 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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.02.02.gif" alt="Elements in the code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Something interesting we can do is customize it. At the top right, we can see a gear, selecting it will give us 3 options, use in a tab preview, show the details and center the result on the screen. We can adjust it to our comfort.&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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.02.03.JPG" 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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.02.03.JPG" alt="Elements in the code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Go to the definition or see it in the same editor
&lt;/h2&gt;

&lt;p&gt;Something we commonly do is go to the definition of a class or interface to see what it has implemented. We do this with the &lt;strong&gt;F12&lt;/strong&gt; key. This key takes us directly to the definition by opening the file involved, but sometimes, it is tedious having to change tabs to get back to where we were working. For this, we have the following improvement using &lt;strong&gt;alt + f12&lt;/strong&gt;. This will open a small window, within the editor, that will let us see the implementation code without changing places or opening a new tab.&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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.03.01.gif" 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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.03.01.gif" alt="Go to the definition or see it in the same editor"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we want to see all the available navigation options, we can right-click on our code and the context menu will open. Note all the options that we have available:&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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.03.02.JPG" 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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.03.02.JPG" alt="Go to the definition or see it in the same editor"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Search References
&lt;/h2&gt;

&lt;p&gt;To quickly see the references on an element or object we must position ourselves on it and press &lt;strong&gt;shift + f12&lt;/strong&gt;. This will open the references window:&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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.04.01.gif" 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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.04.01.gif" alt="Search References"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To quickly see the references on an element or object we must position ourselves on it and press &lt;strong&gt;shift + f12&lt;/strong&gt;. This will open the references window:&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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.04.02.gif" 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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.04.02.gif" alt="Search References"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, we can group the results by projects, types, by files and some more.&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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.04.03.JPG" 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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.04.03.JPG" alt="Move lines up or down"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  CodeLens
&lt;/h2&gt;

&lt;p&gt;This is one of the features that I like, when it came out it was not to the liking of many people, but over time, it became so useful that everyone loves them today. Above identifiers, we can see the word reference with the number of references that the identifier has. With Git we can see the last modification and commits if we select it.&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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.05.01.gif" 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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.05.01.gif" alt="CodeLens"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see in the image, when clicking on references, the list will open and we will be able to choose an item that will take us to the corresponding code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Go to Base or implementation
&lt;/h2&gt;

&lt;p&gt;When positioning on an identifier we can do 2 things, first pressing alt + home will take us to the base class or the implementation interface. But, if we press &lt;strong&gt;ctrl + f12&lt;/strong&gt; it will take us to the derived classes or classes that implement an interface.&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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.06.01.gif" 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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.06.01.gif" alt="Go to Base or implementation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Call hierarchy
&lt;/h2&gt;

&lt;p&gt;The call hierarchy allows us to navigate through all the invocations from a method, property, or constructor, allowing us to examine various levels of code and to see the complex paths of one or several calls.&lt;/p&gt;

&lt;p&gt;To use it we must stop on a member, we will press &lt;strong&gt;ctrl + k ctrl + t&lt;/strong&gt;, the window will open where we can see the hierarchy of calls.&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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.07.01.gif" 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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.07.01.gif" alt="Call hierarchy"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Find Comments: TODO
&lt;/h2&gt;

&lt;p&gt;According to Clean Code practices, the only comment allowed is TODO. TODO allows us to leave a comment for something we should do later. We can track these comments from the Task List window that Visual Studio has.&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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.08.01.gif" 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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.08.01.gif" alt="Find Comments: TODO"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can define a query if we want, we can do this from, &lt;strong&gt;Option &amp;gt; Environment &amp;gt; Task List&lt;/strong&gt;:&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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.08.02.JPG" 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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.08.02.JPG" alt="Find Comments: TODO"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Lines of code
&lt;/h2&gt;

&lt;p&gt;In the previous post, we saw that we can put marks in our code, this is something similar to those marks. We can put direct access to a line of code with the &lt;strong&gt;ctrl + k + h&lt;/strong&gt; keys and with the same combination, we deactivate it.&lt;/p&gt;

&lt;p&gt;Once we place a mark we can visualize them in the Task List. Something interesting is that if we close Visual Studio, they will remain the next time we resume work.&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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.09.01.gif" 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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.09.01.gif" alt="Lines of code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Go to closings and select by block
&lt;/h2&gt;

&lt;p&gt;We can stop on a { and go to the corresponding closing } by pressing the corresponding key &lt;strong&gt;ctrl }&lt;/strong&gt;. If we are standing on }, pressing &lt;strong&gt;ctrl {&lt;/strong&gt;, it will go to the start. This can also be done with () [] and &amp;lt;&amp;gt;.&lt;/p&gt;

&lt;p&gt;Also, if we press Ctrl + Shift }, it will select the block.&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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.10.01.gif" 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%2Fwithoutdebugger.azureedge.net%2Fblog%2F20200720.ProductivityVS2%2FProductivityVS2.10.01.gif" alt="Go to closings and select by block"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;I hope they are useful and help you increase your productivity. Remember, with a little practice, you will be able to remember and master them without problems. In future posts, we will see other functionalities that allow us to be more productive with this wonderful tool called Visual Studio.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>vscode</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Productivity with Visual   Studio: Code Editor | Part 1</title>
      <dc:creator>Fernando Sonego</dc:creator>
      <pubDate>Tue, 21 Jul 2020 00:55:47 +0000</pubDate>
      <link>https://dev.to/fernandosonego/productivity-with-visual-studio-code-editor-part-1-j4n</link>
      <guid>https://dev.to/fernandosonego/productivity-with-visual-studio-code-editor-part-1-j4n</guid>
      <description>&lt;p&gt;In recent days we entered into a discussion with some friends and colleagues on how to increase productivity while developing applications. For my part, I suggested that the first thing we should do, before learning complex programming patterns or techniques, is to know perfectly with the tool we are working with. This made me think about doing a series of posts to see some tricks to increase our productivity with Visual Studio.&lt;/p&gt;

&lt;p&gt;In this first part, we will see some tricks for the code editor, in future posts, we will see debugging, refactoring, and other quite interesting tricks. I recommend that these tricks make them part of your daily work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Move lines up or down
&lt;/h2&gt;

&lt;p&gt;We can select one or several lines of our code to move them up or down. It is as simple as selecting the lines and pressing alt plus the up or down key. &lt;strong&gt;alt+⬆️ alt+⬇️&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fAcWw4yF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.01.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fAcWw4yF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.01.gif" alt="Move lines up or down"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Select in the shape of a rectangle
&lt;/h2&gt;

&lt;p&gt;It is very useful when we need to edit portions of code on multiple lines and multiple columns. We can activate it by pressing &lt;strong&gt;alt + shift&lt;/strong&gt; and pressing the arrows.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tNvCNriZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.02.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tNvCNriZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.02.gif" alt="Select in the shape of a rectangle"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Edit multiple lines at once
&lt;/h2&gt;

&lt;p&gt;Using the above functionality, alt + shift + arrows, we select a text and several lines and we will directly start typing. We will see that everything that was selected in the rectangle will begin to change as we type.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JPqcvH2m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.03.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JPqcvH2m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.03.gif" alt="Edit multiple lines at once"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Editing in multiple places at the same time
&lt;/h2&gt;

&lt;p&gt;At some point, you surely needed to insert or edit the same text in different places. We can do this by choosing multiple entry points. We must hold down &lt;strong&gt;ctrl + alt&lt;/strong&gt;, then, with the left mouse button, press in the places where we want to use as entry points. &lt;strong&gt;ctrl + alt + click&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NoL7WjYf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.04.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NoL7WjYf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.04.gif" alt="Editing in multiple places at the same time"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Selection of code blocks
&lt;/h2&gt;

&lt;p&gt;Suppose we want to select code blocks for their closure, in C# for example {...}, standing inside we can use the key combination &lt;strong&gt;alt + shift + =&lt;/strong&gt; to select outward if we want to make an inward select we will use &lt;strong&gt;alt + shift + -&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sz2imsDT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.06.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sz2imsDT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.06.gif" alt="Selection of code blocks"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Lowercase to uppercase to lowercase
&lt;/h2&gt;

&lt;p&gt;Sometimes we have to change upper or lower case texts or vice versa. We can select the text and press &lt;strong&gt;ctrl + shift + u&lt;/strong&gt;, for upper case and &lt;strong&gt;ctrl + u&lt;/strong&gt; to make it lower case.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SmBHzSmu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.07.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SmBHzSmu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.07.gif" alt="Lowercase to uppercase to lowercase"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Expand, collapse and collapse our code
&lt;/h2&gt;

&lt;p&gt;Visual Studio allows us to collapse, expand or contract our code depending on the language and the block closing structure, for example in C# es {..}. we always see that on the left side of the editor window the + or - to be able to do it with the mouse. It is also possible to do it with the key combination &lt;strong&gt;ctrl + m ctrl + m&lt;/strong&gt; (collapses), &lt;strong&gt;ctrl + m ctrl + p&lt;/strong&gt; (expands).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Pydqzl6S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.08.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Pydqzl6S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.08.gif" alt="Expand, collapse and collapse our code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Code map
&lt;/h2&gt;

&lt;p&gt;This is one of the most useful functionalities of the code editor in Visual Studio, also present in Visual Studio Code, it does not help to quickly position ourselves somewhere in our code file, especially if it is very extensive.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7TMr6xIt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.09.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7TMr6xIt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.09.gif" alt="Code map"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we want to change the behavior of the bar we can do it from the Visual Studio configuration. We should go: &lt;strong&gt;Tools &amp;gt; Options &amp;gt; Text Editor &amp;gt; All Languages &amp;gt; Scroll Bars &amp;gt; Behavior&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Zent-dJN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.09.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Zent-dJN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.09.JPG" alt="Code map"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Markers
&lt;/h2&gt;

&lt;p&gt;Maybe the same thing happens to me, many times we have a large number of open files editing them at the same time. There are several ways to get from one place to another in the code, such as Go to the definition. Other times it is not possible. This is where bookmarks will be useful to us, we can put them in our lines of code to go from one place to another and they remain, although we close Visual Studio, they are:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ctrl+K, Ctrl+K,&lt;/strong&gt; put a marker.&lt;br&gt;
&lt;strong&gt;Ctrl+K Ctrl+N,&lt;/strong&gt; go to the next marker.&lt;br&gt;
&lt;strong&gt;Ctrl+K Ctrl+P,&lt;/strong&gt; go to the previous marker.&lt;br&gt;
&lt;strong&gt;Ctrl+K Ctrl+L,&lt;/strong&gt; clear all our bookmarks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TuKXL-j0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.10.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TuKXL-j0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.10.gif" alt="Markers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Multiple Clipboards
&lt;/h2&gt;

&lt;p&gt;We copy a lot of code all the time, we copy something, then we copy another, we use it, but we also needed the old one. Visual Studio keeps a history of all the &lt;strong&gt;ctrl + c&lt;/strong&gt; that we do while we are working. To access history we must stop at the place where we want to insert any of the codes copied previously and press &lt;strong&gt;ctrl + shift + v&lt;/strong&gt; and the history will be displayed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5jES5z5O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.11.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5jES5z5O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://withoutdebugger.azureedge.net/blog/20200711.ProductivityVS1/ProductivityVS1.11.gif" alt="Multiple Clipboards"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;After these tips and practicing them a lot, we can increase our productivity. Remember, this is not simple at first, it takes practice and a good memory. Many of these functionalities are also present in the Visual Studio Code, so you can use them without problems. In future posts, we will see more tricks for Visual Studio that help us increase our Productivity.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>visualstudio</category>
      <category>dotnet</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Upgrading .Net Core 3.1 to Net 5.0 Preview 6</title>
      <dc:creator>Fernando Sonego</dc:creator>
      <pubDate>Mon, 13 Jul 2020 00:19:28 +0000</pubDate>
      <link>https://dev.to/fernandosonego/upgrading-net-core-3-1-to-net-5-0-preview-6-1924</link>
      <guid>https://dev.to/fernandosonego/upgrading-net-core-3-1-to-net-5-0-preview-6-1924</guid>
      <description>&lt;p&gt;We are getting closer to the final release of .Net 5, the new version of this wonderful framework created by Microsoft. This new version, as we saw in other posts, is not a simple update, it is the unification of what we know with .Net Core and .Net Framework. As it is not a minor issue, Microsoft has recommended migrating all the applications that we have in previous versions to .Net Core 3.1 to this version. Considering that time is running out and that many of us have a curious soul, I leave this guide to update to .Net 5 to test our applications on the new version of the platform.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nJj6y0mE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/uuszCF9SVunWQGuM2ACU6jiZk4RbDKq4ec3qRFyKB2-alVDHzcR98o59JJl2UzyEJTA4fxRphwzdD4US8z9vopBTZTU0-JAFnxT5R38QjxghB6iSPzRmVI0fDx3nq_TxEpnfyVx-" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nJj6y0mE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/uuszCF9SVunWQGuM2ACU6jiZk4RbDKq4ec3qRFyKB2-alVDHzcR98o59JJl2UzyEJTA4fxRphwzdD4US8z9vopBTZTU0-JAFnxT5R38QjxghB6iSPzRmVI0fDx3nq_TxEpnfyVx-" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we talked, we are going to see the necessary steps to update a project to the current version of .Net 5 which is Preview 6. Before we start we must have 3 points in mind:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Although this Preview is quite advanced, they may have some errors that correspond to the version, the recommendation that I can give you is to review the release notes for particular cases. Also, be patient, we will surely have a new Preview in a short time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Let's take this practice as evidence of future migrations and that the new products we will make will be of a quick update. This means that once the final version of .Net 5 is there, we should not stop there, we must start thinking about the next version.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Any problem or error that we find, let's not stop reporting it to the Microsoft team, this will help make the next preview or version more solid and stable. Also, if we find any recommendation and opinion. The team that carries out this framework is open to all the feedback we can give you.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The first thing to do is download the latest available version of .Net 5. You can download it from the following link &lt;a href="https://dotnet.microsoft.com/download/dotnet/5.0"&gt;dotnet5&lt;/a&gt; . Once downloaded we will proceed to install it. We will see the image screen, we just have to follow the steps.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X-pEjSVQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/60ORYkZrkzX1cmfcgOfoBb6QKjam6KaJDi1HaTNjQIj1FV8NU4PxM1d1i23P6GgNhZJruMLBJjbg-K153o0yiXec1CziU3L3b8erYAv_fv0kJnMPzCorjPz7gxWHAypkoHWw2RvR" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X-pEjSVQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/60ORYkZrkzX1cmfcgOfoBb6QKjam6KaJDi1HaTNjQIj1FV8NU4PxM1d1i23P6GgNhZJruMLBJjbg-K153o0yiXec1CziU3L3b8erYAv_fv0kJnMPzCorjPz7gxWHAypkoHWw2RvR" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To correctly verify the version, we will open the Windows console and execute the command dotnet --info.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_I3szFSF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/I95XaEXn7zLg6BDJ0FhSzS8OZ2E38AFKqX-lv4QhlqoZlBlJwkr2r8-9oe_mJZItbSRz4hsxJUpGAk_GVM0wgzBvbjT0TU5VjWnQm8ZbcY1I5VTfloPW2DJvK1-B0lnxQRyzars8" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_I3szFSF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh6.googleusercontent.com/I95XaEXn7zLg6BDJ0FhSzS8OZ2E38AFKqX-lv4QhlqoZlBlJwkr2r8-9oe_mJZItbSRz4hsxJUpGAk_GVM0wgzBvbjT0TU5VjWnQm8ZbcY1I5VTfloPW2DJvK1-B0lnxQRyzars8" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The next thing will be to install the Preview version of Visual Studio 2019 that is currently in version 16.7 Preview 3.1, you can download it from this link &lt;a href="https://docs.microsoft.com/en-us/visualstudio/releases/2019/release"&gt;https://docs.microsoft.com/en-us/visualstudio/releases/2019/release&lt;/a&gt; -notes-preview . In my case I will use the professional version, but you can choose any version you want.&lt;/p&gt;

&lt;p&gt;In the installation steps we will only choose that we want to install the development tools "Asp.Net and Web development".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IothgEHX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/iqKzYQyHcnVQdfRmf2OnbgoDoW5IwWKInHWhj9pA0gbaPt8eKHDGoOOthZoU4MxirDHacxNCDOM-BjpIIe7j_6l8rP0dkHz60U1IKWORz6Q-KJFlw9XizvRVAqTQrX6i7k-T912l" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IothgEHX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh4.googleusercontent.com/iqKzYQyHcnVQdfRmf2OnbgoDoW5IwWKInHWhj9pA0gbaPt8eKHDGoOOthZoU4MxirDHacxNCDOM-BjpIIe7j_6l8rP0dkHz60U1IKWORz6Q-KJFlw9XizvRVAqTQrX6i7k-T912l" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will create a solution with 3 types of projects: Web MVC Razor, Web API and Test Unit that we will use to do the update process.&lt;/p&gt;

&lt;p&gt;The Web project and WebApi are using .Net Core 3.1, they are based on the MVC model. This base template will have the drivers for defaults that come in the templates, for example, in the WebApi project, we will have a driver that returns weather information.&lt;/p&gt;

&lt;p&gt;In both projects we must update the csproj file with the new version. To be able to do it we must select the project, right click, "Edit Project File".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NpX4JLuf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/a1U6tNVdVqHiaqWxuBfpQtzp8Udr9MIJQ0CNnUHoAAXB_vHVpLxUB38KWHVbZuPdNWexCTJyCwxR4DuX0agT-n5ZKlYxmIuRd55WxRVG8NxZhA0oKxZPcAp0T0Kd2NRIGoWtPdDe" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NpX4JLuf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://lh3.googleusercontent.com/a1U6tNVdVqHiaqWxuBfpQtzp8Udr9MIJQ0CNnUHoAAXB_vHVpLxUB38KWHVbZuPdNWexCTJyCwxR4DuX0agT-n5ZKlYxmIuRd55WxRVG8NxZhA0oKxZPcAp0T0Kd2NRIGoWtPdDe" alt="Alt text of image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Both projects will have the same configuration. We can see the code here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;Project&lt;/span&gt; &lt;span class="na"&gt;Sdk = &lt;/span&gt;&lt;span class="s"&gt;"Microsoft.NET.Sdk.Web"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;PropertyGroup&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;TargetFramework&amp;gt;&lt;/span&gt; netcoreapp3.1 &lt;span class="nt"&gt;&amp;lt;/TargetFramework&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/PropertyGroup&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/Project&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We must modify the “TargetFramework” tag from netcoreapp3.1 to netcoreapp5.0, remaining as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;Project&lt;/span&gt; &lt;span class="na"&gt;Sdk = &lt;/span&gt;&lt;span class="s"&gt;"Microsoft.NET.Sdk.Web"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;PropertyGroup&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;TargetFramework&amp;gt;&lt;/span&gt; netcoreapp5.0 &lt;span class="nt"&gt;&amp;lt;/TargetFramework&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/PropertyGroup&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/Project&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It is quite simple, then we compile the application to see that it updates the packages and verify that everything is correct.&lt;/p&gt;

&lt;p&gt;It may be that some more updates are necessary if we are using some references such as EntityFramework or some additional third-party package. The recommended steps to update are as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Update "Microsoft.AspNetCore" to the latest version.&lt;/li&gt;
&lt;li&gt;Update "Microsoft.EntityFrameworCore" to the latest version.&lt;/li&gt;
&lt;li&gt;If we have some third party dependencies in our project, it is a good time to update those dependencies.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once these steps are ready, we are all ready to compile the projects. We will run each of the applications to verify that everything is correct.&lt;/p&gt;

&lt;p&gt;Finally, we have the unit testing project. Although the csproj may look a little different, we will follow the same steps that we used in the previous projects, we will modify the csproj. Before modifying it looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;Project&lt;/span&gt; &lt;span class="na"&gt;Sdk = &lt;/span&gt;&lt;span class="s"&gt;"Microsoft.NET.Sdk"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;PropertyGroup&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;TargetFramework&amp;gt;&lt;/span&gt; netcoreapp3.1 &lt;span class="nt"&gt;&amp;lt;/TargetFramework&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;IsPackable&amp;gt;&lt;/span&gt; false &lt;span class="nt"&gt;&amp;lt;/IsPackable&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/PropertyGroup&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;ItemGroup&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;PackageReference&lt;/span&gt; &lt;span class="na"&gt;Include = &lt;/span&gt;&lt;span class="s"&gt;"Microsoft.NET.Test.Sdk"&lt;/span&gt; &lt;span class="na"&gt;Version = &lt;/span&gt;&lt;span class="s"&gt;"16.5.0"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;PackageReference&lt;/span&gt; &lt;span class="na"&gt;Include = &lt;/span&gt;&lt;span class="s"&gt;"MSTest.TestAdapter"&lt;/span&gt; &lt;span class="na"&gt;Version = &lt;/span&gt;&lt;span class="s"&gt;"2.1.0"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;PackageReference&lt;/span&gt; &lt;span class="na"&gt;Include = &lt;/span&gt;&lt;span class="s"&gt;"MSTest.TestFramework"&lt;/span&gt; &lt;span class="na"&gt;Version = &lt;/span&gt;&lt;span class="s"&gt;"2.1.0"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;PackageReference&lt;/span&gt; &lt;span class="na"&gt;Include = &lt;/span&gt;&lt;span class="s"&gt;"coverlet.collector"&lt;/span&gt; &lt;span class="na"&gt;Version = &lt;/span&gt;&lt;span class="s"&gt;"1.2.0"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/ItemGroup&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/Project&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Modified as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;Project&lt;/span&gt; &lt;span class="na"&gt;Sdk = &lt;/span&gt;&lt;span class="s"&gt;"Microsoft.NET.Sdk"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;PropertyGroup&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;TargetFramework&amp;gt;&lt;/span&gt; net5.0 &lt;span class="nt"&gt;&amp;lt;/TargetFramework&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;IsPackable&amp;gt;&lt;/span&gt; false &lt;span class="nt"&gt;&amp;lt;/IsPackable&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/PropertyGroup&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;ItemGroup&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;PackageReference&lt;/span&gt; &lt;span class="na"&gt;Include = &lt;/span&gt;&lt;span class="s"&gt;"Microsoft.NET.Test.Sdk"&lt;/span&gt; &lt;span class="na"&gt;Version = &lt;/span&gt;&lt;span class="s"&gt;"16.6.1"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;PackageReference&lt;/span&gt; &lt;span class="na"&gt;Include = &lt;/span&gt;&lt;span class="s"&gt;"MSTest.TestAdapter"&lt;/span&gt; &lt;span class="na"&gt;Version = &lt;/span&gt;&lt;span class="s"&gt;"2.1.1"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;PackageReference&lt;/span&gt; &lt;span class="na"&gt;Include = &lt;/span&gt;&lt;span class="s"&gt;"MSTest.TestFramework"&lt;/span&gt; &lt;span class="na"&gt;Version = &lt;/span&gt;&lt;span class="s"&gt;"2.1.1"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;PackageReference&lt;/span&gt; &lt;span class="na"&gt;Include = &lt;/span&gt;&lt;span class="s"&gt;"coverlet.collector"&lt;/span&gt; &lt;span class="na"&gt;Version = &lt;/span&gt;&lt;span class="s"&gt;"1.3.0"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/ItemGroup&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/Project&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We will also need to update the Microsoft.Net.Test.SDK reference. Build and everything ready.&lt;/p&gt;

&lt;h2&gt;
  
  
  conclusion
&lt;/h2&gt;

&lt;p&gt;In this post we saw how in a fast and easy way we could transform our applications in .Net Core 3.1 to Net 5. Please, keep in mind that this is a preliminary version, in no way should we put these versions in productive environments. If it is recommended, test the migration of our current applications to the new versions of .Net. In future posts we will continue to see the news provided by the new versions of .net 5.&lt;/p&gt;

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