Summary of awesome Cross platform .NET Core and ASP.NET Core Webinar. It’s great time to be an .NET developer

Introduction

This article is based on the webinar I gave on 6 March 2017. Thanks to the all the people joining in and sharing the knowledge and Mumbai Techie Group for again giving me the opportunity to share my learning with all the Attendee and getting involve in the Community.

Background

I received lot of feedback after the webinar, it was mixture of all, as i call myself a learner i want to improvise on all the aspects which people told me i can improve. I was happy and enthusiastic to share my knowledge and as well patient enough to note down the learnings. So i decided i should also blog my webinar agenda where people can get the resources to get started along with brief of all the agendas which i covered in the article or due to time constraint i was not able to show them the code. I hope i will learn from my mistake and in future will prioritize to not make the same mistake twice. Thanks all for taking your time and giving me the feedback, I appreciate that a lot.

Youtube Link for the .NET CORE and ASP.NET CORE Webinar

Agenda For the Day

Why ASP.NET 5.0 to .NET CORE 1.0

Introduction to .NET CORE

Command Line Tooling

Running your first dot net core program in windows

Running your first dot net core program in ubuntu VM

Visual Studio Code

Where is my ASP.NET?

Middleware

Sample ASP.NET Core application

Dependency Injection in ASP.NET Core

Deploying ASP.NET Core application using VsCode command line

Why ASP.NET 5.0 to .NET CORE 1.0

In this topic, i talked regarding the organization changes happening in Microsoft that cause the death of ASP.NET 5.0. I forget to tell that I didn’t impact the overall architecture of the.NET CORE 1.0 but it did impact the release candidate ASP.NET 5.0. Guys if you are really interested in knowing what actually happened what lead to the emergence of.NET CORE who they are actually targeting. My honest suggestion will be to watch this below mentioned the video.

Introduction to.NET CORE

Command Line Tooling

Running your first dot net core program in windows

Running your first dot net core program in ubuntu VM

Running your first dot net core program in ubuntu VM

Visual Studio Code

In order to read and learn more about above topics, I would recommend you to please read my previous articles

where I have talked about.NET CORE using VS code deep down. In case if you still face any problem or confusion feel free to ask the same on below comment section.

Where is my ASP.NET?

So, folks, I discussed that our majority of our time at the office is consumed while developing a web application, pretending all of us to be a web developer. So the main thing for career perspective is learning what ASP.NET seems like in ASP.NET Core because at the end of the day we have to create web applications.

So the main thing for career perspective is learning where ASP.NET seems like in ASP.NET Core because at the end of the day I have to create a business web application, not a console application. So, folks, in.NET CORE ASP.NET Core is a console application which will be responsible for creating its own web server as per your requirement, setting it up with all the configuration that you want to build with and then run it up once you want.

Let’s go and have Jam pack session of code to inspect the ASP.NET CORE

Here I have small scaffold CSharp program with hello world, now we gonna make our simple ASP.NET Core from the console program. So in order to create a web application, we need a web server we can run our web application. In ASP.NET Core we have a new lightweight cross-platform web server which has the capability of running cross-platform known as Kestrel. Kestrel is lightning fast web server which can server millions of request at a time and is based on the same library called Libuv which is used in Node.js

So I need to add the kestrel web server in our application and I will also use IISIntegration dependency which helps me to deploy my app on IIS once I am done creating the web application.

You all will be thinking will we have all powers of IIS…..?

My honest answer will be no, if you are targeting cross platform then we can ‘t be dependent on IIS. So now IIS will be working as a proxy server which will just pass all the request from the web to kestrel server to process.

Once we are done restoring the packages we are ready to set up our kestrel web server.

In ASP.NET Core we have a class called Startup which is a need in order to configure our web server. The Startup class gives us the power to control the web application as per our directions. Startup class consists of two methods

ConfigureServices(IServiceCollection services)

and

Configure(IApplicationBuilder app)

methods.

These two methods are the benchmark of the overall application and will decide how request flows through the pipeline. In .NET Core everything is modular and intact so we can swap anything out and swap anything in whenever we want to. We are the captain of this ship but the captain that knowledge of details as well will take good decisions. So be a good Captain as Jack Sparrow.

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
public class StartUp
{
public void ConfigureServices(IServiceCollection services)
{
//Responsible for injecting this class or service we want in our Controller
}
public void Configure(IApplicationBuilder app)
{
//Set up your http pipeline
}
}

Once we are done with done declaring ConfigureServices() and Configure

method. We will now show hello world using ASP.NET CORE. We need to add middleware (Note* we will be talking about middleware in depth later on in this blog) which will write hello to the browser for each request that comes to the web server as shown below. This sample sets up a single request delegate that handles all request and returns the string.

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
public class StartUp
{
public void ConfigureServices(IServiceCollection services)
{
//Responsible for inject this class or service we want
}
public void Configure(IApplicationBuilder app)
{
app.Run(context=>{
return context.Response.WriteAsync("HELLO WORLD FROM ASP.NET CORE");
});
}
}

Now once we are done with Let’s try to continue with setting up our WebServer.

Middleware

Middleware basically is set of software components which are placed together in a Queue form so when the request comes to the ASP.NET CORE pipeline it has through each middleware which we have to decide, we can on our short circuit the pipeline and return back to the browser without going to other middleware in the pipeline. We can also term Middleware as ASP.NET HttpModule and HttpHandler. If you are not aware of HTTP Module and Http Handler my honest suggestion would be to go to The Two Interceptors: HttpModule and HttpHandlers

So each request can subsequently have a PreProcessive logic and PostProcessive logic once the request completes and once one request one it will again flow to the middleware and then back to the UI screen. We can chain up multiple delegates using the app.Use as shown below. The next parameter represents the next delegate to be called in the pipeline.

For this demo, I will be using visual studio and I would consider that you have some abstract knowledge of web api and basic authentication. First, create a simple ASP.NET CORE WEB API with basic ValuesController.

I would not recommend this authentication for public API this authentication can be used where you have an application which accesses the web server which can be of Java or WebAPI with the token and the API is calling inhouse another api like this just to check whether the request came from the authentic source. The UserName and Password are magic numbers which can be checked at the db level. UserName and Password are passed using 64 encoding which can be easily encoded or decoded using ENCODE/DECODE TO BASE 64

So once the request will come from the UI and we will enforce the request to go through authentication middleware by calling the invoke method based on the credential passed user will be denied or allowed access to the API. If user passed the wrong credentials 401: UnAuthorized status code message will be sent.

await _next(app);

await _next(app); will call the next MVC middleware and get the response from the web api.

Once we are done with our Class let register it as Middleware in order to register the class as Middleware I will create another class which will be static class AuthorizationMiddleware

Now we are ready to test our middleware. So press F5 and run the application.

Testing the same using HTTP Client PostMan. I will use PostMan with I an Http Rest client in order to test my API and pass the valid credentials now. Web API is running on http://localhost:65116/api/values

Calling API Using Postman

Sample ASP.NET Core application

Architecture wise MVC 6 remains the same as MVC5 with other features like Tag helpers, view Component etc added. In order to create your simple ASP.NET Core application, you need to add some dependencies. As we discussed that we won’t get anything on our own we need to download it. So we will now download MVC dependency and EF and SqlServer dependency in order to communicate with SqlServer.

I have added my templates of controllers, Models, Views, Persistence(EntityFrameworkMapping)

Now, lets run our first basic ASP.NET CORE WEB APPLICATION which will store the Attendee details to the database and show the same in another page.

Dependency Injection in ASP.NET Core

Dependency Injection is a technique which helps us to inject dependent objects of a class. In order to learn the basic of Dependency i would recommend you to watch the video of shiv prasad Koirala sir on Dependency Injection

Let start with Dependency Injection in ASP.NET Core. ASP.NET Core provide us with build in Dependency Injection framework which was lacking in the previous version of MVC you can still use third party tool like NInject, AutoFac, Unity etc to do the same.

ASP.NET CORE provide us three type of Injections

AddSingleton

AddTransient

AddScoped

AddSingleton

When we use AddSingleton type the framework will create the single instance of the object of a class threw out the lifetime of the application.

AddTransient

AddTransient will create the new instance each time the request comes to the web application.

AddScoped

I found this to be extremely difficult to convey this DI but defining it I would say it will return the same of the for the class referred in i.e. a class referred in Service class or repository etc. We will see this in the demo.

Before explaining the demo I would like to thank Matthew Jones who has taken out his time and shown the ASP.NET CORE DI Demo quite easily which was not properly demonstrated in ASP.NET CORE Website, I would highly recommend you to follow him and read his awesome blogs to be updated. DI by

I hope you have read the blog and download the source code from the Github and I am sure all your doubts who have been cleared as off now. If not feel free to reach me.

Deploying ASP.NET Core application using VsCode command line

The asp.net core is cloud ready framework which can be deployed cross platform. So in this context, we will be deploying the same to IIS. You might be thinking that will we be able to use the blown power of IIS, then my honest answer will be no. IIS with ASP.NET CORE work as a proxy server whose role is to pass all the HTTP request to kestrel. As Microsoft team has said that kestrel not being matured enough should be used behind the IIS. So let’s publish our ASP.NET Core application to IIS.

Now the application has been published we can see all the published DLLS and files.

We have the folder called Runtime which contains all the respective for our cross-platform deployment libraries.

When you publish the application you will find that there is no web.config as we use to have in asp.net application. Now in order to deploy the application on IIS, we still need the web.config file. So inorder to generate the same we need to add the dependency in tool section and scripts as shown below:

So now in order to deploy the application on IIS. We need to add the new application to the default website as shown below:.

Conclusion

I want to thank all the attendee and viewers who have already watched this video, thanks to people who has replied with feedback where I was lacking. I will make sure to improve on that bullet point. I hope this Summary article will be helpful who want to start with.NET CORE and ASP.NET CORE .