Take a look at this tutorial for WCF, and this MSDN Article on duplex services.
You're almost there with your definitions, I would define the above myself as:
Service Contract - The interface that defines the operations a web service exposes to clients.
Callback Contract - Similar to a service contract, but as you note, for the client side. This is defines how a web service can communicate to client as a separate call. (As opposed to simply returning data from the calls in the service contract). These are often used for returning values from long-running web service calls, or event signaling.
IsOneWay = true
on the service contract - This specifies that the service operation is returns no value, and hence the client will simply "fire and forget". The call to the webservice will not block the client until it completes, but instead return immediately. For this reason, operations with IsOneWay = true
can only return void.
IsOneWay = true
on the callback contract - This is much the same as it is on the service contract. When the server calls the operation on the callback contract, it will return immediately and not block until the operation completes.
Void returns - If IsOneWay is not set to true, the call will still block until the operation completes, a SOAP message will still be returned, but with no data (unless you are passing back faults). If you wish to actually return values, you can either do so exactly as you would with normal methods, setting the return type of the operation i.e
[ServiceContract]
public interface IService
{
[OperationContract]
DateTime GetDateTime();
}
public class Service : IService
{
public DateTime GetDateTime()
{
return DateTime.Now;
}
}
Alternatively, you could create a duplex service, with a callback contract, and IsOneWay = true
[ServiceContract(CallbackContract = typeof(ICallbackService))]
public interface IService
{
[OperationContract(IsOneWay = true)]
void GetDateTime();
}
public interface ICallbackService
{
[OperationContract(IsOneWay = true)]
void GetDateTimeCompleted(DateTime dateTime);
}
public class Service : IService
{
public void GetDateTime()
{
// Do long action here.
...
Callback.GetDateTimeCompleted(DateTime.Now);
}
ICallbackService Callback
{
return OperationContext.Current.GetCallbackChannel<ICallbackService>();
}
}
Using this method:
- The call to the webservice
GetDateTime()
operation would return immediately
- The "Very long operation" would execute on the server
- The
GetDateTimeCompleted(DateTime dateTime)
on the client would get triggered when the server completes.
Please note that the above duplex example is not complete, you'll need to ensure you're handling things like sessions correctly.
You're definitely on the right track. I'd recommend following the tutorials linked above, (along with any others you find) and experimenting. You'll soon get a good feel for what is possible.