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.
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.
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.
Leave a Reply