Why WCF returns array representation on wire by default

Hi Bloggers,

I just had a little time , so I thought to share a very small but a good concept of WCF with collections. Here we Go!!!

So you have some sort of service operation that uses any of the collection interfaces such as the IEnumerable<T> ,IList<T> and ICollection<T> ,the wire representation will always convert these collections into an array  .

Let’s say this is you’re WCF operation :

[ServiceContract]
interface IContactManager
{
   [OperationContract]
   IEnumerable<Contact> GetContacts( );
   ...
}
class ContactManager : IContactManager
{
   List<Contact> m_Contacts = new List<Contact>( );

   public IEnumerable<Contact> GetContacts( )
   {
      return m_Contacts;
   }
   ...
}

on the client , this will be exported as :

will be exported as:

[ServiceContract]
interface IContactManager
{
   [OperationContract]
   Contact[] GetContacts( );
}

WHY? Its very obvious because .NET collections are .NET-specific, WCF cannot expose them in the service metadata, WCF offers dedicated marshaling rules for collections .That is a good thing although , WCF  services are platform agnostic ,and they need to represent the most basic(primitive data )types .Imagine that how can you’re service be consumed if you’re client does not know anything about Generics ?

But if you are lucky enough and you’re client is also based on dot net , then you can convert that wired array representation into a generic type list .

For that right click on your project and select Add Service Reference as in the image :

As you can see , in the image there is an Advanced Button click it ,and select to convert it to a List instead of an array  as shown :

Adding Service ReferenceThat’s all to do , there you go presenting your client with a List as defined in your service.There also another interesting option of making your wcf operations called asynchronously , just by clicking the check box and updating the proxy you can see the difference that your service is now consumed in an asynch manner on a different thread .You can see the Reference.cs file , on updating the proxy when you checked the Asynchronous checkbox , it does a cool thing for you by adding some Asynchronous Begin and End blocks to the operation you defined in the service ,so it can be called Asynchronously .Just a small code from the Reference.cs file :

 [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="ServiceReference1.IService1")]
public interface IService1 {

[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/GetData", ReplyAction="http://tempuri.org/IService1/GetDataResponse")]
string GetData(int value);

[System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://tempuri.org/IService1/GetData", ReplyAction="http://tempuri.org/IService1/GetDataResponse")]
System.IAsyncResult BeginGetData(int value, System.AsyncCallback callback, object asyncState);

string EndGetData(System.IAsyncResult result);

[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/GetDataUsingDataContract", ReplyAction="http://tempuri.org/IService1/GetDataUsingDataContractResponse")]
WebApplication1.ServiceReference1.CompositeType GetDataUsingDataContract(WebApplication1.ServiceReference1.CompositeType composite);

[System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://tempuri.org/IService1/GetDataUsingDataContract", ReplyAction="http://tempuri.org/IService1/GetDataUsingDataContractResponse")]
System.IAsyncResult BeginGetDataUsingDataContract(WebApplication1.ServiceReference1.CompositeType composite, System.AsyncCallback callback, object asyncState);

WebApplication1.ServiceReference1.CompositeType EndGetDataUsingDataContract(System.IAsyncResult result);
}

Hope this helps !!!

About creamcode

I graduated from NED . Currently involved in development on Microsoft platforms .I attained Microsoft Certifications in the following areas : ASP.NET 2.0 (70-528). MICROSOFT DOT NET FRAMEWORK FOUNDATION (70-536). TS: Windows Communication Foundation Development with Microsoft .NET Framework 4 (70-513) CHARTERED MEMBER. Its always fun to learn and work on new things . An advise to all the developers , if your stuck , leave it for a while and when you will be back , there will be a new dimension for solving the problem :)....Happy Coding !
This entry was posted in WCF. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s