Monday, April 6, 2020

Learning to develop .net core on Linux - Install VS code

To install Visual Studio Code we need to open a terminal window


First install the key

sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc

Install the repository

sudo sh -c 'echo -e "[code]\nname=Visual Studio Code\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/vscode.repo'


Update the package cache

sudo dnf check-update


Then install code

sudo dnf install code


Now that VS code is installed

 Lets install the .net core 3.1 sdk

 First get the key and repo

sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
sudo wget -O /etc/yum.repos.d/microsoft-prod.repo https://packages.microsoft.com/config/fedora/31/prod.repo

Now we can install .net core 3.1

sudo dnf install dotnet-sdk-3.1


Make sure in Visual Studio code you install the C# extension by Microsoft

Sunday, April 5, 2020

Learning to Develop .net core on Linux - Laptop setup

I decided it would be better learn linux on an actual machine instead of a virtual machine.   I looked at some of the linux laptop available and found I like the System76 laptops but opted to get a get a cheap laptop to learn on.

The laptop I bought was a HP 15-dy1731ms


First thing I did was boot up the computer and enter the Bios by pressing F10 at startup.

In the BIOS i made 2 changes first I disabled secure boot so I could install Linux.


Second I made the USB drive a the primary boot device.



I went ahead and booted off the Fedora usb drive and installed it to the hard drive.  As part of the install I deleted the windows partitions and let fedora partition the hard drive.

Once installed I noticed that the Realtek rtl8821ce wifi did not work.  I tried the steps in this forum question and few like it but did not have any luck getting to work reliably.

https://askubuntu.com/questions/983251/installing-wi-fi-driver-for-realtek-semiconductor-rtl8723de-device-10ecd723

In the end I bought a TP-Link usb wifi adapter (that has linux support) to connect to the internet

https://www.amazon.com/gp/product/B008IFXQFU/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1

Learning to develop .net core on linux

I have been wanting to learn how to develop software for Linux for a while.  Now that Microsoft is has been making .net core work cross platform.  By cross platform I mean it works on Windows, Mac, and Linux.   I figured it was time to learn.

I see Windows 10 has been getting features added to it's Windows Subsystem for Linux.   Visual Studio Code is a light weight IDE which works cross platform (including Linux).  .Net 5 will also be cross platform. 

So I decided to get a cheap laptop and load Linux on it and see what I can do using .net on it. 

Which Linux Distro to choose.  I looked at 3 distros first Ubuntu, Mint Linux, and Fedora.  Ubuntu is a fast, simple, and secure distro.  Mint is a distro of Linux which has a similar UI to Windows 10.   Fedora is the open source version of Red Hat Linux. 

I chose to go with Fedora because it has a Developer center.  In the developer center there is section which covers .net core. 


https://developers.redhat.com/products/dotnet/overview

There is also a free ebook.  Available Transitioning to .NET Core on Red Hat Enterprise Linux

https://developers.redhat.com/promotions/dot-net-core/?intcmp=70160000000gyhZAAQ 



So to get started with Fedora  I downloaded the Fedora Media Writer and Fedora Workstation 31 iso

https://getfedora.org/en/workstation/download/

Note any usb drive you use as a Fedora boot disk will only work with Linux afterwards.  Use the Fedora Media Writer to create a boot USB you can use to install Fedora on a laptop


Saturday, September 28, 2019

Using AsyncStreams to call a web service

.Net Core 3.0 includes Asynchronous streams.   


Basically IAsyncEnumerable<T> is an asynchronous version of IEnumerable<T>.   To use it in a you await a foreach loop to consume the elements

await foreach (var location in GetISSLocationSequence())
{
    Console.WriteLine(location);
}




GetISSLocationSequence is an async function which yield 20 IIS locations.  
It waits 1 second between getting locations


public static async System.Collections.Generic.IAsyncEnumerable<stringGetISSLocationSequence()
{
    using HttpClient http = new HttpClient();
    for (int i = 0; i < 20; i++)
    {
        var json = await http.GetStringAsync("http://api.open-notify.org/iss-now.json");
        await Task.Delay(2000);
        yield return json;
    }
}

One other thing I would like to point out was the format for the using 
statement.  Notice in this function I am using a using statement to make 
sure the HtppClient get disposed of when we done with it.  Notice we no 
long have to put the code is {}.  The HttpClient will dispose at the end 
of the function

To demo this I put the code in a .net core console application.  To allow 
await in the sub main I changed it from a void to async Task


class Program
{
    static async Task Main(string[] args)
    {
  
        await foreach (var location in GetISSLocationSequence())
        {
            Console.WriteLine(location);
        }
    }

Sunday, May 19, 2019

Error with Cert when starting an Asp.Net core website on the Mac

On my Mac I tried to run a Asp.Net core web site and got an error when starting the web site about ssl cert was invalid.

Adding the certificate to the Trusted Root Certficates store failed with the following error: Failed with a critical error.

The sites were working a few days before so I tried this command line.

     

dotnet dev-certs https --trust


Got a response A valid HTTPS certificate is already present.

Ran app again and still got same error.


The way I fixed it on my Mac was to open the KeyChain Access app and deleted the localhost certificate




Ran 

dotnet dev-certs https --trust 




After this I was able to debug my asp.net core apps in vs for Mac 2019 and Rider



Sunday, July 29, 2018

Xamarin Forms Storing some Json in a Standard Library

In one of my Xamarin forms app I wanted to include some default data for the app in Json format.

Rather than create a web service to load the data on the first run I went ahead and created a Json file to include the default data.

[
{"Question":"Name two key components that make up Sitecore XP","Answer":"Experience CMS, and marketing platform"}

etc...


I added the Json file to my Xamarin Forms standard library and set its build action to embedded resource





To extract the data I used a function like this.

        public ObservableCollection<QuestionPair> GetQuestions()
        {
            var assembly = typeof(DataService).GetTypeInfo().Assembly;
            Stream stream = assembly.GetManifestResourceStream("SitecoreFlashCards.questions.json");
            StreamReader streamReader = new StreamReader(stream);
            string json = streamReader.ReadToEnd();
            ObservableCollection<QuestionPair> questions = new ObservableCollection<QuestionPair>();
            var list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<QuestionPair>>(json);
            foreach(var item in list)
            {
                questions.Add(item);
            }

            return questions;
        }

First I created a stream so I can access the Json file in the standard library.  The I used a stream reader to get the text from the file and deserialize it with Json.net

The path to the resource is the class library default namespace and the path to the file and file name in the dll.

Replace DataService with the name of the class you are trying to get the Json file from.


Sunday, July 22, 2018

Using GitHub with Visual Studio for the Mac

First lets create a new Repository on GitHub .

For this demo I created a public repository that has a readme, and a Visual Studio Git Ignore file.




Now I created a simple .Net Core Console app to check into GitHub.  I checked use git for version control.



Once I created the app I built it to make sure it runs.   Now the first thing to do is create an initial commit to the local git repository.  In the version control menu select Review and Commit.


In Git Hub open the Repository you created. Press the clone or download button and copy the url for the repository.

In Visual Studio Version Control Menu select Manage Branches and Remotes



Add a new Remote source with the url from the GitHub website



I made a minor code change and Selected Review and Commit changes




In the dialog I selected Push to remote.  First time you connect to GitHub it will ask for you credentials




Once you push your changes you should see it in GitHub.  Make sure before you push your changes you update the solution from GitHub.