<?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: gaotter</title>
    <description>The latest articles on DEV Community by gaotter (@gaotter).</description>
    <link>https://dev.to/gaotter</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%2F318742%2F1b5ffa10-09e6-4d41-a1f0-27b97697a43f.jpg</url>
      <title>DEV Community: gaotter</title>
      <link>https://dev.to/gaotter</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gaotter"/>
    <language>en</language>
    <item>
      <title>Change a .Net Console application into an web application</title>
      <dc:creator>gaotter</dc:creator>
      <pubDate>Fri, 03 Jan 2025 13:11:29 +0000</pubDate>
      <link>https://dev.to/gaotter/change-a-net-console-application-into-an-web-application-2i2l</link>
      <guid>https://dev.to/gaotter/change-a-net-console-application-into-an-web-application-2i2l</guid>
      <description>&lt;p&gt;I was experimenting with a small .NET console application and decided to convert it into a web application. Initially, I thought I needed to download a NuGet package, but it turns out that’s no longer necessary. &lt;/p&gt;

&lt;p&gt;Instead, I discovered that I just needed to change the Sdk attribute in the project file from &lt;code&gt;&amp;lt;Project Sdk="Microsoft.NET.Sdk"&amp;gt;&lt;/code&gt; to &lt;code&gt;&amp;lt;Project Sdk="Microsoft.NET.Sdk.Web"&amp;gt;&lt;/code&gt; and remove the &lt;code&gt;&amp;lt;OutputType&amp;gt;Exe&amp;lt;/OutputType&amp;gt;&lt;/code&gt;. It was a simple change, but definitely good to know!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>dotnetcore</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>Classes variables early thoughts 🤔</title>
      <dc:creator>gaotter</dc:creator>
      <pubDate>Tue, 21 May 2024 08:28:00 +0000</pubDate>
      <link>https://dev.to/gaotter/classes-variables-early-thoughts-5h9l</link>
      <guid>https://dev.to/gaotter/classes-variables-early-thoughts-5h9l</guid>
      <description>&lt;p&gt;I'm just starting to learn Python, coming from languages like C# and JavaScript/TypeScript. I'm going thought this &lt;a href="https://docs.python.org/3/tutorial/index.html"&gt;https://docs.python.org/3/tutorial/index.html&lt;/a&gt; tutorials. I gotten to the classes section. One thing I kinda felt can be a little risky thing to use is the class variables. In that you can loose the control of state of the objects in you code, when using them.&lt;/p&gt;

&lt;p&gt;Here is the Dog example that is in the Python tutorial.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Dog:
    kind = 'canine'

    def __init__(self, name):
        self.name = name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I make to instances of the dog class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fido = Dog('Fido')
buddy = Dog('Buddy')

print(fido.kind) // prints canine
print(buddy.kind) // prints canine
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All is fine, I know what kind is going to be. In the tutorial it states that the class variables a shared between instances of the class. Like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Dog.kind = 'dog'

print(fido.kind) // prints dog
print(buddy.kind) // prints dog
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if i set &lt;code&gt;kind&lt;/code&gt; on one of the instances, the instance 'looses' shared &lt;code&gt;kind&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fido.kind = 'super dog'
Dog.kind = 'dog'

print(fido.kind) // prints super dog
print(buddy.kind) // prints dog
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In a large code base,  one can easily loose track of the &lt;code&gt;kind&lt;/code&gt; variable. One can't really trust the variable, as it can either be changed by the shared class variable, or some code has set the instance variable. &lt;/p&gt;

&lt;p&gt;One can marking as private using &lt;code&gt;_kind&lt;/code&gt; to tell to other developers that is should thought of as private, but still one can easily miss use it. Unsure if I ever would want to us it an public way. But still new to Pyhon so still learing.  &lt;/p&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>Send initial data to a component in Angular Universal</title>
      <dc:creator>gaotter</dc:creator>
      <pubDate>Sun, 26 Mar 2023 11:19:50 +0000</pubDate>
      <link>https://dev.to/gaotter/send-initial-data-to-a-component-in-angular-universal-25c7</link>
      <guid>https://dev.to/gaotter/send-initial-data-to-a-component-in-angular-universal-25c7</guid>
      <description>&lt;h2&gt;
  
  
  Steps
&lt;/h2&gt;

&lt;p&gt;Setting up a simple test angular universal, I wanted to try to send some initial data to the components, and have it server render all the data.  To test it I used postman as it does not run any JavaScript&lt;/p&gt;

&lt;p&gt;To get up and running call &lt;code&gt;ng new univesaltest1&lt;/code&gt; and to enable universal called &lt;code&gt;ng add @nguniversal/express-engine&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To have some components to test with run &lt;code&gt;ng generate component test --module app.module.ts&lt;/code&gt; and &lt;code&gt;ng generate component test2 --module app.module.ts&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then making a simple message model and an injection token&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { InjectionToken } from "@angular/core";

export class MessageModel {
  constructor(
    public message: string
  ) { }
}


// injecton token
export const MESSAGE = new InjectionToken&amp;lt;MessageModel&amp;gt;('message');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the server.ts file set up the initial model and some query string code. Then provide the model using the MESSAGE injection token.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  // All regular routes use the Universal engine
  server.get('*', (req, res) =&amp;gt; {
    const message:string = req.query['message']?.toString() ?? 'hello from server.ts';
    const messageModel = new MessageModel(message);

    res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }, {provide: MESSAGE, useValue: messageModel}] });
  });

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

&lt;/div&gt;



&lt;p&gt;In the test.component.ts inject platform id and on the server run code inject the message model using the MESSAGE injection token. I need the transfer state as the rendering on the client will set the message, overriding the server rendered message. If I was to disable JavaScript in the browser, I do not need the transfer state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { isPlatformServer } from '@angular/common';
import { Component, inject, Inject, PLATFORM_ID } from '@angular/core';
import { makeStateKey, StateKey, TransferState } from '@angular/platform-browser';
import { MESSAGE, MessageModel } from 'src/models/message.model';

@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.css']
})
export class TestComponent {
  // transfare state key
  private messageStateKey:StateKey&amp;lt;MessageModel&amp;gt; = makeStateKey&amp;lt;MessageModel&amp;gt;('message');

  public message: MessageModel = this.state.get(this.messageStateKey, new MessageModel('hello from test.component.ts'));

  constructor(@Inject(PLATFORM_ID) private platformId:object,  private state:TransferState)
  {
    // check if we are on the server
    if(isPlatformServer(this.platformId)) {
      const serverMessage = inject(MESSAGE);
      this.message = serverMessage;
      // set the message in the state
      this.state.set(this.messageStateKey, serverMessage);
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make it more interesting input the message model into test2 component using this code &lt;/p&gt;

&lt;p&gt;test.component.html&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;div &amp;gt;
  &amp;lt;app-test2 [message]="message"&amp;gt;&amp;lt;/app-test2&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;test2.component.ts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component, Input } from '@angular/core';
import { MessageModel } from 'src/models/message.model';

@Component({
  selector: 'app-test2',
  templateUrl: './test2.component.html',
  styleUrls: ['./test2.component.css']
})
export class Test2Component {
     @Input() message: MessageModel = new MessageModel('hello from test2.component.ts');
}

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

&lt;/div&gt;



&lt;p&gt;test2.component.html&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;p&amp;gt;{{message.message}}&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we are all set. To test the code start the universal version using &lt;code&gt;npm run dev:ssr&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Calling it postman and all the html with the model data is rendered on the server&lt;/p&gt;

&lt;p&gt;Request: localhost:4200?message="Hello from query string"&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;body&amp;gt;
    &amp;lt;app-root _nghost-sc3="" ng-version="15.2.4" ng-server-context="ssr"&amp;gt;
        &amp;lt;app-test _ngcontent-sc3="" _nghost-sc2=""&amp;gt;
            &amp;lt;div _ngcontent-sc2=""&amp;gt;
                &amp;lt;app-test2 _ngcontent-sc2="" _nghost-sc1="" ng-reflect-message="[object Object]"&amp;gt;
                    &amp;lt;p _ngcontent-sc1=""&amp;gt;"Hello from query string"&amp;lt;/p&amp;gt;
                &amp;lt;/app-test2&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/app-test&amp;gt;
    &amp;lt;/app-root&amp;gt;
    &amp;lt;script src="runtime.js" type="module"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script src="polyfills.js" type="module"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script src="vendor.js" type="module"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script src="main.js" type="module"&amp;gt;&amp;lt;/script&amp;gt;

    &amp;lt;script id="serverApp-state" type="application/json"&amp;gt;
        {&amp;amp;q;message&amp;amp;q;:{&amp;amp;q;message&amp;amp;q;:&amp;amp;q;\&amp;amp;q;Hello from query string\&amp;amp;q;&amp;amp;q;}}
    &amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Road
&lt;/h2&gt;

&lt;p&gt;I did some mistakes when trying to get this to work. In the first try I tried to inject the Message model using the @Inject in the constructor. The problem then is that the client part of the application is not able to make sense of the provided message model. &lt;/p&gt;

&lt;p&gt;I go the error&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;R3InjectorError(AppModule)[InjectionToken message -&amp;gt; InjectionToken message -&amp;gt; InjectionToken message]: 
  NullInjectorError: No provider for InjectionToken message!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Trying to provide the injection token in the app.module.ts using&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  providers: [{provide: MESSAGE, useValue: null }],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; @Inject(MESSAGE) private messageModel:MessageModel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The client app model overrides the provided message with null. So you could se the message blink before the client set it to null. &lt;/p&gt;

&lt;p&gt;The solution was to not try to mix the server injected code with the clint part of the code.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>angular</category>
    </item>
    <item>
      <title> NTFS_FILE_SYSTEM error Windows 10 😱, and I guess I know some stuff I don't know.</title>
      <dc:creator>gaotter</dc:creator>
      <pubDate>Fri, 04 Dec 2020 13:14:47 +0000</pubDate>
      <link>https://dev.to/gaotter/ntfsfilesystem-error-windows-10-and-i-guess-i-know-some-stuff-i-don-t-know-de9</link>
      <guid>https://dev.to/gaotter/ntfsfilesystem-error-windows-10-and-i-guess-i-know-some-stuff-i-don-t-know-de9</guid>
      <description>&lt;p&gt;This weekend I left my laptop on without power supply a hp spectre, while going out for a walk with my kids. When I got home the laptop had been turn off (in a bad way). When trying to turn it on I got a blue screen with the error code NTFS_FILE_SYSTEM. 😱 No!! What to do, my warranty just when out. Is the hard drive broken. It's a ploy to get me to buy a new computer. All the crazy stuff went through my head, as I panicked. &lt;/p&gt;

&lt;p&gt;After cooling down (after some hours) I found that I could reach the command line tools, and the computer had a x drive (partitioned), then something back in my head said "chkdsk" try "chkdsk". But I ignored that, and did all the other stuff. Try to run reset to a old restore point in Windows, and all the other stuff I don't know what does. Event thought about formatting the disk, and reinstall everything. But the voice in my head still said "chkdsk, try chkdsk " . Ok!! lets try chkdsk then. Run chkdsk C: /f from the x drive, some messages about fixing some bad stuff. Waiting.... reboot, worked. &lt;/p&gt;

&lt;p&gt;So what did I learn. Don't leave the computer on (without power saving on) un plugged. And listen to you inner voice, event it's telling you some old stuff you don't know you know.&lt;/p&gt;

</description>
      <category>windows</category>
    </item>
    <item>
      <title>How not understanding stuff can make you learn other stuff and waste time.</title>
      <dc:creator>gaotter</dc:creator>
      <pubDate>Wed, 18 Nov 2020 14:55:47 +0000</pubDate>
      <link>https://dev.to/gaotter/how-not-understanding-stuff-can-make-you-learn-other-stuff-and-waste-time-51oc</link>
      <guid>https://dev.to/gaotter/how-not-understanding-stuff-can-make-you-learn-other-stuff-and-waste-time-51oc</guid>
      <description>&lt;p&gt;To learn more about setting up and running front end with an simple web api in Azure. I started building a simple clint app in Angular, that calls a ping method in a web API using .net. Setting up the environment was not that hard as there are a lot of examples on how to do that, and I have done it before. Making the a proxy.config.json to proxy call to the local API etc.  &lt;/p&gt;

&lt;p&gt;But how to host it? I knew you could host it as a static page in a Storage account. Here is where my miss comprehension of CORS put me on the wrong path. I though the client, had to set Allowed Origins to the api. Not the other way around, where API server has to set Allowed Origins. If I had taken the time to read about CORS (&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS"&gt;https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS&lt;/a&gt;). I would only need to deploy my web api, set Allowed Origins to the static site url, and be done width my little test. &lt;/p&gt;

&lt;p&gt;But no I was still in the shadow of my miss comprehension, and did not think it could work (or was more difficult) using the static site in a storage account (but why even have static site, Morten??). I then started looking at how to host angular web app using app service, and express as the hosting server. &lt;/p&gt;

&lt;p&gt;I read though John Papa blog post on deploying a angular app to Azure (&lt;a href="https://johnpapa.net/deploy-angular-to-azure-vsts-angular-cli/"&gt;https://johnpapa.net/deploy-angular-to-azure-vsts-angular-cli/&lt;/a&gt;). This was just for testing, so I just looked at the github project on how to set it up, not the devops part (basically not the blog 😁). I got it working locally. I hade to set up the angular.json different, since the in the github solution it is using the older .angular-cli.json file. &lt;/p&gt;

&lt;p&gt;I got it working locally, and deployed it to azure using Visual Studio code. But the app did not work, since the express part missed the node_module folder. The node_module is excluded when deploying. I could manually deploy it but did not feel like the right solution. After some more testing and reading I found that I was missing the .deployment (see under) file in the express part of the solution. Now the app worked.&lt;/p&gt;

&lt;p&gt;.deployment&lt;br&gt;
[config]&lt;br&gt;
SCM_DO_BUILD_DURING_DEPLOYMENT=true&lt;/p&gt;

&lt;p&gt;Hosting was working but the original problem of setting up CORS did not work. I tried to set it up in the app service, hmmm did not work. Then tried to set it up in the Express app, still not working. Ok I have missed something. Maybe reading a little more on how CORS really works 😉? After about two minutes of reading it was clear. All the express stuff, and trying to get that to work was unnecessary (except from learning about hosting and deploying using express). So then I just deployed the client to the storage account. Then set the static site to be trusted in the CORS settings in the API app service. Wow it worked. &lt;/p&gt;

&lt;p&gt;So what did I learn (ones more)? How to set up an express server to host an Angular app in Azure (not ones more). But more importantly. Thinking that one have understood something based on loosely hearing about it, does not mean on really understands it. Before one starts a task or project. It is important to think about what is assumptions, and take some time reading about them. It can save a lot of time. This was just me learning stuff, but in a real project it can be very expensive when one just assume things. &lt;/p&gt;

</description>
      <category>angular</category>
      <category>cors</category>
      <category>azure</category>
    </item>
    <item>
      <title>Some windows short cuts I like to remember</title>
      <dc:creator>gaotter</dc:creator>
      <pubDate>Thu, 15 Oct 2020 08:04:27 +0000</pubDate>
      <link>https://dev.to/gaotter/some-windows-short-cuts-i-like-to-remember-3ppc</link>
      <guid>https://dev.to/gaotter/some-windows-short-cuts-i-like-to-remember-3ppc</guid>
      <description>&lt;p&gt;Hi here is some window short cuts I like to remember. &lt;/p&gt;

&lt;p&gt;Win + shift + s a nice snipping tool. &lt;/p&gt;

&lt;p&gt;Win + v good old clipboard, missed you.&lt;/p&gt;

&lt;p&gt;Win + . (⌐■_■)&lt;/p&gt;

&lt;p&gt;Win + Alt + R Se how I did stuff&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
