简体   繁体   中英

Understanding WCF IsOneWay, CalbackContracts and Duplex - Are my assumptions correct?

I have been reading and experimenting with WCF and trying to understand the workings in simple terms. So my questions are for verification and validation of what I believe to be correct but I need to be sure: In a typical Publish-Subscribe Duplex service.

1: Service Contract - this is the communication path that clients must make to the service.

2: Callback contract - this is the communication methods back to the client.

3: Setting the IsOneWay = true property on a callback contract means the Client will not get anything back from the server.

4: setting IsOneWay = true on the ServiceContract means the server will not get anything back from the client.

5: void return methods still send a reply back, if IsOneWay=true, the reply is is ignored, if false error and soap info can be obtained. ^

for brevity I have looked at the following and then Some^ⁿ:

Understanding WCF

WCF issue with using IsOneWay attribute

Understanding WCF Client to Server

https://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontractattribute.isoneway(v=vs.110).aspx

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 ie

[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:

  1. The call to the webservice GetDateTime() operation would return immediately
  2. The "Very long operation" would execute on the server
  3. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM