Saturday, September 22, 2018

.Net Core | Project file


csproj file has been changed with .Net core

  1. Solution explorer is mapped with hard drive location, any file added in hard drive location will be visible in solution explorer, no entry required in .csproj file.
  2. Project file is used to contain packages references  
  3. Microsoft.AspNetCore.App contains
    • All required Core features
    • EntityFramework
    • Third party packages dependencies used by Core

.Net Core | Bundling & Minification

Bundling is a process to combine same type of multiple reference files into single file to minimize multiple files loading into one.
Minification is a process in which some techniques get applied over file like removing whitespace, shortening variable names, replacing verbose functions with shorter and more concise functions etc.

  • Add a json file bundleconfig.json.
    • OutputFileName: File that will be created after bundling and minification of given input files.
    • inputFiles: One or more files to be bundled and minified into one output file.




  • If not using Visual studio dotnet bundle is the command that can be used on command line to perform bundling.
  • Using Visual studio  
  1. Install BundlerMinifier.Core from https://marketplace.visualstudio.com/items?itemName=MadsKristensen.BundlerMinifier
  2. Use Task Runner Explorer  
    Bottom part of above given picture is showing Task Runner Explorer.
  3. All files or output files can be configured for Bundling and Minification on below events, bundleconfig.json.bindings file contains this information. On these events output files get created
    • Before Build
    • After Build 
    • Clean Build
    • Project Open

Note: Gulp and Grunt are java script based tools that can be used for bundling and minification easily.

Sunday, September 2, 2018

.Net Core | Dependency Injection (DI/Ioc)

Dependency Injection(DI) & Inversion of Control(Ioc)
  • ConfigureServices is the method used to register Types/ Services in Ioc container.
  • Classes of application (even view using Razor) can call these services whenever required.
  • If a class uses any Ioc registered service instance, its required to implement interface of that service in the class.
  • Lifetime of registered service instances is managed by runtime(framework). Because you already mention the same while registering a service.

Ex.

public void ConfigureServices(IServiceCollection services)  
{  
        services.AddSingleton<IEmailSender, EmailSender>();
        services.AddMvc();  
services.AddSingleton<InterfaceName, className>();
services.AddScoped<InterfaceName, className>();
services.AddTransient<InterfaceName, className>();


Benefits: 
  • Required services instance can be used throughout the application.
  • No need to create and dispose services instances when calling them.
  • Lifetime of services instances managed by Ioc container, that is called by runtime.

.Net Core | Initialization of Applications

Initialization of .Net core Web Application
  1. No Global.asax
  2. Web application and other application starts from program class contains main method


  3. main method calls another static method CreateWebHostBuilder method that returns object of IWebHostBuilder interface.
  4. CreateWebHostBuilder calls CreateDefaultBuilder static method. 
  5. That creates object of WebHostBuilder setup it and run it.


    • Created object 'builder' of WebHostbuilder
    • UseKestral(): Tells builder to use Kestrel.
      Kestrel
      is embedded internal server that host application and communicate (Request/Response) with external web server like IIS.
    • UseIISIntegration():  Tells to use IIS and communication between IIS and Kestrel.
    • UseContentRoot: Access project directory
    • ConfigureAppConfiguration: Loads appSettings.json file contains settings
    • ConfigureLogging: Configures logging in console window and debug window including in libraries used.
    • UseDefaultServiceProvider: Tells builder to use asp.net core's internal dependency injection container. 
    • ConfigureServices: Used to add types to the internal dependency injection container. Adding type Kestrel that is used.
    • Returns object of WebHostbuilder
  6. Object of WebHostBuilder configured to use Startup class.
  7. Startup Class: Contains 2 methods
    • ConfigureServices: Used to register services in Ioc container
    • Configure: Used to configure Request/Response pipeline
           public class Startup   
           {  
                  public void Configure(IApplicationBuilder app)  
                  {  
                     //Register types (services) to use in application
                  }  
                  public void ConfigureServices(IServiceCollection services)  
                 {  
                     //Configure how to handle http request 
                  }       
           }

.Net Core | Installation

Installation
Visual studio installation does not includes .Net Core, you need to download and install it from
https://www.dot.net

Templates available in Visual Studio for
  1. API
  2. Console App
  3. ASP.Net Core web application  
  4. ASP.Net Core web application(MVC)
  5. Angular
  6. React.js
Project file structure (.csproj or .vbproj)
  1. Includes wwwroot folder.
    • Only wwwroot folder is accessible from browser
    • Nothing is accessible by default 
    • Only files accessible included in white list
  2. Solution explorer directly maps local stored drive location. It means adding any file on hard drive shown directly in solution explorer.
  3. Includes .Net core framework version
  4. Includes Package/library references 
Meta Package 
  1. It includes every thing required in ASP.net core and Entity Framework.
  2. It also include internal and external dependencies

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...