Friday, September 30, 2016

This tutorial attempts to help one easily publish an ASP.NET web application to Azure. An Azure subscription can be obtained through MSDN, Microsoft Imagine (formerly known as Dreamspark), or simply get a free one month $250 trial account from https://azure.microsoft.com/en-us/free/.
For students, get a free account from https://catalog.imagine.microsoft.com/en-us. Although this account is limited in services, it allows a student to host web applications using SQL Azure. Scroll further down the page and click on “Activate Azure benefits for Students”.

Once you have secured a subscription, you can go ahead and create an ASP.NET MVC 6 web application. For this tutorial, I am using the free Visual Studio 2015 Community Edition, which you can download and install from https://www.visualstudio.com/downloads/.

Let’s get started. We will first create an ASP.NET MVC 5 web application.

I named the web application “TryAzureDeploy”. Go ahead and give it whatever name you fancy.

Click OK.

Under “Select a template” select MVC and leave “Host in the cloud” and “Add unit tests” unchecked:

Click OK.

Once the application is created, build it by hitting “Shift + Ctrl + B” on your keyboard.

Run the application by hitting “Ctrl + F5” on your keyboard. You will see a fully functional ASP.NET MVC app that looks like this:

Our next task is to deploy the app to Azure. Before we do that, it is necessary to install the latest Azure SDK in your Visual Studio before you continue. To do that, click on “Tools >> Extensions and Updates…”

Click on Updates on the left-side. If there is an outstanding Azure SDK update, you will see it in the list. At the time of writing, the latest Azure SDK update was version 2.9.5. Make sure you install the latest outstanding version.

Before we publish our web application, let us create a database server and database on Azure. Point your browser to https://portal.azure.com and login. Click on “SQL databases” on the left-side:

The next step is to create a database server and a database instance. To do that, click on “+Add” at the top.

Once you are done creating the database server and database instance, return to Visual Studio. Right click on the project and select “Publish…”

This results in the next dialog being displayed.

Click on “Microsoft Azure App Service”.

Click New.

After you enter the missing data, click on “Create”. This takes you back to the publish wizard’s connection phase.

Click Next. At this stage, we will need to get the connection string of the database that was previously created. Return to the Azure portal in your browser and click on your new database:

Click on “Show database connection string”.

Copy the “ADO.NET (SQL Authentication)” connection string. Paste it into a text editor like notepad.exe. Replace {your_username} and {your_password} with the appropriate username and password for the database server. Thereafter, copy and paste the connection string with the correct username and password into the “ApplicationDbContext (DefaultConnection)” field.

Click Next.

We are now good to publish. This may take a few minutes. Once it is done, the new website will be open in your default browser. Go ahead and register a new user.

Successfully registering a new user means that the website is indeed working with SQL Azure. Pat yourself on the back.

Saturday, September 24, 2016

In this post we will look at building a simple command line .NET Core application that works with SQL Server. The database is created using the Code First development paradigm. The database table will look like this:

1) Create a working directory named InstituteSqlServer.

2) From within a command prompt in that folder, type: dotnet new. This creates a Hello-World .NET Core command-line application.

3) Using Visual Studio Code, navigate to the InstituteSqlServer folder to open the project.

4) Open Program.cs , change the namespace to InstituteSqlServer and save the file.

5) To build the “Hello World” command-line app and run it, execute the following commands from the command prompt:

dotnet restoredotnet builddotnet run

6) Since we will be accessing SQL Server using Entity Framework, we will need to add the following to the dependencies block of the project.json file:

11) It is necessary to have a helper method that reads name/value pairs from the appsettings.json configuration file. To this end, we will create a class file named Utility.cs in the Models folder that fulfills this task. This file contains the following code:

13) When the application is built, the .dll file is created in a directory somewhere under the bin folder. We need to make sure that the appsettings.json file is also copied to the same directory as the .dll file so that it can be read by the application. This is accomplished by adding the following to the “buildOptions” section of the project.json file:

"copyToOutput": { "include": [ "appsettings.json" ]}

14) It is time to build our application. At a command-prompt in the project folder, type the following command:

dotnet build

15) It is now possible for us to create the database using Entity Framework’s code-first paradigm. While in the command prompt, execute the following two EF commands:

dotnet ef migrations add FirstMigrationdotnet ef database update

Upon completion of the above EF commands, the database will have been created.

16) We are now in a position to add and retrieve data to and from the database. Add the following two methods to your Program.cs file:

19) This final step is the most exciting. If you run the application, it should add data to the Students table in the database and retrieve its contents. Can’t wait, lets do it. Execute these commands from the command prompt:

I am planning to write a series of blogs posts to introduce application and web developers to the all new .NET Core framework. This article is intended as an introductory lesson on the very basics of .NET Core. Unlike previous version of the .NET framework, .NET Core is open source, cross platform, cross device, small, and fast.

Our first task is to build a simple command-line application with .NET Core version 1.0.0. We will use the light weight Visual Studio Code for our text editor.

3) Start Visual Studio Code and open this project folder. There are two others ways, I know of, that can achieve the same result:

a. In the command-line, change directory to the “Institution” folder and type “code .”. This will open up your project folder contents in Visual Studio Code in the current directory (.).
b. Alternatively, navigate to the “Institution” folder using File Explorer (in Windows). Right-click on the folder and select “Open with Code”.

4) Once in Visual Studio Code, you can view the contents of this file. The Program.cs file looks like this:

The project.json file has NuGet dependencies necessary to build our console app. You may also notice the presence of another file named project.lock.json. This file is auto-generated and expands on project.json with more detailed dependencies required by your application. There is no need to commit file project.lock.json to source control.

5) Change the namespace from ConsoleApplication to Institution. Also, change the statement Console.WriteLine("Hello World!"); to

Console.WriteLine("Welcome to our institution!");

6) Save and back in the command prompt, type in:

dotnet restore

This instruction causes dependencies to be brought in from the NuGet repository

7) To execute the application, you can simple run the following command from the command-line:

dotnet run

“dotnet run” calls “dotnet build” to ensure that the app has been built, and then calls “dotnet institution.dll” to run the application. You can find the resulting institution.dll file in the bin\Debug\netcoreapp1.0 directory. Notice that the build process does not produce a institution.exe file. This is because we just created a portable app. Instead of creating a portable app, let us produce a self-contained .exe app.

Note that the name of the .dll file is dictated by the name of the primary project folder, and not by the name of your primary class file (Program.cs).

Compiling a self-contained .exe app

We will need to make some changes to the project.json file.

1) Delete the "type": "platform" element from all dependencies.

2) Next, add a new runtimes node as follows:

"runtimes": { "win10-x64": {}, "osx.10.11-x64": {} }

This causes the build system to generate native executables for the current environment. In Windows, you will build a Windows executable. On the Mac, you will build the OS X executable.
Save the project.json file, then run the following commands from the command-line:

dotnet restore dotnet build

The self-contained .exe file can be found at bin\Debug\netcoreapp1.0\win10-x64\Institution.exe.
You can execute the institution.exe file by typing the following on the command line while in the Institution folder:

bin\Debug\netcoreapp1.0\win10-x64\hello.exe
or simply:

dotnet run

Let us add a Student class to the project. It is good practice to organize our files in folders. This is especially beneficial when you are dealing with large projects. Therefore, create a folder named Model and add to it a Student.cs class file with the following code:

The above code describes a Student class with properties: Id, FirstName, LastName, Major, and DateOfBirth. It also generates some dummy data that can be accessed through the GetSampleStudents() static method.
Back in Program.cs, replace the Main() method with the following: public static void Main(string[] args) { List<Student> students = Student.GetSampleStudents(); foreach(var i in students) { Console.WriteLine("{0}\t{1}\t{2}", i.Id, i.FirstName, i.LastName); }} Do not forget to resolve the Student and List classes by using the following namespaces:

using System.Collections.Generic;using Institution.Models;

If you run the application by executing “dotnet run” from the command-line, you should see the following output:

1 Ann Lee2 Bob Doe3 Sue Douglas4 Tom Brown5 Joe Mason

In future posts, I am hoping to explore more complicated data driven apps developed using .NET Core. Meantime, I hope you benefited from this article.