Hello Everywhere with .NET Core

Leave a comment
.Net / Newbies / Web

Hello Everywhere .NETCore from Maria Naggaga on Vimeo.

In this video, I demonstrated how to get started with .NET Core on OS X,  Docker and Windows.  In this post, I am going to go into a little more detail on how to get started on building  a simple .NET Core application on various.

What did you learn ?

Setting up your developer environment

OS X

To get started  on .NET Core you will require  the following:

Docker

To get started with Docker you will need the following :

  • Docker Images for  Mac, Windows and Linux.
  • Start running the .NET image using the command  below

Windows

To get started on windows

.NET Core is Cross Platform

In the past,  when newer developers thought of  .NET and C#  solely being on Windows.  With  the .NET Core you can easily you can build this application on any platform of your choice.

.NET Core Command Line(dotnet cli)

Making it easy to say hello ! The dotnet commands make is pretty easy to hello.  Let’s have a look at the commands I used in the video ,and what’s happening at each command.

dotnet new , dotnet run and dotnet restore

dotnet new , dotnet run and dotnet restore

dotnet new

Initializes a basic .NET project;  this means that it creates a simple C# project. This includes the following files:
Program.CS : A simple C# console application that has one line of executable code;

Console.WriteLine("Hello Everywhere!")

Project.json : Defines: the dependencies that this application needs to run;  target framework, and applications properties like version numbers.

{
    "version": "1.0.0-*",
    "compilationOptions": {
        "emitEntryPoint": true
    },

    "dependencies": {
        "NETStandard.Library": "1.0.0-rc2-23811"
    },

    "frameworks": {
        "dnxcore50": { }
    }
}

dotnet restore
The dependencies section, of the project.json defines the packages needed to run the application.
dotnet restore  does the following:

  • Creates the project.lock.json file . In a nutshell the project.lock.json is a file that is generated in the process of restoring  Nuget packages and includes the version, contents, and dependencies of all the packages your application depends on.
  • Downloads all the required packages you needed to run the application

dotnet run
Compiles and executes the applications

Hello World Console to Hello World Web

You have created simple console Hello World app. In this section I am going to show you how to take your console application to the browser. In order to do this there are couple of things we need to add to our existing console applications

Add the Kestrel HTTP Server package as dependency to the project.json file

{
        "version": "1.0.0-*",
        "buildOptions": {
        "debugType": "portable",
        "emitEntryPoint": true
  },
        "dependencies": {},
        "frameworks": {
        "netcoreapp1.0": {
        "dependencies": {
            "Microsoft.NETCore.App": {
            "type": "platform",
            "version": "1.0.0"
        },
        "Microsoft.AspNetCore.Server.Kestrel": "1.0.0"
      },
      "imports": "dnxcore50"
    }
  }
}

Restore Packages

dotnet restore

Add a Startup.cs  file that defines the request logic

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;

namespace movingtoweb
{
    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {
            app.Run(context =>
            {
                return context.Response.WriteAsync("Hello I'm the Web");
            });
        }
    }
}

Setup an start the web host in  Program.cs

using System;
using Microsoft.AspNetCore.Hosting;

namespace movingtoweb
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }
}

Run the app

 dotnet run

Go too http://localhost:5000

Thanks for reading !

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>