Hangfire

What is Hangfire? The Hangfire is an open-source .net package. It allows you to run scheduled jobs with limited amount of administrative cost. Just like Windows Scheduler, Windows Service deployment is not easy, but with the help of Hangfire you can run different types of background jobs such as one …

Setting HttpContext Response using HttpResponseMessage or Request.CreateResponse in WebApi2

Background In my recent Continuous Improvement (CI) initiative I have been introducing few ActionFilters for the WebApi Controllers. These action filters validates the request by using payload or the current user token against some business logics. If the request is not fulfilling business requirements then the filter should stop further processing and send …

Extension Methods in C#

Let say you want to extend a class that you cannot inherit. These classes might be defined as sealed or even a 3rd party DLL that you have just downloaded from nugget. How would you extend these classes? Let us take an example of struct types in C# that are …

Tips #1 camelCase Json from webApi

Problem:

I have came into the situation where I need to build webapi that returns value in camel case so that it is defined as per javascript writing notation standard.

 

Solution:

I have two options that either I define json property for each POCO ( Plain old CLR object).

 [JsonObject("todoItem")]
 public class TodoItem
 {

[JsonProperty("id")]
public int ID { get; set; }
 [JsonProperty("title")]
 public string Title { get; set; }

 [JsonProperty("isCompleted")]
 public bool Completed { get; set; }
 }

This way Json seialization woudl know how it should represent the json result.

The another solution by using Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver class.

Use this below snippet to configure the serialization format.

config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

I have wrote this line at WebApiConfig.cs file for WEB API project:

public static void Register(HttpConfiguration config)
 {
// configure serialize settings so that the response comes as camel case serialization.
 config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
 }