Introduction
This article will cover the following things
- Brief Concepts
- Implementation - Two ways to develop an application in Windows:
- Implementation - Develop application in Windows:
- Implementation - Install .NET Core on Linux Server (Ubuntu 16.04)
- Implementation - Deploy Application (Created on Windows) on Linux Server (Ubuntu 16.04)
- Implementation - Develop application on Linux
- Implementation - Develop a MVC application on Windows using Visual Studio:
Brief Concepts
What is Dot Net Core?
- Development platform/framework maintained by Microsoft.
- It supports following OS:
- Windows
- MAC
- Linux
- cloud
- Embedded/IOT scenarios
Features
- Flexible deployment:
- Can be deployed on all the supported machines.
- Cross-platform:
- Runs on Windows, MAC and Linux; - - will be covered in today’s session
- Can be ported to other OS. - - will be covered in today’s session
- Can be installed by any command line tool.
- Compatibility:
- NET Core is compatible with .NET Framework, Xamarin and Mono, via the .NET Standard Library.
- Open source:
Implementation (Using Code):
1. Two ways to develop an application in Windows
- Using command line
- Using Visual Studio IDE
2. Develop application in Windows
2.1. Using Command Line - STEPS
![Image 1](/KB/dotnet/1112989/image1.png)
- Open cmd.exe / relevant command line tool
- Follow the below commands to create a sample project and install .NET core
- cd C:\Gourav_Personal\Study Material\.NET CORE\Demo
- mkdir demo1
- cd demo1
- dotnet new
![Image 2](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Restore the packages and run the project
- dotnet restore
- dotnet run
![Image 3](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Download demo1 attachment for the reference.
2.2. Using Visual Studio - STEPS
- Install and Open VS 2015 with update 3 or above
- Install .NET Core RC2 Package
- Create a project
- Choose .NET Core tab and select console application(.NET Core)
![Image 4](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Write few line of codes in program.cs
![Image 5](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
![Image 6](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Download demo2 attachment for the reference.
3. Install .NET Core on Linux Server (Ubuntu 16.04)
- I am using Oracle Virtual Box with Ubuntu 16.04 image on my windows machine to execute this use case.
- Run the following commands to get this working:
- sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet/ trusty main" > /etc/apt/sources.list.d/dotnetdev.list'
- sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 417A0893
- sudo apt-get update
![Image 7](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Install the following package on your Linux machine:
![Image 8](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Now GO to the downloaded location and run the following commands :
- sudo dpkg -i libicu52_52.1-3ubuntu0.4_amd64.deb
![Image 9](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- sudo apt-get install dotnet-sharedframework-microsoft.netcore.app-1.0.0-rc2-3002702
![Image 10](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- sudo apt-get install dotnet-dev-1.0.0-preview2-003121
![Image 11](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
4. Deploy Application (Created on Windows) on Linux Server (Ubuntu 16.04)
- Move the demo1 application attached in the article into the Linux machine and un-pack it.
- Go the application location in terminal.
![Image 12](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Restore the packages and run the project
![Image 13](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
![Image 14](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
5. Develop application on Linux
STEPS
- Open Terminal
- Follow the below commands to create a sample project and install .NET core
- cd Downloads
- mkdir demo2
- cd demo2
- dotnet new
![Image 15](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Restore the packages and run the project
- dotnet restore
- dotnet run
![Image 16](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
6. Develop a MVC application on Windows using Visual Studio
STEPS – Multiple Concepts
- General:
- Open VS 2015 and Create Project
![Image 17](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Choose Web - > ASP .NET Core Web Application (.NET Core) and Choose template – Empty (To understand the concept better)
![Image 18](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
![Image 19](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Static Files:
- Comment the default code in the StartUp.cs file.
![Image 20](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Add Html files in wwwroot folder
![Image 21](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Add “Microsoft.AspNetCore.StaticFiles” into the project.json file under the dependencies section
![Image 22](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Restoring of packages once we save the file in VS 2015
- Add
app.UseStaticFiles()
into the startup file into the configure method.
![Image 23](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Run the application with the static URL for e.g. http://localhost:50938/Sample.html
![Image 24](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- MVC
- Add “
Microsoft.AspNetCore.Mvc
” into the project.json file
![Image 25](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Add MVC structure (Add Controllers and Views Folder and default files into that)
![Image 26](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Add
services.AddMvc()
in the "ConfigureServices
" method into the startup file - Add the following code into the "
Configure
" method related to MVc route
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" }
);
});
![Image 27](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Run the application with the MVC URL for e.g. http://localhost:50938/Home/Index
![Image 28](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Class Library Concept:
- Add Class library to extract any common code (type .NET core)
![Image 29](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Create interface and class and implement some logic
![Image 30](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Mention the entry into the main project's project.json “for the class library” to use it in the main project. For e.g. "ClassLibrary1": "1.0.0-*"
![Image 31](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Use it as a service in Startup.cs file using dependency injection concept. For e.g.
services.AddSingleton<IInterface, Class1>();
![Image 32](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Implement the same into the controller as "Controller Injection"
![Image 33](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Run the application with the MVC URL for e.g. http://localhost:50938/Home/Index
![Image 34](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Middleware Concept:
- Comment all the code in the "
Configure
" method into the StartUp.cs file.
![Image 35](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Add middleware class into the main project.
![Image 36](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Few changes into the "
Invoke
" method into the middleware class for e.g.
public Task Invoke(HttpContext httpContext)
{
httpContext.Response.WriteAsync("Middleware Output");
return _next(httpContext);
}
![Image 37](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Use the same middleware class into the startup file for e.g.
app.UseMiddleware();
![Image 38](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
- Run the application for e.g. http://localhost:50938
![Image 39](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Download demo3 attachment for the reference.
Points of Interest
I loved using .NET core as it allows us to develop application on any platform and apart from that if the application is developed on Windows or Linux,it can be deployed on both of them.
Thats amazing thing by Microsoft :)
Reference Demos
I have attached three demo files for the demostration of the explaination showed in the above article.