<?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>Forem: Jeffrey 'Aztec' Barnes</title>
    <description>The latest articles on Forem by Jeffrey 'Aztec' Barnes (@aztecconsulting).</description>
    <link>https://forem.com/aztecconsulting</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%2F742558%2Fb6ca60e0-6025-4e5c-bbad-9dc9d18191e7.jpg</url>
      <title>Forem: Jeffrey 'Aztec' Barnes</title>
      <link>https://forem.com/aztecconsulting</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/aztecconsulting"/>
    <language>en</language>
    <item>
      <title>I was banned...</title>
      <dc:creator>Jeffrey 'Aztec' Barnes</dc:creator>
      <pubDate>Tue, 06 Dec 2022 18:15:59 +0000</pubDate>
      <link>https://forem.com/aztecconsulting/i-was-banned-37i</link>
      <guid>https://forem.com/aztecconsulting/i-was-banned-37i</guid>
      <description>&lt;p&gt;Finding a home to post content and build a community is hard.&lt;/p&gt;

&lt;p&gt;I want to be more involved, but it's been hard to crawl out of the shell my former employer imposed because we weren't allowed to participate in many community events for fear of being seen as representing the company, rather than ourselves.  Now, free of those restrictions I have tried to grow more and participate more.  I heard about a community effort to link 50 blogs in a &lt;a href="https://csadvent.christmas/" rel="noopener noreferrer"&gt;C# Advent calendar&lt;/a&gt; to help share ideas and build the community so I applied to join in.  I got the Dec 4th spot and needed to write an article to link and participate.  &lt;/p&gt;

&lt;p&gt;I don't yet want to discuss the site I used for my participating entry, I've since moved the content &lt;a href="https://dev.to/aztecconsulting/invoking-legacy-javascript-from-blazor-webassembly-using-the-jsruntime-1df3"&gt;here&lt;/a&gt; but I wanted to document what happened.  &lt;/p&gt;

&lt;p&gt;On Sunday my post when live...&lt;/p&gt;

&lt;p&gt;On Monday I saw over 350 people had clicked through to see my article...&lt;/p&gt;

&lt;p&gt;On Tuesday I was banned...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Ftximjj9g39uf3hsm3cev.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ftximjj9g39uf3hsm3cev.jpg" alt="Image description" width="800" height="278"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Not my post was removed...&lt;br&gt;
not we need to discuss your post...&lt;br&gt;
but GTFO, you've been banned!&lt;/p&gt;

&lt;p&gt;I'm trying to backchannel an appeal, but this is an impressively hardcode response.  &lt;/p&gt;

&lt;p&gt;More to come...   &lt;/p&gt;

</description>
      <category>discuss</category>
      <category>career</category>
      <category>design</category>
    </item>
    <item>
      <title>Invoking Legacy JavaScript from Blazor WebAssembly using the JSRuntime</title>
      <dc:creator>Jeffrey 'Aztec' Barnes</dc:creator>
      <pubDate>Tue, 06 Dec 2022 17:02:54 +0000</pubDate>
      <link>https://forem.com/aztecconsulting/invoking-legacy-javascript-from-blazor-webassembly-using-the-jsruntime-1df3</link>
      <guid>https://forem.com/aztecconsulting/invoking-legacy-javascript-from-blazor-webassembly-using-the-jsruntime-1df3</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zUKp503H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t0et8veuilfnx7zc5mk2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zUKp503H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t0et8veuilfnx7zc5mk2.png" alt="Image description" width="300" height="300"&gt;&lt;/a&gt;This post, in addition to being the first in the JavaScript Runtime Interop Series is also a part of the &lt;a href="https://csadvent.christmas/"&gt;C# advent calendar 2022&lt;/a&gt;!  For other articles in this collection see C# Advent Calendar 2022 and have a very happy holidays.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sometimes the ocean can't be boiled...&lt;/strong&gt;&lt;br&gt;
One of the significant challenges of the Modernization of pre-existing or “Legacy” code is how difficult it is to get started because it is too complex or extensive. Refactoring and rewriting can be easier to tackle if you can integrate new code with the existing Legacy code and then migrate slowly and iteratively over time.&lt;/p&gt;

&lt;p&gt;For this article, I will start with a placeholder legacy JavaScript function that increments a supplied variable. This function serves only to be emblematic of some more extensive pre-existing Legacy code. I will then use the now infamous Blazor WASM demo project from Microsoft to illustrate how simply the razor counter component can invoke this sample function.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A word about code samples...&lt;/strong&gt;&lt;br&gt;
Often the most challenging part of exploring a programming topic is successfully setting up a demo to illustrate the points.  This sample JavaScript method is internationally grossly simple so it doesn't distract from it's implementation in the Blazor WebAssembly or WASM project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var arg = 0;


function legacyFunction(arg) {
  //Do stuff and 
  var nxt = arg + 1;
  console.log(arg + ' &amp;gt; ' + nxt );
  return nxt;
}

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;To explore the code in this article further, use this &lt;a href="https://github.com/AztecConsulting/JS-Invoke-Sample-Blazor-WebAssembly/releases/tag/v0.1.0"&gt;GitHub Repository&lt;/a&gt;! The above function can be found in the wwwroot folder in the legacysample.js file.  I have also provided in that folder a Sample.html file you can view with the browser of your choice to test the legacyFunction before exploring the code changes outlined below to understand how simple it is to invoke from the Blazor project.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The three steps to implementing the legacyFunction in the Blazor WASM demo project are:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1) Add the script src to the index.html file&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="legacysample.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Counter.Razor&lt;br&gt;
&lt;strong&gt;2) Inject the JSRuntime&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@inject IJSRuntime JSRuntime
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3) Modify the IncrementCount() method to be&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async void IncrementCount()
{
     //currentCount++;
     currentCount = await JSRuntime.InvokeAsync&amp;lt;int&amp;gt;("legacyFunction", currentCount);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this third and final step, you can see that currentCount uses the JSRuntime.InvokeAsync call with  to indicate the integer value returned by the JavaScript "legacyFunction" and the currentCount passed in for incrementing.  Note that the method was changed from private to async to allow the use of await so the JSRuntime can complete the InvokeAsync before returning control to the razor component.  &lt;/p&gt;

&lt;p&gt;With two supporting lines of code, this one call is all that is needed to invoke the sample function and can be all you need to start looking at the existing legacy javascript functions in your environment. We will explore this further next week when we drill into calling JavaScript methods without parameters, with multiple parameters, and having the legacy javascript method now invoke a C# method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The complete JavaScript Runtime Interop Series details:&lt;/strong&gt;&lt;br&gt;
12/4 (This week) – Invoking Legacy Javascript from Blazor WebAssembly using the JSRuntime&lt;br&gt;
12/11 (Next week) – Boomerang: Getting JavaScript to call a method in a Razor component&lt;br&gt;
12/18 – Integrating Blazor Server with Legacy JavaScript&lt;br&gt;
12/25 – JavaScript Runtime Interop in Dot Net 7.0&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>csharp</category>
    </item>
  </channel>
</rss>
