Monday, 5 December 2016

Reading appsettings.json in .Net Core Console Application under Linux

Recently I was looking at how to load and use appsettings.json file in .Net Core Console application. Many resources on .Net forums and sites were descripting use of appsettings.json file in ASP.NET Core web App, but very little described appsettings with .Net Core Console app. I managed to created sample .Net Core app with the minimum required libraries. Creation of this sample program I am going to describe here. Let's call our App "appsettings".
$mkdir appsettings
$cd appsettings
$dotnet new
$dotnet restore
Now create appsettings.json file:
{
    "AppSettings": {
        "Date": "1900-01-01"
    }
}
Date is the setting I am interested in loading from AppSettings section.

Edit project.json file to include "Microsoft.Extensions.Configuration.UserSecrets": "1.1.0" in dependencies section. This is the only extra library we need in order to use ConfigurationBuilder functions.
{
    "version": "1.0.0-*",
    "buildOptions": {
        "debugType": "portable",
        "emitEntryPoint": true,
        "outputName": "AppSettings"
    },
    "dependencies": {},
    "frameworks": {
        "netcoreapp1.0": {
            "dependencies": {
                "Microsoft.NETCore.App": {
                    "type": "platform",
                    "version": "1.1.0"
                },
                "Microsoft.Extensions.Configuration.UserSecrets": "1.1.0"
            },
            "imports": "dnxcore50"
        }
    }
}
Now lets edit the Program.cs file. We are going to load the Date setting and display it in Console. Don't forget to include "using Microsoft.Extensions.Configuration;"
using System;
using System.IO;
using Microsoft.Extensions.Configuration;

namespace ConsoleApplication
{
    public class Program
    {
        public static string AppSettingsFile = "appsettings.json";
        public static string AppSettingsSection = "AppSettings";
        public static string DateSetting = "Date";

        public static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile(Program.AppSettingsFile, optional: true, 
                             reloadOnChange: true);
            
            var config = builder.Build();
            var appSettings = config.GetSection(Program.AppSettingsSection);
            string dateString = appSettings[Program.DateSetting];
            Console.WriteLine("Date:" + dateString);
        }
    }
}
That is pretty much all. Now let's build and run it.
$dotnet build
$dotnet run
Date:1900-01-01

Monday, 14 November 2016

xUnit Theory test ClassData with complex type

xUnit.net is a powerful, free, open source, unit testing tool for the .NET Framework. xUnit uses different notation to NUnit or MSUnit to specify test with parameters. Here I am going to show how to use Theory ClassData with complex type.

I have a sample class Plant, which has function 'bool IsEqual(Plant plant);'. We will create a Theory test for it.
[Theory] [ClassData(typeof(IsEqualTestData))] public void IsEqual(Plant plant1, Plant plant2, bool expectedResult) { Assert.Equal(expectedResult, plant1.IsEqual(plant2)); }
Class IsEqualTestData is a ClassData used with complex type Plant. It generates a list of arrays of 3 objects Plant1, Plant2, and ExpectedResult boolean.
private class IsEqualTestData : IEnumerable<object[]> { private readonly List<object[]> _data = new List<object[]> { new object[] { new Plant() { Name = PlantTester.PlantName1, Description = PlantTester.PlantDescription1 }, new Plant() { Name = PlantTester.PlantName2, Description = PlantTester.PlantDescription2 }, false }, new object[] { new Plant() { Name = PlantTester.PlantName1 }, new Plant() { Name = PlantTester.PlantName1 }, true }, new object[] { new Plant() { Name = PlantTester.PlantName1, Description = PlantTester.PlantDescription1 }, new Plant() { Name = PlantTester.PlantName2, Description = PlantTester.PlantDescription1 }, true } }; public IEnumerator<object[]> GetEnumerator() { return _data.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } }
When running in xUnit it will report result of running each entry in the test.

If you are using Visual Studio you can download snippet file from github repository.

Reference:

xUnit_ClassData.snippet
Using dotnet watch test for continuous testing with .NET Core and XUnit.net - Scott Hanselman

Tuesday, 5 July 2016

.Net Core 1.0 Released! Or installing .Net Core on Debian

This was a great news to me that .Net Core 1.0 was released. I already was trying .Net Core RC2 on my Debian 8.x so I prepared to go through the installation process of new release. Surprisingly installation of .Net Core 1.0 described in Install for Debian 8 on Microsoft site was very smooth in comparison to beta versions. All I had to do is to clean up my previous install from ~/.dotnet go through the instructions on install page and "Hello World!" programs runs. Very pleased to see that improvement.

So here I am going to provide some links to what to do next once you get your install done too.

References:





Sunday, 26 June 2016

Recording Music CDs under Win 7


From days of Windows 95, Windows 98, Windows XP CD/DVD burners and writers were only coming out on the market and computer users were required to install additional software, which will allow them to burn CD and DVD disks. Disk writing software like Nero was included for free with the CD/DVD burner you could buy for your desktop computer or included on new laptop. Modern Windows operating system like Windows 7, Vista, Windows 8 and Windows 10 already have built in support for burning CD / DVD music, data and video disks. Users can use included Windows Media Player to rip and burn music CDs.

Under Windows 7 to burn audio CDs insert CD disk in disk drive and in Windows Media Player open Burn tab, drag music files you want to burn. The blue bar shows how much space left on the disk. Once files are selected click Start burn to start recording on the disk.


Data CDs / DVDs can be burned directly from Windows Explorer. Disk image files with ISO extension can be burned using right mouse click on the file and selecting "Burn disk image".

If you still require more features than the basic functions to burn disks look through available free and commercial applications available on sites like filehippo

References:




Sunday, 24 January 2016

Install NVidia Graphic Drivers on ASUS ROG G750JW

ASUS ROG G750JW comes with GEFORCE GTX NVidia 2GB graphic card. To get a full power of this card on Debian we need to install nvidia drivers for it. Debian Wiki NvidiaGraphicsDrivers article describes several ways to do it. Here I am going to summarise the steps that worked for me.
  1. Add SteamOS repository to /etc/apt/sources.list.d/steamos.list, for example:
  2. deb http://repo.steampowered.com/steamos brewmaster main contrib non-free
  3. Update the list of available packages. Install the appropriate nvidia driver and related packages from SteamOS repository:
  4. # aptitude update
    # aptitude install -t brewmaster libegl1-nvidia:amd64 libgl1-nvidia-glx:amd64 libgl1-nvidia-glx:i386 libgl1-nvidia-glx-i386 libgles1-nvidia:amd64 libgles2-nvidia:amd64 libnvidia-eglcore:amd64 libnvidia-ml1:amd64 libxnvctrl0 nvidia-alternative nvidia-driver nvidia-driver-bin nvidia-kernel-dkms nvidia-modprobe nvidia-settings nvidia-vdpau-driver:amd64 xserver-xorg-video-nvidia
This will also install the recommended nvidia-driver package. DKMS will build the nvidia module for your system. Restart your system to enable the nouveau blacklist.

References: