In my current company we are going to start with Web API’s so I just thought of doing some reading and figure out questions for below answers.

Difference between WCF and ASP.Net Web API?
Why not SOAP? – it is also XML based.
Is WCF dead? If not, when to use WCF or ASP.Net Web API?

Difference between WCF and ASP.Net Web API?
  • WCF was conceived back in its Indigo and .NET 3 days, the main goal was to support/exchange SOAP-based messages using variety of transport protocols like HTTP, TCP, NamedPipes or MSMQ etc.
  • ASP.NET API is a framework for building non-SOAP based services over HTTP only.

Points of interest

  • In .NET 3.5, WCF got a new binding, “WebHttpBinding” — a new binding that helped creating non-SOAP service over HTTP, better known as a RESTful service. (WCF Rest)
    • The WebHttpBinding was not enough, and after WCF 3.5 was released, a new set of tools was created – the WCF REST Starter Kit.
    • REST starter kit was an attempt to enrich the support of WCF 3.5 for HTTP services
      • Add better client-side support for .NET apps
      • Extend the server side support for other content types
      • Enable response and request caching
      • Inspection of messages and so forth.
    • Unfortunately, this great toolkit was never officially released
  • In .NET 4, Some of the server side features of REST Starter Kit was integrated in WCF 4 but we didn’t get any of the client-side libraries.
    • We did get most of the service-side features (excluding the new inspectors). Some were well-integrated into WCF while others required the use of ASP.NET (by turning on the ASP.NET compatibility mode).

So with WCF 4 we had some support for “Web” HTTP services, but it wasn’t that perfect – to get some of the features you needed IIS hosting and ASP.NET, not all types of requests were supported easily.

Why not SOAP? – it is also XML based.
  • SOAP allows us to place all the knowledge required by our service in the message itself, disregarding its transport protocol, whether it is TCP, HTTP, UDP, PGM, Named Pipes etc
  • HTTP is an application-level protocol, and as such it offers a wide variety of features:
    • It supports verbs that define the action, GET, POST/PUT, DELETE
    • Message headers that are very meaningful and descriptive – headers that suggest the content type of the message’s body, headers that explain how to cache information, how to secure it etc.
    • Message body that can be used for any type of content, not just XML content as SOAP enforces (and if you want something else – encode it to base64 strings and place it in the SOAP’s XML content).
      • The body of HTTP messages can be anything you want – HTML, plain XML, JSON, binary files (images, videos, documents…)

Points of interest

  • The use of HTTP has evolved over the years. Application-level protocol architectural styles such as Hypermedia APIs or this link have emerged on top of HTTP.
    • These, in turn, harness the power of HTTP to create resource-oriented services, and better define the stateless interaction between clients and services

SOAP is still useful for building messages when you don’t have an alternative application-level protocol at your disposal, or when you want to use SOAP across the board while considering HTTP as no more than another way to pass messages (for example, use HTTP because it can cross firewalls more easily than TCP).

Is WCF dead? If not, when to use WCF or ASP.Net Web API?
  • ASP.NET Web API framework is not going to replace WCF. It’s just another way of building services that are non-SOAP based e.g. plain XML, JSON string etc. – This mean WCF certification still holds value J
  • WCF is still the best choice for
    • Messag Queuing scenario using MSMQ.
    • One-way communication or Duplex communication
    • If we intended to use transport other than HTTP e.g. TCP, UDP or Named Pipes.

Above information is best as per knowledge and readings. My understanding might be wrong at few places.


Collection was modified; enumeration operation might not execute

“Collection was modified; enumeration operation might not execute”  this common error developer faces every time he is updating(add/delete/sort) the collection which is being enumerated.

The question is why this happens, it understandable that Deleting and Sorting might change the way index might have sorted into but Adding should work fine – if there is no sort defined.

All this is related to the way MS has implemented enumeration for its classes.

Take List<T> as an example.

In List, there is an internal variable by name “version” which is incremented on any kind of operation done on list, this includes Add, Delete, Sort. Now when enumerator object is requested from List, internally list instantiate a struct (Enumerator) which implements IEnumerator.

Initialization of struct enumerator requires list as its mandatory parameter and locally notes down current “version” of list You can think struct enumerator as Linked-list implementation using arrays.

“MoveNext()” API call, It first checks the version noted by enumerator and list matches, secondly gives the reference of next element in the array/list.

Whenever mismatch happens exception is thrown.

All above is said about MS implementation. If you create custom collection class and override GetEnumerator function you won’t receive the error.

Implicit vs explicit interface implementation

Whenever we need to implement an interface in C#, two options pops up (click Ctrl+’.’).

  • Implicit interface implementation 
  • Explicit interface implementation

What shall be selected? before making a decision, lets understand what is the difference between those two.

Solving the diamond problem
The diamond problem is related to object oriented languages that allow multiple inheritance. The problem raises when there is base class A, classes B and C derived from A and class D derived from B and C. B and C, both have a method “Foo”, that has been overridden differently. If this method is called in D, then via which class is it inherited: B or C?

As we know C# does not support multiple inheritance of classes, but it does support multiple inheritance of interfaces. Therefore, the same ambiguity may arise unless explicit interface implementation is used. Going explicitly, we can have different implementations to the same method. The problem is solved…

interface IRegularAccess
    void ApproveLoan();

interface IAdministratorAccess
    void ApproveLoan();

class BankDatabaseProxy : IRegularAccess, IAdministratorAccess
    void IRegularAccess.ApproveLoan()
        // Code for regular access approve loan.

    void IAdministratorAccess.ApproveLoan()
        // Code for administrator access approve loan.

A good example of such a usage is a bank, where one class implements 2 interfaces to the data-base proxy: IRegularAccess and IAdministratorAccess.It is clear that the ApproveLoan method shall not be implemented in the same manner for both cases.
Private and Public issues:

In implicit implementation, the public keyword is attached to the method (otherwise, compilation error). It is the opposite with explicit implementation (where the private keyword is used). This is how the implicit implementation looks with our example:

class BankDatabaseProxy : IRegularAccess, IAdministratorAccess
     public void ApproveLoan()
         // Insert your code.

The result is that the explicit implementation keeps the interface members out of the class (and out of the Intellisense too). Again, it is much easier to demonstrate this issue in code:

// Does not compile because of explicit implementation.
BankDatabaseProxy bankDB = new BankDatabaseProxy();

// This is good.
IAdministratorAccess adminAccessToBankDB = new BankDatabaseProxy();

No Virtual for Explicit:
Explicit method implementation can not be virtual, whereas an implicit method can. Child classes won’t be able to override an explicit method.

No Abstract for Explicit:
Explicit method implementation can not be abstract, whereas an implicit method can. Class that use the explicit implementation can not be abstract!

Build and Deploy a .NET COM Assembly

This article shows how to build a .NET COM assembly following the recommended best practices, and shows how to deploy it with a Visual Studio Setup and Deployment Project. Article also shows you two types of client for the COM object (early binding and late binding) and discuss the advantages and disadvantages of each.


Is managed DLL binary compatible?

This is in continuation of the error discussed in my earlier blog .

While working with this error I accidently clicked on my main application without rebuilding and it worked although I had done changes to my referenced DLL.

Does it mean compiled DLL are binary compatible?

Lets see how it works,

Below image show my code
  • Referenced DLL (you can see I’ve commented the overloaded API ) 

  • My test application code  

Now I build my exe and read the generated IL code in C# using reflector
Below are the images:

  • When overloaded API is commented 


  • When overloaded API is uncommented and my application is rebuilt. 


These images reveal that compiler intelligently creates the syntax (in IL) according to the best available function.

Pretty interesting stuff!!

Learning: creating overloads of function with params type parameter

This is about mistake which happened couple of days ago. 
We had a function having declaration like:
protected string function1(string parameter1, params string[] parameterArray)
//some code

Due to some requirement to overload this API  we added below function 

protected string function1(string parameter1, string parameter2, params string[] parameterArray)
//some code

Now if we compile these two functions the project will be compiled with no errors and also the calling function will be compiled successfully.
But for the cases where code was calling function1 using below syntax may give some undesired results.

function1(“some string“, “some string1“, “some string2“, “some string3“);

Reason:  Now the call is getting diverted to best available match which is our newly added API.

We should be extra carful about creating overloading funcitons specially when current function is having any of the params type parameter.
For above discussed case best way is to create a new API as chaning old API may end up in breaking some calling codes.