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 :
That’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 !!!
