Friday, May 17, 2013

How to return a List from WCF Service?


Consider a WCF service method that returns a generic list as follows:

public List<Employee> GetAllEmployees()
{
//code details here....

//returning a generic List<Employee>
}

On Client-side, if we try to access returned value of that web service method as follows:

MyServiceClient client = new MyServiceClient();
List<Employees> employeeList = client.GetAllEmployees();

We will face following error:
Cannot Implicitly convert type 'MyService.Employee[]' to System.Collections.Generic.List<MyService.Employee>.

Actually, Collections are exposed as array in Service metadata. Generic lists from a WCF service method will be returned as arrays, so we need to do some extra steps to get List<Employee> on client-side.

While adding a service reference, click on "Advance" button. It will open "Service Reference Settings" window. On this window, choose appropriate option for Collection Type i.e. System.Collections.Generic.List.

Now following code will run smoothly without any issue.
List<Employees> employeeList = client.GetAllEmployees();

Friday, May 3, 2013

WCF Service - Configuring Timeouts for Binding

As we already know that a WCF Service endpoint has ABC (Address, Binding and Contract).

Address means "Where to find the service?"
Binding means "How to communicate with service?"
Contract defines "What to communicate?

As WCF Service Binding defines all about how to communicate. It plays an important role in managing performance and security for the service. So, understanding timeouts configuration values on binding will definitely have impact on performance, usability as well as security.

Few days back, one of my colleague was facing an issue of inactivity timeout on a WCF service. We spend sometime looking into configuration and find out that "ReceiveTimeout" value on binding was set to a very short value.So, In order to resolve the issue, we just increase this timeout value to something bigger and reasonable as follows:

    <bindings>
        <netTcpBinding>
            <binding receiveTimeout="00:30:00".........>
            </binding>
        </netTcpBinding>
    </bindings>

"ReceiveTimeout" value on binding actually defines how long a session will remain idle before timing out.

Various timeout values on binding are listed below and we can find its detail here:
  1. OpenTimeout 
  2. CloseTimeout
  3. SendTimeout
  4. ReceiveTimeout
Going through these configuration in detail will definitely improve performance and security for WCF Service.

Sunday, April 21, 2013

WCF Service - Instance Deactivation


What is Instance Deactivation in Windows Communication Foundation? How explicitly we can deactivate an instance?

Instance DeactivationWhen a service method being called from client, its forwarded to a service instance. So, Instance deactivation means that the moment this service instance is disposed off by WCF.


In order to explicitly deactivate a service instance, we will do the following.
OperationContext.Current.InstanceContext.ReleaseServiceInstance();



Other WCF Service Tutorial: