Return Type of One-Way or Simplex WCF Service

WCF is one of the most-asked topics in interviews. Recently one of my readers sent me a mail. He asked, "What is the error in the following code?"

His code snippet was:
 

[ServiceContract]

    public interface IService1

    {

        [OperationContract(IsOneWay=true)]

        string  SendHello(string name);       

    }

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

IsOneWay property

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

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

[ServiceContract]

    public interface IService1

    {

        [OperationContract(IsOneWay=true)]

        void  SendHello(string name);       

    }

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

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

Up Next
    Ebook Download
    View all
    Learn
    View all