affiliate_link

Tuesday, March 13, 2018

Multithreading locking different ways?


The lock statement is analogous to Monitor.Enter, which does potentially block. Granted, in your example code, there shouldn't be any blocking issues; but I would wager that since lock provides blocking, it does a little more work (potentially) than TryEnter does.

1. Lock
2. Monitor.Enter
3. Monitor.TryEnter

100 iterations:
lock: 4.4 microseconds
Monitor.TryEnter: 16.1 microseconds
Monitor.Enter: 3.9 microseconds
100000 iterations:
lock: 2872.5 microseconds
Monitor.TryEnter: 5226.6 microseconds
Monitor.Enter: 2432.9 microseconds
This seriously undermines my initial guess and shows that, on my system, lock (which performs about the same as Monitor.Enter) actually drastically outperforms Monitor.TryEnter.

Monday, March 12, 2018

Difference between WCF and Web API?

The .Net framework has a number of technologies that allow you to create HTTP services such as Web Service, WCF and now Web API. There are a lot of articles over the internet which may describe to whom you should use.

Web Service

  • It is based on SOAP and return data in XML form.
  • It support only HTTP protocol.
  • It is not open source but can be consumed by any client that understands xml.
  • It can be hosted only on IIS.

WCF

  • It is also based on SOAP and return data in XML form.
  • It is the evolution of the web service(ASMX) and support various protocols like TCP, HTTP, HTTPS, Named Pipes, MSMQ.
  • The main issue with WCF is, its tedious and extensive configuration.
  • It is not open source but can be consumed by any client that understands xml.
  • It can be hosted with in the applicaion or on IIS or using window service.

WCF Rest

  • To use WCF as WCF Rest service you have to enable webHttpBindings.
  • It support HTTP GET and POST verbs by [WebGet] and [WebInvoke] attributes respectively.
  • To enable other HTTP verbs you have to do some configuration in IIS to accept request of that particular verb on .svc files
  • Passing data through parameters using a WebGet needs configuration. The UriTemplate must be specified.
  • It support XML, JSON and ATOM data format.

Web API

  • This is the new framework for building HTTP services with easy and simple way.
  • Web API is open source an ideal platform for building REST-ful services over the .NET Framework.
  • Unlike WCF Rest service, it use the full feature of HTTP (like URIs, request/response headers, caching, versioning, various content formats)
  • It also supports the MVC features such as routing, controllers, action results, filter, model binders, IOC container or dependency injection, unit testing that makes it more simple and robust.
  • It can be hosted with in the application or on IIS.
  • It is light weight architecture and good for devices which have limited bandwidth like smart phones.
  • Responses are formatted by Web API’s MediaTypeFormatter into JSON, XML or whatever format you want to add as a MediaTypeFormatter.

To whom choose between WCF or WEB API

  • Choose WCF when you want to create a service that should support special scenarios such as one way messaging, message queues, duplex communication etc.
  • Choose WCF when you want to create a service that can use fast transport channels when available, such as TCP, Named Pipes, or maybe even UDP (in WCF 4.5), and you also want to support HTTP when all other transport channels are unavailable.
  • Choose Web API when you want to create a resource-oriented services over HTTP that can use the full features of HTTP (like URIs, request/response headers, caching, versioning, various content formats).
  • Choose Web API when you want to expose your service to a broad range of clients including browsers, mobiles, iphone and tablets.

What is Action Delegate?

Microsoft introduced some pre-built delegates so that we don't have to declare delegates every time. Action is one of the pre-built delegates.

Action in C# represents a delegate that has void return type and optional parameters. There are two variants of Action delegate.
  1. Action
  2. Action<in T>

Non-Generic Action Delegate

First variant is non-generic delegate that takes no parameters and has void return type. In this Action delegate, we can store only those methods which has no parameters and void return type.
 








static void Main(string[] args)
{
    Action doWorkAction = new Action(DoWork);
    doWorkAction(); //Print "Hi, I am doing work."
}
 
public static void DoWork()
{
    Console.WriteLine("Hi, I am doing work.");
}
 
We can also store method in the Action delegate with direct initializing with the method. Below is the example.










static void Main(string[] args)
{
    Action doWorkAction = DoWork;
    doWorkAction(); //Print "Hi, I am doing work."
}
 
public static void DoWork()
{
    Console.WriteLine("Hi, I am doing work.");
}

Generic Action Delegate

We can also store method in the Action delegate with direct initializing with the method.

We have to choose Action delegate according to the method, which we want to store. If our method takes two parameters, then we have to choose action delegate which has two parameters Action<in T1, T2>(T1 arg1, T2 arg2). Below are some examples


static void Main(string[] args)
{
    Action<int> firstAction = DoWorkWithOneParameter;
    Action<int, int> secondAction = DoWorkWithTwoParameters;
    Action<int, int, int> thirdAction = DoWorkWithThreeParameters;
 
    firstAction(1); // Print 1
    secondAction(1, 2); // Print 1-2
    thirdAction(1, 2, 3); //Print 1-2-3
}
 
public static void DoWorkWithOneParameter(int arg)
{
    Console.WriteLine(arg);
}
 
public static void DoWorkWithTwoParameters(int arg1, int arg2)
{
    Console.WriteLine(arg1 + "-" + arg2);
}
 
public static void DoWorkWithThreeParameters(int arg1, int arg2, int arg3)
{
    Console.WriteLine(arg1 + "-" + arg2 + "-" + arg3);
}

Action with Lambda Expression

We can use Lambda expression with Action delegate. Below is the example.





















static void Main(string[] args)
{
    Action act = () =>
    {
        Console.WriteLine("No Parameter");
    };
 
    Action<int> actWithOneParameter = (arg1) =>
        {
            Console.WriteLine("Par: " + arg1);
        };
 
    Action<int, int> actWithTwoParameter = (arg1, arg2) =>
        {
            Console.WriteLine("Par1: " + arg1 + ", Par2: " + arg2);
        };
 
    act();
    actWithOneParameter(1);
    actWithTwoParameter(1, 2);
}