Wednesday, November 15, 2017

Building .Net Core Apps for Docker


As mentioned in other posts about Docker, Docker is talk of the town. Docker is a containerization platform which fully lives up to it's motto - "Build, Ship, and Run Any App, Anywhere".

Docker containers provides an additional layer of abstraction on top of virtual machines we're familiar with. Simply put, Docker is a tool that can package an application and it's dependencies in a virtual container that can run on any server (Linux or Windows). This capability gives freedom from underlying operating system constraints and no longer can we say "It works on my machine!". 

New kid on the block ASP.NET Core provides first class Docker support owing it's inherit capability for cross-platform code. 

Visual Studio 2017 supports tooling for adding Docker support for brand new .Net Core Apps or existing .Net Framework apps. You may ask can we run existing Full .Net Framework apps on Docker? We can safely yes if your were to choose Windows containers. 

We must be living in an age of innovation. No time is better than now where we have fairly stable .NET Core 2.0 standard, matured .Net Core stacks (EF Core, ASP.NET Core 2.0) and a feature rich Docker support (did I say about multi-stage deployments).

If you're from .Net school,  you may be looking for a perfect tutorial to dip your toes into Docker world using .Net Core, we can happily recommend perfectly written, very detailed and any .Net programmer can get it. 

Excellent blog post by Maher Jendoubi on ASP.Net Core 2.0 in Docker is an excellent place to begin your Docker journey with .Net Core  stack.




C# code sample calculating US Holidays List for a given year programmatically

C# code sample to calculate US holidays list for any given year programmatically. All these holidays occur on a specific week in a month of year.

  •  MLK Day 
  •  President's Day 
  •  Memorial Day 
  •  Labor Day 
  •  Columbus Day 
  •  Thanksgiving Day

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Linq;

namespace holidayhelper
{
    class Program
    {
        static void Main(string[] args)
        {
            int year = 2017;
            Console.WriteLine("{0,-20}:{1}","MLK Day",HolidayHelper.MlkDay(year));
            Console.WriteLine("{0,-20}:{1}","Presidents Day",HolidayHelper.PresidentDay(year));
            Console.WriteLine("{0,-20}:{1}","Memorial Day",HolidayHelper.MemorialDay(year));
            Console.WriteLine("{0,-20}:{1}","Labor Day",HolidayHelper.LaborDay(year));
            Console.WriteLine("{0,-20}:{1}","Columbus Day",HolidayHelper.ColumbusDay(year));
            Console.WriteLine("{0,-20}:{1}","Thanks Giving Day",HolidayHelper.ThanksgivingDay(year));
        }
    }
    public static class HolidayHelper{
        //MLK Day - Jan
        public static string MlkDay(int year)
        {
            return NthDayOfMonth(year, 1, DayOfWeek.Monday, 3);
        }
        // President's Day - Feb
        public static string PresidentDay(int year)
        {
            return NthDayOfMonth(year, 2, DayOfWeek.Monday, 3);
        }
        //Memorial Day - May last week
        public static string MemorialDay(int year)
        {
            return LastMondayOfMay(year);
        }
        //Labor Day - Sept First week
        public static string LaborDay(int year)
        {
            return NthDayOfMonth(year, 9, DayOfWeek.Monday, 1);
        }
        ///Columbus Day - october second week
        public static string ColumbusDay(int year)
        {
            return NthDayOfMonth(year, 10, DayOfWeek.Monday, 2);
        }
        ///ThanksGiving Day - nov Fourth week
        public static string ThanksgivingDay(int year)
        {
            return NthDayOfMonth(year, 11, DayOfWeek.Thursday, 4);
        }
        
         /// <summary>
        /// returns the nth day of given month (like 1st monday of month, 3rd tuesday of month or 4th monday of month)
        /// </summary>
        /// <param name="year">input year</param>
        /// <param name="month">input month</param>
        /// <param name="dow">Day of week</param>
        /// <param name="n">nth day</param>
        /// <returns>Datetime for the nthday</returns>
        private static string NthDayOfMonth(int year, int month, DayOfWeek dow, int n)
        {
            var days = DateTime.DaysInMonth(year, month);
            var nthday = (from day in Enumerable.Range(1, days)
                let dt = new DateTime(year, month, day)
                where dt.DayOfWeek == dow && (day - 1) / 7 == (n - 1)
                select dt).FirstOrDefault();
            return nthday.ToShortDateString();
        }

         /// <summary>
        /// returns the last monday date for memorial day for the given year. 
        /// </summary>
        /// <param name="year">input year</param>
        /// <returns>Memorial day date</returns>
        private static string LastMondayOfMay(int year)
        {
            var dt = new DateTime(year, 5, 31);
            while (dt.DayOfWeek != DayOfWeek.Monday)
            {
                dt = dt.AddDays(-1);
            }
            return dt.ToShortDateString();
        }
    }
}

Docker commands cheat sheet


Welcome to world of Docker. Docker is such a blessing for DevOps folks and processes. We still remember days when world is amazed by virtual machines. Oh, I can spin off multiple virtual OSes running on host windows. Virtual machines indeed solved some of the problems. However, they're clunky, heavy in size.

Docker took us to another granular level of containers & images much thinner then virtual OSes, layered images further reducing the size of images and making deployments easy piece of cake. You can further optimize cost savings with Docker containers. Totally isolated, bootstrapped environment which can run on any docker host with any constraints opens up a whole lot of possibilities.

We're impressed with Docker. We integrated Docker containers into our workflows. Docker file is an amazing piece of art. Few concise commands in a text file can initialize, build and run a mission critical app in few seconds.

Let's grasp some useful docker commands. Of course, this list is very small. We'll keep adding as we learn more and more

DOCKER COMMAND
DESCRIPTION
Docker PS
list of running containers
DOCKER RM [ID]
removes container with specific ID
DOCKER Images ls
list of all containers
DOCKER STOP [ID]
stops container
LESS PATH
UNIX command to open a longer file
docker build -t api -f testapp/Dockerfile . &&  docker run -it -p 9000:9000 api
Builds a container using specific docker file and runs containers on a specific port
docker run -it -p 9000:9000 --entrypoint bash api
Runs a shell in a container that is running
CAT  PATH – opens a file in terminal

Q – to exit the file

Popular Outlook keyboard shortcuts cheat sheet


We're a big fan of Office 365 Outlook or for that matter, MS office suite. World comes to a stand still moment MS Office disappears from planet earth. This is true no exaggeration. Business world runs on Excel, Word and Outlook.

Outlook is still one of the leaders for emailing in the corporate world. Knowing some of keyboard shortcuts for Outlook can make your life little easy.

P.S. Following keyboard shortcuts are WINDOWS based laptops and desktops

Task
Command
Switch to Mail 
 CTRL + 1
Switch to Calendar
CTRL + 2
Switch to Notes
CTRL + 5
Create a mail
CTRL + SHIFT +M
Create a new appointment
CTRL +SHIFT + A
Create  a new Note
CTRL + SHIFT +N
Forward a message
CTRL + F
Send a message
CTRL + ENTER
Reply to message
CTRL + R
Reply to All
CTRL + SHIFT + R
Check for new mail
CTRL + M or F9
Open a received message
CTRL + O
Mark a message as read
CTRL + Q
Shift to Inbox
CTRL + SHIFT + I
Add an attachment
There is no straight keyboard command. Use ALT and then Press H, then A  then F key


Visual studio 2017 keyboard shortcuts one page cheat sheet



Visual Studio 2017 has ton of keyboard shortcuts. It is overwhelmingly large. But, we very frequently use small percentage of them on a daily basis.

Being familiar with important keyboard shortcuts makes you a ninja programmer and added bonus improved productivity levels. Programming world loves keyboard so much. GUI is for kids.

Visual Studio 2017 popular keyboard shortcuts 

RESHARPER REFACTOR MENU   
CTRL + SHIFT + R
-        MOVE TO NEW FILE
CTRL + R, O
-        RENAME
CRT + R, R
VISUAL STUDIO MAIN MENU
ALT +
BUILD
F6
REBUILD
ALT + B,R
DOCK WINDOWS IN STUDIO
ALT + -
NAVIGATE TO ANY CODE ARTIFACT
CTRL + ,
NAVIGATE BACKWARD FORWARD
CTRL + -
START WITHOUT DEBUGGING
CTRL + F5
STOP DEBUGGING
SHIFT + F5
START DEBUGGING
F5
ATTACH TO PROCESS
CTRL + SHIFT + P
PROPERTIES
ALT + ENTER
DELETE ALL BREAK POINTS
CTRL + SHIFT +F9
BREAKPOINT WINDOW
CTRL + ALT + B
CALL STACK WINDOW
CTRL + ALT + C
IMMEDIATE WINDOW
CTRL + SHIFT + I
LOCAL WINDOW
CTRL + ALT + V, L
ERROR WINDOW
CTRL + \, E
QUICK WINDOW
CTRL + ALT + Q
RUN TO CURSOR
CTRL + F10
COLLAPSE DEFINITION
CTRL + M, CTRL + O
COLLAPSE REGION
CTRL + M, CTRL + S
EXPAND REGION
CTRL + X, CTRL + E
EXPAND DEFINITION
CTRL + M, CTRL + X
CYCLE CLIPBOARD
CTRL + SHIFT + V
FIND IN FILES
CTRL + SHIFT + F
FORMAT DOCUMENT
CTRL + K, CTRL + D
FORMAT SELECTION
CTRL + K, CTRL +F
GO TO DECLARATION
CTRL + F12
GO TO DEFINITION
F12
FIND ALL REFERENCES
SHIFT + F12
PEEK DEFINITION
ALT + F12
MAKE LOWER CASE
CTRL + SHIFT + U
MAKE UPPERCASE
CTRL + U
NAVIGATE
CTRL + ,
REPLACE
CTRL + SHIFT + H
SELECT WORD
CTRL + SHIFT + RIGHT ARROW/LEFT ARROW
OPEN FILE
CTRL + O
RUN ALL TESTS
CTRL + R, CTRL + A