Creating Serverless Microservices with AWS Lambda and ASP.NET Core


As you know Micro service is one alternative approach to write Web API method. There are so many ways to create the micro-service. But in this post i m going to write creating micro service using Amazon Web Service(AWS) Lambda ToolKit on Asp.net Core

what is the Serverless Microservices ?

Serverless microservices are deployed within a serverless vendor’s infrastructure and only run when they are needed by the application. Depending on the size of a microservice, it may also be broken up into even smaller functions.

It doesn’t means that there will be no server. There will be cloud vendor’s server.

AWS use the Lamda and API gateways for this functionalities.

What are the advantages of this approach ?

1. You don’t have to pay for idle server time. So it will save huge deployment cost
2. If you have small application like POC and you are going to use that application for specific time interval then this approach will good. It will save huge money.
3. On demand we can scale the application, since it is on cloud.
4. It is give good performance as compare to monolithic approach.

How to create Serverless microservice with AWS

Step 1: Install the AWS Toolkit Extension as given below

Step 2: Create the Web Api Serverless Project like this

This template will add the all the requited dll and required files for us to making serverless microservice

Step 3: Add some Empty API controller in Controller folder and write some test method like this



using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Net.Http;

namespace AWSServerless3.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        // GET: api/Test
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "This is from Test Controller" };
        }
        //external
        [HttpGet]
        [Route("external")]
        public string External()
        {
            try
            {
                HttpClient client = new HttpClient();
                var requestTask = client.GetStringAsync("http://www.chandradev819.wordpress.com");
                requestTask.Wait();
                return "external connectivity PASS";
            }
            catch (Exception e)
            {
                return "external connectivity FAIL";
            }
        }


    }
}

Add one more controller i.e EmpController



using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace AWSServerless3.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmpController : ControllerBase
    {
        // GET: api/Emp
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "This is from Emp Controller" };
        }
    }
}

Step 4; Build the application and run on local machine before deployment

Now We are good to deploy on AWS. Now we have to create the profile on

https://aws.amazon.com/

Configure AWS user credentials as given below post

https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/credentials.html

Step 5: After successfully configure, Right click on Solution explores and deploy the application like this

Now publish the application on AWS Cloud like this

After published we will get the URL like this

Now our services has been deployed as Server less microservice on AWS Cloud. We donot have to do any thing.

Now we are ready to use the microservice on our application

Summary

I hope in this small post you might have got clear idea about creating Serverless Microservice using AWS Lambda Toolkit in Asp.net Core application.

Advertisement

Implementing Swagger in Asp.net Web API Core 3.1 in clean way


What is the Swagger ?

This is one of the best tool for testing web api method. It will also give very good user friendly definition of web api method.

Advantages:
1. We can easily test web api method
2. We will get user friendly documented message about web api method
3. We can easily debug the web api method using this tool.

How to implement in Asp.net Web API Core 3.1 in Clean way ?

Step 1: Firstly create the web api 3.1 project like this

Creating Web Api Core 3.0 layer using Dapper and .net standard 2.0

Step 2: Go to Web API layer and install the swagger related nuget package

Swagger
Swagger

Note: if you are using asp.net core 3.0 or 3.1 then install the swagger version v5.0

Step 3: Create SwaggerExtension.cs file in the Extensions folder of We API Layer and write the Extension Method for IServiceCollection and IApplicationBuilder like this

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;

namespace Dapper_Demo.Extensions
{
    public static class SwaggerExtension
    {
        public static void AddSwagger(this IServiceCollection services)
        {
            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "EmpManagement API", Version = "v1" });
            });
        }

        public static void AddCustomSwagger(this IApplicationBuilder app)
        {
            //Adding Swagger and SwaggerUI
            app.UseSwagger();

            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "EmpManagement ASP.NET Core API v1");
            });
        }
    }
}

Step 4: Go to the Startup file of application and add the swagger in middleware and service configure like this

Swagger_Middleware
Swagger_Middleware

Step 5: Run the application and type this URL

If you want to download the source code for this demo. You can download from below demo.

https://github.com/Chandradev819/Dapper_Demo_With_Asp.netCore

Creating Web Api Core 3.0 layer using Dapper and .net standard 2.0


Recently I got a chance to work with dapper using .net standard 2.0 So i thought to write my learning with you in this small blog post.

Firstly we will know what is the dapper ?

Dapper is micro ORM(Object Relational Mapper) framework which helps to map database directly with C# object. It is developed by StackOverflow team and released as open source.

Advantages of Dapper

1. It is a high-performance data access system
2. It is database independent
3. Same code work with all relational database.
4. Fewer lines of code.
5. Easy Handling of SQL Query.
6. Easy Handling of Stored Procedure.
7. Dapper also allows fetching multiple data based on multiple inputs.
8. Get generic result for simple or complex data type.
9. ease of use
10. It provides support for both static and dynamic object binding using transactions, stored
procedures, or bulk inserts of data.

In this post I have also used .net standard 2.0, so we have to know what is the advantage of this

Advantages of .net standard 2.0

>> Same code can be shared on all type of .net application. This will be portable. This means you can write and can use in applications that run on multiple platforms. Their purpose is to share code between applications.

Now we will see how to implement in asp.net core web api

In this demo I m going to design the simple demo web api as given below simple architecture

Step 1: Now we will Create the blank empty web api project like this

Step 2: Right click on solution explorer and create the EmpManagement.Entities layers using .net standard 2.0 like this

Step 3: Follow the same steps and create the EmpManagement.Repository

Step 4: Follow the same steps and create the EmpManagement.Repository.Interfaces

Step 5: Create the table and sp in database like this

USE [EmpManagement]
GO
/****** Object:  StoredProcedure [dbo].[AddEmp]    Script Date: 12/12/2019 10:14:54 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[AddEmp]
   @EmpName nvarchar(50),  
    @EmpAddress nvarchar(50),  
    @EmailId nvarchar(50),  
    @MobileNum nvarchar(50)  
   
AS  
 
BEGIN  
SET NOCOUNT ON;  
insert into tblEmp(EmpName, EmpAddress, EmailId, MobileNum)  
      values(@EmpName, @EmpAddress, @EmailId, @MobileNum)  
END  

GO
/****** Object:  StoredProcedure [dbo].[DeleteEmp]    Script Date: 12/12/2019 10:14:54 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[DeleteEmp]
   @Id int
   
AS  
 
BEGIN  
SET NOCOUNT ON;  
DELETE from tblEmp where	Id=@Id 
END  

GO
/****** Object:  StoredProcedure [dbo].[GetAllEmps]    Script Date: 12/12/2019 10:14:54 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetAllEmps]
  
   
AS  
 
BEGIN  
SET NOCOUNT ON;  
SELECT * from TBLEMP
END  

GO
/****** Object:  StoredProcedure [dbo].[GetEmpById]    Script Date: 12/12/2019 10:14:54 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetEmpById]
  @Id int
   
AS  
 
BEGIN  
SET NOCOUNT ON;  
SELECT * from TBLEMP where Id=@Id
END  

GO
/****** Object:  StoredProcedure [dbo].[UpdateEmp]    Script Date: 12/12/2019 10:14:54 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[UpdateEmp]
  @Id int,
  @EmpName nvarchar(50),
  @EmpAddress nvarchar(50),
  @EmailId nvarchar(50),
  @MobileNum nvarchar(50)

AS  
BEGIN  
SET NOCOUNT ON;  
UPDATE tblEmp SET EmpName=@EmpName,EmpAddress=@EmpAddress,EmailId=@EmailId,MobileNum=@MobileNum where Id=@Id	 
END  

GO
/****** Object:  Table [dbo].[tblEmp]    Script Date: 12/12/2019 10:14:54 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tblEmp](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[EmpName] [nvarchar](50) NULL,
	[EmpAddress] [nvarchar](50) NULL,
	[EmailId] [nvarchar](50) NULL,
	[MobileNum] [nvarchar](50) NULL,
 CONSTRAINT [PK_tblEmp] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

Step 6: Go to EmpManagement.Entities layer and Create the Emp Entities class like this

using System;

namespace DataManagement.Entities
{
    public class Emp
    {
        public int Id { get; set; }
        public string EmpName { get; set; }
        public string EmpAddress { get; set; }
        public string EmailId { get; set; }
        public string MobileNum { get; set; }
    }
}

Step 7: Go to EmpManagement.Repository.Interfaces layer and Create the generic interface like this

using System.Collections.Generic;

namespace DataManagement.Repository.Interfaces
{

    public interface IEmpRepository<T> where T : class
    {
        IEnumerable<T> GetAllEmp();
        T GetEmpById(int id);
        void AddEmp(T entity);
        void DeleteEmp(int id);
        void UpdateEmp(T entity);
    }


}

Step 8: Create the BaseRepository.cs file in EmpManagement.Repository layer like this

using System;
using System.Data;
using System.Data.SqlClient;

namespace DataManagement.Repository
{
    public class BaseRepository : IDisposable
    {
        protected IDbConnection con;
        public BaseRepository()
        {
            string connectionString = "Data Source=localhost;Initial Catalog=EmpManagement;Integrated Security=True";
            con = new SqlConnection(connectionString);
        }
        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }
}

Step 9: Create the BaseRepository.cs file in EmpManagement.Repository layer like this

using Dapper;
using DataManagement.Entities;
using DataManagement.Repository;
using DataManagement.Repository.Interfaces;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace EmpManagement.Repository
{
    public  class EmpRepository<T> : BaseRepository, IEmpRepository<Emp>
    {
        public void AddEmp(Emp objEmp)
        {
            try
            {
                DynamicParameters parameters = new DynamicParameters();
                con.Open();
                parameters.Add("EmpName", objEmp.EmpName);
                parameters.Add("EmpAddress", objEmp.EmpAddress);
                parameters.Add("EmailId", objEmp.EmailId);
                parameters.Add("MobileNum", objEmp.MobileNum);
                SqlMapper.Execute(con, "AddEmp", param: parameters, commandType: CommandType.StoredProcedure);

                //For implementing commandtimeout and transaction
                // SqlMapper.Execute(con, "AddEmp", param: parameters, transaction:null, commandTimeout: 100, commandType: CommandType.StoredProcedure);
                // Other approach to excecute Storeprocedure in dapper
                // con.Execute("AddEmp", parameters, null, null, commandType: CommandType.StoredProcedure);

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void DeleteEmp(int Id)
        {
            try
            {
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("Id", Id);
                SqlMapper.Execute(con, "DeleteEmp", param: parameters, commandType: CommandType.StoredProcedure);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public IEnumerable<Emp> GetAllEmp()
        {
            try
            {
                return SqlMapper.Query<Emp>(con, "GetAllEmps", commandType: CommandType.StoredProcedure);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public Emp GetEmpById(int Id)
        {
            try
            {
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("Id", Id);
                return SqlMapper.Query<Emp>(con, "GetEmpById", parameters, commandType: CommandType.StoredProcedure).FirstOrDefault();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void UpdateEmp(Emp objEmp)
        {
            try
            {
                DynamicParameters parameters = new DynamicParameters();
                con.Open();
                parameters.Add("EmpName", objEmp.EmpName);
                parameters.Add("EmpAddress", objEmp.EmpAddress);
                parameters.Add("EmailId", objEmp.EmailId);
                parameters.Add("MobileNum", objEmp.MobileNum);
                parameters.Add("Id", objEmp.Id);
                SqlMapper.Execute(con, "UpdateEmp", param: parameters, commandType: CommandType.StoredProcedure);
            }
            catch (Exception)
            {

                throw;
            }

        }
    }
}

Step 10 :Create the Emp Controller in WebApi Layer as given below

using DataManagement.Entities;
using DataManagement.Repository.Interfaces;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace Dapper_Demo.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EmpController : ControllerBase
    {
      
        IEmpRepository<Emp> _empRepository;
        public EmpController(IEmpRepository<Emp> empRepository)

        {
            _empRepository = empRepository;
        }

        // GET: api/Emp
        [HttpGet]
        public IEnumerable<Emp> Get()
        {
            return _empRepository.GetAllEmp();
        }

        // GET: api/Emp/5
        [HttpGet("{id}", Name = "Get")]
        public Emp Get(int id)
        {
            return _empRepository.GetEmpById(id);
        }

        // POST: api/Emp
        [HttpPost]
        public void Post([FromBody] Emp emp)
        {
            _empRepository.AddEmp(emp);
        }

        // PUT: api/Emp/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] Emp emp)
        {
            _empRepository.UpdateEmp(emp);
        }

        // DELETE: api/ApiWithActions/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
            _empRepository.DeleteEmp(id);
        }
    }
}

Step 11: Register the interface in Startup.cs file like this

Step 12: Now run the application you will see the output like this

Summary:

In this post we learnt that how to create the web api core standalone layer using dapper and .net standard 2.0.
You can download the working code from this github repo

Source code download from github

How to get Soap HTTP Request and Response message without debugging the code ?


Today while working on the bug fixing in my application. There was the requirement to give Soap HTTP Request and Response message one wcf service to other team with in quick time interval. That wcf was external service. So we could not step in for further debugging process.

WordPress.com

For this task i took the help of “Telerik Fiddler” tool for getting HTTP Request and Response message.

In this demo, i will take simple WCF service and show you how to track request and response without doing debugging process.

Step 1: Install the “Telerik Fiddler” tool.

Step 2: Run the Telerik Fiddler tool.

Step 3: Run the application on localhost.

WCF Debugging
WCF Debugging

Step 4: See the tracing in fiddler like this

Telerik Fiddler
Telerik Fiddler

Summary:

This is handy tool for the developer to quickly get the http request and response message from service without debugging the complete source code.

WordPress.com

How to handle page not found exception message in asp.net MVC 5.0 in user friendly ways


WordPress.com

If we have asp.net mvc 5.0 application and we are searching some different page on browser then we will get the exception message.

“The resource cannot be found” I.e HTTP 404 status code.

To display this error message as user friendly we can install the “NotFoundMVC” Nuget package in Visual studio like this

NotFound

Now go to the web config file and delete the this line of code. This is the problem in this NuGet package, which is creating some invalid code in web config file.

WebConfig

Now this package will create the Not found page for you.

This will also add the required code in web config file like this

Now run the application with wrong controller/Action name. It will give user friendly error message like this

Limitation of Control
This control does not handle the other error message. It will handle on status code 404. For other error we have to implement separate error handle approach.

Summary:
I hope this post will help to implement the user friendly error message for http status code 404.

WordPress.com