Sun Maung Oo Sun Maung Oo - 1 month ago 11
C# Question

Is One Way Operation Contract faster?

This is a really noob question. I wanted to know if the One Way Contract of the WCF is faster than its counterpart which return the result.For example Is

[OperationContract(IsOneWay=true)]
[WebGet(UriTemplate = "/SendData/{data}", BodyStyle = WebMessageBodyStyle.WrappedResponse,
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
void SendData(string data)


faster than

[OperationContract(IsOneWay=false)]
[WebGet(UriTemplate="/SendData/{data}", BodyStyle = WebMessageBodyStyle.WrappedResponse,
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
bool SendData(string data)


the method SendData is like this with a lot of database code and locking

bool SendData(string data)
{
check if data is valid
if(data is valid)
{
lock(a database table)
{
search duplicate data
if(duplicate data found)
{
does not do anything (no-op)
}
else
{
save a data to the table
}
return true;
}
}
else
{
return false;
}
}


As you can see the method bool SendData(string data),have a lot of database and locking code.The main point of the question is should I change the Operation Contract to one way for faster result with the same code without changing anything.Or should my SendData method be like below to return the method faster

void SendData(string data)
{
Task.Factory.StartNew(() =>
{
Do Task
}
);
}

Answer

It's not faster per se, it's different.

A one-way call is one way. You send the message, that's it. It's fire-and-forget. If an exception is thrown in the service because for example the database could not be opened, you will never know. the call will return as soon as the message was delivered to the service. It will not wait until the service call completed.

If you want to wait until your service call is complete to make sure there were no errors, you will have to use the normal, non-one-way method.