<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>dotnet-core on The Cloud Hub</title>
    <link>https://thecloudhub.com/category/dotnet-core/</link>
    <description>Recent content in dotnet-core on The Cloud Hub</description>
    <generator>Hugo -- 0.118.2</generator>
    <language>en-au</language>
    <lastBuildDate>Wed, 19 May 2021 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://thecloudhub.com/category/dotnet-core/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Get started with ABP framework on a Mac</title>
      <link>https://thecloudhub.com/2021/05/19/get-started-with-abp-framework-on-a-mac/</link>
      <pubDate>Wed, 19 May 2021 00:00:00 +0000</pubDate>
      <guid>https://thecloudhub.com/2021/05/19/get-started-with-abp-framework-on-a-mac/</guid>
      <description>In this post we&amp;rsquo;ll explore setting up the popular ABP framework for .NET Core on a Mac with a Postgres database.
In case you&amp;rsquo;re unaware, the ABP framework offers a number of out of the box features that can save a tremendous amount of time when starting a new web application. This includes a strong and opinionated architecture based on domain driven design, as well as multi-tenancy, theming and more.</description>
      <content:encoded><![CDATA[<p>In this post we&rsquo;ll explore setting up <a href="https://abp.io">the popular ABP framework</a> for .NET Core on a Mac with a Postgres database.</p>
<p>In case you&rsquo;re unaware, the ABP framework offers <a href="https://abp.io/features">a number of out of the box features</a> that can save a tremendous amount of time when starting a new web application. This includes a strong and opinionated architecture based on domain driven design, as well as multi-tenancy, theming and more.</p>
<p><strong>Getting your environment setup</strong></p>
<p>In this post we&rsquo;ll explore getting the basic ABP app with Blazor up and running on a Mac.</p>
<p>Firstly, if you haven&rsquo;t already done so <a href="https://dotnet.microsoft.com/download">download and install the latest .NET Core 5 SDK</a> from Microsoft&rsquo;s site.</p>
<p>Once installed you&rsquo;ll want to add the ABP command line tools. These will allow you to quickly scaffold projects and code. This can be installed by opening the <code>Terminal.app</code> on your Mac, and running this line of code:</p>
<p><code>dotnet tool install -g Volo.Abp.Cli</code></p>
<p>If you&rsquo;re running a more recent version of macOS - such as Big Sur - you&rsquo;ll now be using ZSH by default instead of Bash in the Terminal app.</p>
<p>Try running the command <code>abp --help</code>. <strong>If you get a message stating that <code>abp</code> is not a known command</strong>, you may need to create a <code>.zshrc</code> file to allow you to access the ABP CLI from ZSH. Within Terminal, run the following commands:</p>
<ol>
<li><code>vi ~/.zshrc</code></li>
<li>Paste <code>export PATH=$HOME/.dotnet/tools:$PATH</code></li>
<li>Run the command <code>:wq</code> to write the file to disk and quit</li>
<li>Restart <code>Terminal.app</code> and you should be able to run <code>abp --help</code></li>
</ol>
<p><strong>Creating the ABP project</strong></p>
<p>Create a new folder called <code>ToDoProject</code>. Navigate to it from within Terminal, and run the following command:</p>
<p><code>abp new TodoApp -u blazor-server</code></p>
<p>This creates a new .NET Core solution based on the ABP framework.</p>
<p>If you&rsquo;re on a Windows machine, normally you would now run the <code>DbMigrator</code> project to setup your database tables. However by default the ABP project uses LocalDB which is unavailable on the Mac.</p>
<p>As an alternative, you can use Postgres which IS available for the Mac.</p>
<p>The easiest way to install Postgres is to <a href="https://postgresapp.com">download and install the Postgres.app</a> for free. Once launched, you can then install an <a href="https://eggerapps.at/postico/">app like Postico</a> or use the command line to create a new Postgres database.</p>
<p>If using Postico, connect to your <code>localhost</code> Postgres server either via the server settings within Postico, or by double clicking the server from within the Postgres app.</p>
<p>Then, navigate to your server within Postico by clicking <code>localhost</code> in the top navigation bar. Click &ldquo;+ Database&rdquo; and add a new database called <code>todoapp</code>.</p>
<!-- raw HTML omitted -->
<p><img loading="lazy" src="images/Screen-Shot-2021-05-19-at-10.28.56-am-1024x773.png" alt=""  />
</p>
<!-- raw HTML omitted -->
<p>Click these buttons to setup the new database</p>
<!-- raw HTML omitted -->
<!-- raw HTML omitted -->
<p>Now we need to make some changes to the sample ABP app. Open the <code>TodoApp.sln</code> in your editor of choice (for the Mac this can include Visual Studio for Mac, Visual Studio Code or Rider).</p>
<p>Next replace the SQL package with Postgres. Add the <code>[Volo.Abp.EntityFrameworkCore.PostgreSql](https://www.nuget.org/packages/Volo.Abp.EntityFrameworkCore.PostgreSql)</code> package via Nuget, and remove <a href="https://www.nuget.org/packages/Volo.Abp.EntityFrameworkCore.SqlServer"><code>Volo.Abp.EntityFrameworkCore.SqlServer</code></a>.</p>
<p>Then follow the <a href="https://docs.abp.io/en/abp/latest/Entity-Framework-Core-PostgreSQL">rest of the steps in this article</a> to update your project files to use the new Postgres package.</p>
<p>Lastly you&rsquo;ll need to update the connection strings for your database.</p>
<p>Open <code>src/TodoApp.Blazor/appsettings.json</code> and update the following line to connect to your Postgres database, updating `<!-- raw HTML omitted -->` with your database username (for a Mac this is normally your Mac login username, unless you&rsquo;ve specified a new user):</p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>&#34;ConnectionStrings&#34;: {
</span></span><span style="display:flex;"><span>  &#34;Default&#34;: &#34;Server=localhost;Port=5432;Database=todoapp;User Id=&lt;your username&gt;/;Password=&#34;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></td></tr></table>
</div>
</div><p>Similarly, open <code>src/TodoApp.DbMigrator/appsettings.json</code> and update the same connection string:</p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2
</span><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>&#34;ConnectionStrings&#34;: {
</span></span><span style="display:flex;"><span>  &#34;Default&#34;: &#34;Server=localhost;Port=5432;Database=todoapp;User Id=&lt;your username&gt;/;Password=&#34;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></td></tr></table>
</div>
</div><p>From within your IDE you should now be able to build and run your app, or from the Terminal app run <code>dotnet run</code> to see your first ABP web app running!</p>
]]></content:encoded>
    </item>
    <item>
      <title>Understanding the Newtonsoft JSON Token</title>
      <link>https://thecloudhub.com/2019/03/26/understanding-the-newtonsoft-json-token/</link>
      <pubDate>Tue, 26 Mar 2019 00:00:00 +0000</pubDate>
      <guid>https://thecloudhub.com/2019/03/26/understanding-the-newtonsoft-json-token/</guid>
      <description>One of the concepts worth understanding when writing a custom converter for the popular Newtonsoft JSON framework is that of the JsonToken, and the role it plays in helping you understand the JSON being read.
The JsonToken documentation for Newtonsoft is relatively sparse - in part because once you understand it, it&amp;rsquo;s a relatively simple concept.
Effectively, when you&amp;rsquo;re attempting to deserialise a string representation of a JSON object, such as in the ReadJson method of a custom JsonConverter, the JsonToken represents each element or component (token if you prefer) of the object as it&amp;rsquo;s deserialised.</description>
      <content:encoded><![CDATA[<p>One of the concepts worth understanding when writing a custom converter for <a href="https://github.com/JamesNK/Newtonsoft.Json">the popular Newtonsoft JSON framework</a> is that of the <code>JsonToken</code>, and the role it plays in helping you understand the JSON being read.</p>
<p>The <code>JsonToken</code> documentation for Newtonsoft is relatively sparse - in part because once you understand it, it&rsquo;s a relatively simple concept.</p>
<p>Effectively, when you&rsquo;re attempting to deserialise a string representation of a JSON object, such as in the <code>ReadJson</code> method of a custom <code>JsonConverter</code>, the <code>JsonToken</code> represents each element or component (token if you prefer) of the object as it&rsquo;s deserialised.</p>
<p>So for example, if you have the following JSON and implement the <code>ReadJson</code> method:</p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>{	&#34;message&#34;: &#34;Hello world&#34;}
</span></span></code></pre></td></tr></table>
</div>
</div><p>When you first get the <code>reader.TokenType</code> value you&rsquo;ll get <code>JsonToken.StartObject</code> back - this represents that the <code>{</code> implies that you&rsquo;re first reading an object element.</p>
<p>If you were to then call <code>reader.ReadAsync()</code>, and then subsequently request the <code>reader.TokenType</code> again now you&rsquo;ll get <code>JsonToken.PropertyName</code> in response. This is because the next element within the JSON string is the property name (in other words, the &ldquo;message&rdquo; key).</p>
<p>Lastly, if you were to call <code>reader.ReadAsync()</code> for a third time, you&rsquo;d get back <code>JsonToken.String</code> as the value of the property is a string (&ldquo;Hello world&rdquo;). NewtonSoft will try and parse the value into a representation of a type - so for example, if that was a number, you&rsquo;d get back <code>Integer</code> of <code>Float</code>, and if it was a Boolean you&rsquo;d get back <code>Boolean</code>.</p>
]]></content:encoded>
    </item>
    <item>
      <title>Throw or throw ex in C#?</title>
      <link>https://thecloudhub.com/2019/03/20/throw-or-throw-ex-in-c/</link>
      <pubDate>Wed, 20 Mar 2019 00:00:00 +0000</pubDate>
      <guid>https://thecloudhub.com/2019/03/20/throw-or-throw-ex-in-c/</guid>
      <description>One common question that comes up when working with exceptions in C#, is whether or not you should re-throw an existing exception by specifying the exception, or just using the throw keyword where an error occurs.
In most cases, it&amp;rsquo;s best to rethrow the existing exception, such as below:
1 try {	var networkResponse = await someNetworkService.GetAllPosts();}catch (Exception ex){	Log.Warning(&amp;#34;An exception occurred&amp;#34;, ex);	//Now lets rethrow this same exception	throw;} By doing so, you ensure that the original stack trace for the exception is maintained.</description>
      <content:encoded><![CDATA[<p>One <a href="https://stackoverflow.com/questions/178456/what-is-the-proper-way-to-re-throw-an-exception-in-c">common question</a> that comes up when working with exceptions in C#, is whether or not you should re-throw an existing exception by specifying the exception, or just using the <code>throw</code> keyword where an error occurs.</p>
<p>In most cases, it&rsquo;s best to rethrow the existing exception, such as below:</p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>try {	var networkResponse = await someNetworkService.GetAllPosts();}catch (Exception ex){	Log.Warning(&#34;An exception occurred&#34;, ex);	//Now lets rethrow this same exception	throw;}
</span></span></code></pre></td></tr></table>
</div>
</div><p>By doing so, you ensure that the original stack trace for the exception is maintained. If you were to instead do something like this:</p>
<div class="highlight"><div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">
<table style="border-spacing:0;padding:0;margin:0;border:0;"><tr><td style="vertical-align:top;padding:0;margin:0;border:0;">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code><span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1
</span></code></pre></td>
<td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-fallback" data-lang="fallback"><span style="display:flex;"><span>try {	var networkResponse = await someNetworkService.GetAllPosts();}catch (Exception ex){	Log.Warning(&#34;An exception occurred&#34;, ex);	//Now lets throw ex again	throw ex;}
</span></span></code></pre></td></tr></table>
</div>
</div><p>You&rsquo;ll now see that the exception stack trace only goes as far back as this line of code - not to the raw source of the exception.</p>
<p>In short, that&rsquo;s why its normally better to just throw again, instead of specifying the exception when re-throwing.</p>
]]></content:encoded>
    </item>
  </channel>
</rss>
