Troubleshooting application problems for Applications running on Azure

Troubleshooting application problems are difficult. It takes a lot of time. I would argue that it might be the thing that we developers spend most of our time. When your application is in production, it is even more difficult to find out what went wrong.

Traditionally, you could RDP into the server and open up the app logs, IIS logs or look at the event logs to get a hint of what went wrong. But where can you find that information if you are running your app in the cloud? Things are different in the cloud – there may not be a server to log into.

I recommend read more details on Azure log types and how to activate and use them effectively at

Log files are useful and even when you are running in Azure, you have plenty of options to get information from log files.

It is difficult to get information from log files as you need to aggregate them and somehow analyze them. These are difficult problems that will slow you down when you are bug-hunting an issue in production.

I recommend using tools that visualise the information that is contained in your Azure logs. You do not have to enable the logs for this specifically as most of these tools capture this information automatically. Tools like “Application Insights” enable you to get an overview of the health of all your applications, including information that is contained in the log files and more. Using tools like these also enable you to be notified of exceptions so that you can go bug-hunting proactively.

Application Insights

Application Insights is an extensible Application Performance Management (APM) service for web developers on multiple platforms. Use it to monitor your live web application. It will automatically detect performance anomalies. It includes powerful analytics tools to help you diagnose issues and to understand what users actually do with your app. It’s designed to help you continuously improve performance and usability.

Read more…

What you can do with Application Insights?

16 Things Every Developer Needs to Know About Application Insights

Application Insights is Microsoft’s lightweight application performance monitoring service. I have collected a nice list of things that every developer should know, including tips, key features, and limitations.

Read more…

Structured logs with Serilog using Application Insights

Serilog is library to gather structured logs from our application. We can log both exceptions and other events that happened somewhere. I really like it because it’s simple and I can easily serialize and include some objects (ie. method arguments) into logs and easily browse and query them.

Read more…

I hope this will help !!!

.NET Core 2.1 – Use HttpClientFactory to implement resilient HTTP requests

The original and well-know HttpClient class can be easily used, but in some cases, it is not being properly used by many developers.

As a first issue, while this class is disposable, using it with the using statement is not the best choice because even when you dispose HttpClient object, the underlying socket is not immediately released and can cause a serious issue named ‘sockets exhaustion’.

Therefore, HttpClient is intended to be instantiated once and reused throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. That issue will result in SocketException errors. Possible approaches to solve that problem are based on the creation of the HttpClient object as singleton or static.

But there’s a second issue with HttpClient that you can have when you use it as singleton or static object. In this case, a singleton or static HttpClient doesn’t respect DNS changes.

To address those mentioned issues and make the management of HttpClient instances easier, .NET Core 2.1 offers a new HttpClientFactory that can also be used to implement resilient HTTP calls by integrating Polly with it.

Read more…

I hope this will help !!!!


AWS API Gateway – API Creation with Lambda Proxy Integration and Web Page Redirection (302) using .NET Core 2.0

In this article, we will see how to create and test an API with Lambda proxy integration using the API Gateway console. We will also see how a Lambda backend parses the query string request and implements app logic that depends on the incoming query string parameters. we will also see how to create 302 Redirect response using Lambda function and redirect caller directly to web page.

Topic to cover in this article

  1. How to create Lambda function for Lambda Proxy Integration using .NET core 2.0?
  2. How to create Resource?
  3. How to create HTTP GET Method with Lambda Proxy Integration?
  4. How to setup Request query string parameter?
  5. How to setup Response redirect (HttpStatus=302) with Location header parameter?

Step-1: Create Lambda function for Lambda Proxy Integration using .NET core 2.0

Create New AWS Lambda Project “ApiLambdaVerifyEmail” using Visual Studio 2017 version 15.5.6:


Add following NuGet Packages for Lambda function which will be needed for API Gateway Proxy Request and Response and other Lambda features:


Update Function Handler Code with below code snippets:

public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest input, ILambdaContext context)
    //Ready query string parameter
    string queryString ;
    input.QueryStringParameters.TryGetValue("pageKey", out queryString);

    //Set Default URL if no match found
    string redirectUrl = @"";

        Console.WriteLine("pageKey : " + queryString);

    //Use Query String Parameters to do some DB Operation 

    //Based on database operation redirect page to X Web Page or Y Web Page or Z Web Page
    switch (queryString)
        case "google":
            redirectUrl = @"";
        case "twitter":
            redirectUrl = @"";
        case "sandeep":
            redirectUrl = @"";

    Console.WriteLine("URL : " + redirectUrl);

    //Redirect to Web Page using 302 Response Code and URL
    var response = new APIGatewayProxyResponse
        StatusCode = (int)HttpStatusCode.Redirect,
        Headers = new Dictionary { { "location", redirectUrl } }

    return response;


What above code does is:

  • Read query string parameters – pageKey
  • Based on query string parameter values – set redirect URLfor the response.
  • Create APIGatewayProxyResponse object with page redirection (302) Status code and add header location for the page where to redirect.

Publish Lambda function by right clicking Project on solution explorer


Next, It will display Upload Lambda function popup where you can select/define your profile, region and Lambda function name and press Next button:


Next, It will display popup to select permissions for Lambda function you are uploading and once you select Role then press Upload button:


Note: Create Role for Lambda function if you haven’t created yet using IAM console as per your Lambda function Access Requirements. For this article we are not accessing any other AWS Service from Lambda function so no role as such required.

Next step is to create API Gateway API with Lambda Proxy Integration.

Step-2: Create Resource – TestRedirectPage :

How to setup Resource guide from AWS?


Step-3: Setup GET Method with Lambda Proxy Integration: 

How to setup HTTP Method guide from AWS?


3-Add permission to lambda function-confirmation

After GET Method configuration is saved it will show all request / response configuration on screen:


Step-4: Setup Query String Parameters for API Gateway – pageKey:


Verify GET Integration Request Type is LAMBDA_PROXY:


Step-5: Setup GET Method Response redirect (HttpStatus=302) with Location header parameter:

When setting up the API Gateway Method Response, start by declaring the status code in the Method Response section. 200 will already be there so delete it. Add your 302 here instead and add the Location response header.


After above all configurations are done, Method Execution will look like:


Test GET Method we configured by clicking Test link.

  • Set Query Strings textbox “pageKey=sandeep” and press test button.


Deploy API

  • Select GET method
  • then click on Actions button on top
  • then select deploy api option
  • select/create Dev stage and press Deploy button


Get Invoke URL from Dev environment

  • select GET method of testredirectpage resource
  • then it will display invoke URL
  • by using this URL we can hit API gateway resource and execute method from browser or Postman.


Run this Invoke URL in Browsser by Adding Query String pageKey=sandeep


Browser will invoke GET API method of testredirectpage resource on API gateway and then run our lambda function endpoint, then lambda function will parse this query string value, then send response to redirect Url= , then browser will redirect page.


Similarly, you can test with other Query String parameters and see which web page opens 🙂

I hope this is useful. 

Important Facts of Asynchronous Programming in .NET

Modern apps make extensive use of file and networking I/O. I/O APIs traditionally block by default, resulting in poor user experiences and hardware utilization unless you want to learn and use challenging patterns.

Task-based async APIs and the language-level asynchronous programming model invert this model, making async execution the default with few new concepts to learn.

Async code has the following characteristics:

  • Handles more server requests by yielding threads to handle more requests while waiting for I/O requests to return.
  • Enables UIs to be more responsive by yielding threads to UI interaction while waiting for I/O requests and by transitioning long-running work to other CPU cores.
  • Many of the newer .NET APIs are asynchronous.
  • It’s easy to write async code in .NET

One of the main advantages of using asynchronous methods is with I/O-based code.

By doing an await, you can let a thread be reused to do other work while the I/O is in flight.

The biggest misunderstanding about Asynchronous programming in many developers is asynchronous method automatically spawns a new thread, and that is not the case.

Recent improvements Microsoft has made towards Asynchronous programming is “Generalized Async Return Types

This means that you’re no longer limited to using Task or Task<T> as the return type for an asynchronous method. As long as the return type satisfies the asynchronous pattern, you’re good. Using this new ValueTask, you can avoid memory allocations, which can help in addressing performance issues.

Top Nuget Packages that Microsoft recommend for asynchronous programming are “System.Collections.Concurrent” and “System.Collections.Immutable“.

System.Collections.Immutable provides collections that allow a developer to use a collection (e.g. ConcurrentBag or ConcurrentDictionary) in a concurrent fashion safely. Therefore, the developer doesn’t need to do their own locking mechanisms to use the collection. Immutable collections allow developers to share collections safely because updates are only seen by the code that made the update.

Avoid void as a return type for asynchronous methods at all costs.

The only time it’s valid to do this is with an event handler. Otherwise, asynchronous methods should always return a Task type.

Immutable structures are very important when working with concurrent programming.

The main advantage with immutable data types is that you can share them across multiple tasks without worrying about synchronizing their contents. By their definition, they’re immutable, so if a task decides to “change” an immutable object, it will get a new version of that object. If another task has a reference to the original immutable object, it doesn’t change.

Take advantage of C# features to write immutable structures.

This means defining fields with the readonly keyword and properties that only have getters (not even private setters). Also, C# now allows you to specify a method parameter with the in keyword, which means that reference cannot be changed.

That’s all for now. I hope these facts will help you while doing Asynchronous Programming for your projects 🙂

Amazon Simple Queue Service – SQS

Amazon Simple Queue Service (SQS) is a distributed queue system that enables web services to quickly and reliably queue messages that one component in the application generates to be consumed by another component. A queue is a temporary repository for messages that await processing.


Below sample architecture we are going to use to understand how SQS works. There are two SQS Queue created, one for Request and another for Response.

Web Server who act as producer, who creates a message and puts it into request queue. it also read from response queue as well.

You can have multiple producers who can add multiple messages to the queue at the same time. You don’t have to worry about the traffic or peaks. SQS handles that for you.


Queued messages are processed by consumer. The consumer is requesting new messages periodically from the queue. You can have multiple consumers, but you have to remember that each message can be processed only once. It means that you can have multiple instances of the same consumer, but you can’t read the same message from one queue in two different components. Each of these components should use a separate SQS queue.

After a consumer processes the message, it has to be deleted from the queue. Deleting is important because SQS assumes that processing can fail. To prevent that, after the consumer receives a message, it is hidden from the queue for a defined period of time and after that, if it is not deleted, the message shows up in the queue again.


Two specific features of Amazon SQS make this possible:

  • A single Amazon SQS queue can be shared by multiple servers simultaneously.
  • A server that is processing a message can prevent other servers from processing the same message at the same time by temporarily “locking” a message. The server can specify the amount of time the message is locked. When the server is done processing the message, it should delete the message. If the server fails while processing the message, another server can get the message after the lockout period.

These two features ensure that the number of processing servers can be easily changed dynamically to handle varying load. The entire process can be automated to ensure that at any given time the optimal number of EC2 instances is running. This practice is commonly referred to as “auto-scaling.”


Standard Queue

  • Available in all regions.
  • Unlimited Throughput – Standard queues support a nearly unlimited number of transactions per second (TPS) per API action.
  • At-Least-Once Delivery – A message is delivered at least once, but occasionally more than one copy of a message is delivered.
  • Best-Effort Ordering – Occasionally, messages might be delivered in an order different from which they were sent.
  • When to use? – Send data between applications when the throughput is important, for example:
    • Decouple live user requests from intensive background work: let users upload media while resizing or encoding it.
    • Allocate tasks to multiple worker nodes: process a high number of credit card validation requests.
    • Batch messages for future processing: schedule multiple entries to be added to a database.

FIFO Queue

  • Available in the US East (N. Virginia), US East (Ohio), US West (Oregon), and EU (Ireland) regions.
  • High Throughput – FIFO queues support up to 300 messages per second (300 send, receive, or delete operations per second). When you batch 10 messages per operation (maximum), FIFO queues can support up to 3,000 messages per second. To request a limit increase, file a support request.
  • Exactly-Once Processing – A message is delivered once and remains available until a consumer processes and deletes it. Duplicates aren’t introduced into the queue.
  • First-In-First-Out Delivery – The order in which messages are sent and received is strictly preserved.
  • When to use – Send data between applications when the order of events is important, for example:
    • Ensure that user-entered commands are executed in the right order.
    • Display the correct product price by sending price modifications in the right order.
    • Prevent a student from enrolling in a course before registering for an account.


  • File processing – image scaling, video re-compression
  • Asynchronous communication with external services
  • Sending emails
  • Order Processing Application.


I followed these steps to create an IAM user for authentication in SQS and then create a queue with that user having full access.

This is a very simple application that posts a message to a queue, and receives one, just by passing the credentials in the code. I have noticed that AWS nicely handles duplicates so if you send the same message multiple times, it only seems to show up once.


//the url for our queue
var queueUrl = "[USERID]/[QUEUENAME]";

Console.WriteLine("Queue Test Starting!");

Console.WriteLine("Creating Client and request");

//Create some Credentials with our IAM user
var awsCreds = new BasicAWSCredentials("[ACCESSKEY]", "[SECRETKEY]");

//Create a client to talk to SQS
var amazonSQSClient = new AmazonSQSClient(awsCreds, Amazon.RegionEndpoint.EUWest1);

//Create the request to send
var sendRequest = new SendMessageRequest();
sendRequest.QueueUrl = queueUrl;
sendRequest.MessageBody = "{ 'message' : 'hello world' }";

//Send the message to the queue and wait for the result
Console.WriteLine("Sending Message");
var sendMessageResponse = amazonSQSClient.SendMessageAsync(sendRequest).Result;

Console.WriteLine("Receiving Message");

//Create a receive requesdt to see if there are any messages on the queue
var receiveMessageRequest = new ReceiveMessageRequest();
receiveMessageRequest.QueueUrl = queueUrl;

//Send the receive request and wait for the response
var response = amazonSQSClient.ReceiveMessageAsync(receiveMessageRequest).Result;

//If we have any messages available
    foreach(var message in response.Messages)
        //Spit it out

        //Remove it from the queue as we don't want to see it again
        var deleteMessageRequest = new DeleteMessageRequest();
        deleteMessageRequest.QueueUrl = queueUrl;
        deleteMessageRequest.ReceiptHandle = message.ReceiptHandle;

        var result = amazonSQSClient.DeleteMessageAsync(deleteMessageRequest).Result;

For more details, please refer “How Amazon SQS Queues Work” page. I have read documentation provided by amazon for SQS and believe me its really useful and all the things are very well documented.

I hope this will help !!!!