<?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: Omar Zouaid</title>
    <description>The latest articles on DEV Community by Omar Zouaid (@zwdor20).</description>
    <link>https://dev.to/zwdor20</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%2F442216%2F3d118a48-e75a-493f-8e18-3341cbceb437.jpeg</url>
      <title>DEV Community: Omar Zouaid</title>
      <link>https://dev.to/zwdor20</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zwdor20"/>
    <language>en</language>
    <item>
      <title>Background Jobs and .NET Hosted Services </title>
      <dc:creator>Omar Zouaid</dc:creator>
      <pubDate>Sat, 16 Oct 2021 20:50:45 +0000</pubDate>
      <link>https://dev.to/zwdor20/background-jobs-and-net-hosted-services-1pl8</link>
      <guid>https://dev.to/zwdor20/background-jobs-and-net-hosted-services-1pl8</guid>
      <description>&lt;h1&gt;
  
  
  Introduction :
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Background jobs are an alternative to synchronous processing, it's an approach to lighten the main thread and offload tasks to a secondary thread. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To explain the concept let's imagine a web application dedicated to video processing which's obviously a heavy task, so the main application get the request from a third party application or the browser, then it delegates the task to a background job, but the main thread can't wait for the response cause it will take a considerable amount of time to finish the task from the worker job, so to free the main application thread the communication is established asynchronously by creating a message queue bridge between the application and background job, so the main application place the task in queue and then move on to get more processing request, and the background job can treat the requests at it's own speed. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The main criteria to decide whether a task can be executed in background or not is by asking yourself if the task need the interaction with user while in execution or the user is obliged to wait for the job result, if the answer is yes so the backgound job implementation can be inapropriate in this case. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Background jobs triggers types :
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;There's two type of background job triggers : &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Event driven trigger :&lt;/strong&gt;  the background job is launched in parallel with the main application when a start event is raised. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Schedule driven trigger :&lt;/strong&gt; in this approach the task is invoked periodically based on a timer.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Dotnet Core Hosted Services :
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;In the .Net core platform we can build background jobs by implementing &lt;strong&gt;IHostedService&lt;/strong&gt; interface. &lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In the code above The &lt;strong&gt;StartAsync&lt;/strong&gt; method is triggered when our service kicks off, it's a setup method where we configure and launch the background job to do the work. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;On the other side &lt;strong&gt;StopAsync&lt;/strong&gt; is called when we stop our background service so we can do whatever we need to dispose used resources.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The .Net hosted services are tied to the hosted service, that means&lt;br&gt;
they are deployed with the main application within the same host&lt;br&gt;
infrastructure.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Dotnet hosted service example :
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;We will create a simple hosted service which will simply print Hello World every 10 seconds, and inject it as hosted service to  ASP .Net Api application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.  Implemente IHostedService&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;In the code above we decalred an instance of a Timer object which will be configured to execute a callback method periodically.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;a href="https://docs.microsoft.com/en-us/dotnet/api/system.timers.timer?view=net-5.0"&gt;Timer&lt;/a&gt; is an object  that raises an event after a number of milliseconds elapsed. You can configure the Timer object to raise the event just once or repeatedly. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;In the StartAsync method we configured the timer to execute  &lt;code&gt;PrintHelloWord()&lt;/code&gt; periodically after each 10 seconds, in the other side StopAsync method is used  to stop the timer callbacks when the job is turned off.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Configure API by adding our hosted service&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We need to add our hosted service to &lt;code&gt;ConfigureServices&lt;/code&gt; method in startup.cs :&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;And we are done if we start our api now MyHostedService will kicks off, check the Debug output to see the result 😉.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Hosted services limitation :
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;The hosted services are tied to the main host web application, so when we scale horizontally our web application it involves the creation of new job instance. So we should be aware of that and avoid using hosted services in cases where jobs will compete with each others and lead to unwanted behaviours. &lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Why we can't use Out or Ref parameters with Asynchronous method in .Net Core ?</title>
      <dc:creator>Omar Zouaid</dc:creator>
      <pubDate>Tue, 14 Sep 2021 22:35:09 +0000</pubDate>
      <link>https://dev.to/zwdor20/why-we-can-t-use-out-or-ref-parameters-with-asynchronous-method-in-net-core-3nid</link>
      <guid>https://dev.to/zwdor20/why-we-can-t-use-out-or-ref-parameters-with-asynchronous-method-in-net-core-3nid</guid>
      <description>&lt;p&gt;Do you ever find yourself in a situation where you need to return multiple values from a method?&lt;/p&gt;

&lt;p&gt;The standard approach is whether to create a class object which hold all parameters as properties and use it as a return type, but this approach can be cumbersome and you will find yourself creating meaningless objects. Better approach is using dotnet &lt;strong&gt;Out&lt;/strong&gt; parameters.&lt;/p&gt;

&lt;p&gt;But it's not always possible to use out parameter, because in my case I was dealing with asynchronous method, and what I was unware of is the fact that the dotnet compiler forbid  the use of out parameter inside asynchronous methods :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvl556ytjryt9eznayfxi.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvl556ytjryt9eznayfxi.jpg" alt="Error Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that pushed me to wonder why asynchronous method are designed on that way? and how can I figure out a  workaround to this limitation? &lt;/p&gt;

&lt;h2&gt;
  
  
  Internal State machine (IAsyncStateMachine)
&lt;/h2&gt;

&lt;p&gt;So to understand the problem we need to know how  compiler deals with Asynchronous code. &lt;/p&gt;

&lt;p&gt;So Every time we have an asynchronous method, the compiler actually turns the method into an &lt;strong&gt;internal state object&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Internal state class is designed in build time and instantiated in run time by the CLR, it implements the &lt;strong&gt;IAsyncStateMachine&lt;/strong&gt; interface. This class is responsible for keeping the state of your method during the life cycle of the asynchronous operation, it encapsulates all the variables of your method as fields, splits your code into sections that are executed as the state machine transitions between states, so that the thread can leave the method and when it comes back the state is intact. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Common Language Runtime (CLR) is .NET run-time environment that runs the code and provides services that make the development process easier.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So let's create a class with asnychronous method and see how generated class will looks like. In the code below &lt;strong&gt;ExampleAsyncMethod&lt;/strong&gt; is our async method :&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ExampleAsyncMethod&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;ExampleAsyncMethod&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="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;variable&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;Empty&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;CallApiAsync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;Debug&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;"First Await"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;variable&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"new value"&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;CallApiAsync&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;Debug&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;"Second Await"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;variable&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"second new value"&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;CallApiAsync&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;//Api call&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;When we try to build this code, .Net compiler will generate an internal state class :&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;CompilerGenerated&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;sealed&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ExampleAsyncMethod&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;d_0&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IAsyncStateMachine&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//fields&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="n"&gt;_state&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;ExampleAsync&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ExampleAsync&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="n"&gt;_this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;AsyncTaskMethodBuilder&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;t_builder&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;TaskAwaiter&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;u_1&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;int&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;variable&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="m"&gt;5_1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// other fields&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Examining the generated class, we notice that our internal variable “variable”  is now a class field.&lt;/p&gt;

&lt;p&gt;We can also see other class fields used internally to maintain the state of the IAsyncStateMachine, one of them is &lt;strong&gt;MoveNext()&lt;/strong&gt; which is a method with a big switch block. So basically MoveNext() we'll be called whenever there's state transitions (task completed) in our asynchronous code. The code below is a part of the generated MoveNext() method :&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;void&lt;/span&gt; &lt;span class="nf"&gt;MoveNext&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="n"&gt;_state&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;TaskAwaiter&lt;/span&gt; &lt;span class="n"&gt;awaiter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;ExampleAsync&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ExampleAsync&lt;/span&gt;&lt;span class="p"&gt;.&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;ExampleAsyncMethod&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;d_0d_&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;goto&lt;/span&gt; &lt;span class="n"&gt;Label_00D6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;myVariable&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;5_1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="n"&gt;awaiter&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="n"&gt;_this_this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;CallApiAsync&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;GetAwaiter&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;awaiter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_IsCompleted&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
                &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;goto&lt;/span&gt; &lt;span class="n"&gt;Label_007E&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="n"&gt;__state&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;u__1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;awaiter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="n"&gt;d__&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&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;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;awiter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetResult&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;Debug&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;"First Await"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;variable&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;5_1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;TaskAwaiter&lt;/span&gt; &lt;span class="n"&gt;awaiter2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="n"&gt;_this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;CallApiAsync&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;GetAwaiter&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;awaiter2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_IsCompleted&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;goto&lt;/span&gt; &lt;span class="n"&gt;Label_00F2&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;this&lt;/span&gt;&lt;span class="p"&gt;.&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="n"&gt;__state&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;u__1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;awaiter2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;d__&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&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;span class="n"&gt;Label_00D6&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;awaiter2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;u_1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;u_1&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;TaskAwaiter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&amp;lt;&amp;gt;&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="n"&gt;_state&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Label_00F2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="c1"&gt;/// other code labels&lt;/span&gt;

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


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Back to our question
&lt;/h2&gt;

&lt;p&gt;So we have seen how the compiler turn async method to state class, now let's go back on topic, &lt;strong&gt;Why we can't declare Out or Ref parameters in asynchronous method?&lt;/strong&gt; So according to Lucian Wischik a member of the C# language design team, this problem is because of some limitation of CLR. Unfortunately in internal state class we can't store  the address of an out or ref parameter, basically CLR has no safe way to deal with the address of an object. As consequence the compiler forbid the use of Ref and out parameters in asynchronous method.&lt;/p&gt;
&lt;h2&gt;
  
  
  Workaround
&lt;/h2&gt;

&lt;p&gt;So till now I have explained why the CLR forbid the use of ref and out parameter in async method. But what can we do to overpass this limitation? My solution to this problem was using &lt;strong&gt;Tuples&lt;/strong&gt;. Since the version 7, C# brought to table the notion of Tuples, an easy way to store multiple items in a single variable. So with tuples we can  return mutuple values from method with no need to use out parameter.&lt;/p&gt;

&lt;p&gt;So instead of using code like this (which is forbidden by the compiler): &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;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="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;GetDataTaskAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;parameters&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//...&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;someIntValueTask&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;I  switch to Tuples  :&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;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;Tuple&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;GetDataTaskAsync&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//...&lt;/span&gt;
    &lt;span class="n"&gt;Tuple&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&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="nf"&gt;TaskReturnTuple&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;result&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;In my case I just returned two values, but with Tuples we can return as many values as we want with no constraint.&lt;/p&gt;

&lt;p&gt;I hope this article helped you to understand why we can't declare out and ref parameters in asynchronous method, if you have any question or comment please feel free to express your thoughts 😉.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>Blazor in nutshell</title>
      <dc:creator>Omar Zouaid</dc:creator>
      <pubDate>Fri, 08 Jan 2021 12:21:12 +0000</pubDate>
      <link>https://dev.to/zwdor20/blazor-in-nutshell-5g75</link>
      <guid>https://dev.to/zwdor20/blazor-in-nutshell-5g75</guid>
      <description>&lt;h1&gt;
  
  
  What's BLAZOR?
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Blazor is a new framework from Microsoft designed to create SPAs (single page applications) UI, Blazor apps are composed of reusable web UI components implemented using C#, HTML, and CSS ( Oops can't see Js 🤔 ). Blazor is &lt;a href="https://www.google.com" rel="noopener noreferrer"&gt;an open source project from microsoft &lt;/a&gt; and was initialy released 2 years ago (January 2018). By releasing Blazor WebAssambly version in May 2020, the community has become much larger and people are getting more intrested.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The name of Blazor is a combination of two words, first one is  &lt;strong&gt;Browser&lt;/strong&gt; and  second one is &lt;strong&gt;Razor&lt;/strong&gt;. Razor is a template markup syntax, used to create dynamic web pages based on the C# programming language, that enables the programmer to use an HTML construction workflow in ASP .NET MVC Framework. The implication behind the name is instead of having to execute code in the server( like we used to do it with Razor pages apps) inorder to render HTML, Blazor is capable of doing the same thing but in the browser (without relying in one single line of JS).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  How does Blazor Work ?
&lt;/h1&gt;

&lt;p&gt;To understand how Blazor works we need to diffrentiate between two version of Blazor, Blazor server version and Blazor WebAssembly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Blazor Server :&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Blazor Server uses a standard ASP .NET Core application to run, and  Within this application we can add server-side functionality such as DataBase communication, Authentification service...
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmgssvd1boie722z0i630.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Blazor server rely on &lt;a href="https://dotnet.microsoft.com/apps/aspnet/signalr" rel="noopener noreferrer"&gt;SignalR&lt;/a&gt; which's is basically a library for ASP .Net  used to add real-time web functionality to their applications based in the famous WebSocket protocol.&lt;br&gt;
    - Blazor server app handles a copy of the DOM for every connected client, and each UI transaction trigger an event that will transported to the server using SignalR WebSocket than the server will update the DOM or apply BackEnd functionality.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note : Latency still a major problem of Blazor Server. Client is tied to the server with a webSocket protocol which bring into question the scalability of Blazor server App (virtual DOM management for each client need importante CPU, Storage and Network resources) &lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Blazor Wasm :&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Blazor Wasm or Blazor WebAssembly  is another  supported way to host your Blazor  pages in client-side  and relying on WebAssembly Host system, which is an open web standards supported in all modern web browsers.
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flbgrrbra3ygmnd20h385.png" alt="Alt Text"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;WebAssembly is an open standard for running binary programs in the browser with near-native performance. WebAssembly can go where JavaScript has not showed great performance (3D animation, media editing, high games ending ...).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note : WebAssembly is not a programming language however it's a compile-target code (similar to .Net IL or java byteCode), so the idea behind it is simple, you write your code in any language you are familiar with and than your code is giong to be transpiled to webAssembly code, and finally executed in the browser.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frvzykr4c72ehm9xyh8ev.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frvzykr4c72ehm9xyh8ev.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;So what happens when we run Blazor wasm app ? Technically when Blazor Wasm app is going to be launched, it will build and shipped like a DLL files (packaging format for .Net apps) + css files + Index.html, and than sent to the browser with a compact run time environement (2Mo) called Mono interpreter which is a lightweight dotnet Run-Time. Mono interpreter is responsable to transform DLL package to webAssembly code, this processus is only executed in the first transaction between the application and the Browser, after the initial HTTP request the application will run completly in the browser.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Note : Microsoft is working to reduce the size of the Mono interpreter by implementing &lt;a href="https://visualstudiomagazine.com/articles/2020/05/22/blazor-future.aspx" rel="noopener noreferrer"&gt;AOT compilation&lt;/a&gt; which will impact the performance of Blazor Wasm app.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Blazor wasm app can be deployed as static files, so it can run independantly in the browser in offline state.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Where can I learn it ?
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;So here I will share with you some free ressources (you can also find great courses in Udemy, Pluralsight ...) that I found useful for learning Blazor :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/aspnet/core/blazor/?view=aspnetcore-5.0" rel="noopener noreferrer"&gt;Official Microsoft Blazor Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blazor-university.com/" rel="noopener noreferrer"&gt;Blazor university : Blog for learning the main concepts behind Blazor&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=MR0uc3pJYf4&amp;amp;list=PL4WEkbdagHIR0RBe_P4bai64UDqZEbQap&amp;amp;index=3&amp;amp;ab_channel=CuriousDrive]" rel="noopener noreferrer"&gt;Blazor Tutorial : Build your first Blazor App &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=KlngrOF6RPw&amp;amp;list=PLdo4fOcmZ0oWlP1Qpzg7Dwzxr298ewdUQ&amp;amp;ab_channel=dotNET" rel="noopener noreferrer"&gt;Official Dotnet channel in youtube&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;That's all, I hope this article helped you somehow to understand the principal concepts behind Blazor, thank you 😉&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

</description>
      <category>dotnet</category>
      <category>blazor</category>
      <category>webassembly</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
