WCF is one of the most asked topics in interviews. Recently one of my reader sent me a mail. He asked, “What is error in following code?” His code snippet was as below,


[ServiceContract]
    public interface IService1
    {

        [OperationContract(IsOneWay=true)]
        string  SendHello(string name);
        
    }


In one sentence, problem with above code is in return type of Operation Contract. IsOneWay property of Operation Contract is set to True, which is making service message format as Simplex or One Way. A One way WCF Service cannot return anything. Its return type must be set to Void.

image

In given code snippet, Service is created as One Way Service and return type of operation contract is set to string and that is the reason Service is throwing run time Exception.

image

Given code snippet can be fixed by changing return type of operation contract from string to void.


[ServiceContract]
    public interface IService1
    {

        [OperationContract(IsOneWay=true)]
        void  SendHello(string name);
        
    }


Conclusion is one-way or Simplex WCF Service cannot return any data to client or its return type must be set to Void. If one-way service return type is set to any other return type then run time exception will be thrown.

I hope you find this post useful. Thanks for reading.

Advertisements

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 )

Google+ photo

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

Connecting to %s