Compartilhar via


How to: Create a Request-Reply Contract

This topic shows the basic steps to create methods that use a request-reply contract. Such methods invoke operations on a Windows Communication Foundation (WCF) service and expect a reply. The reply must be sent and correlated to the request under the terms of this contract. Even if the method returns void, the infrastructure creates and sends an empty message to indicate to the caller that the method has returned. The only way to cancel the creation and dispatch of a reply message is to use a one-way contract for the operation.

For more information about how to specify operation contracts, see the OperationContractAttribute class and, in particular, the IsOneWay property.

For more information about creating a client application for a duplex contract, see How to: Access WCF Services with One-Way and Request-Reply Contracts.

To create a request-reply contract

  1. Create the service contract by applying the ServiceContractAttribute class to the interface that defines the methods the service is to implement.

  2. Indicate which methods in the interface the client can invoke by applying the OperationContractAttribute class to them.

  3. The value of the IsOneWay property indicates whether an operation returns a reply message. If an operation has a request-reply contract, this property is set to false. If the operation has a one-way contract, the property is set to true. All of the operations that carry the OperationContractAttribute class satisfy a request-reply contract by default because the IsOneWay property is false by default. So it is optional to explicitly specify the value of the attribute property to be false.

Example

The following sample defines a contract for a calculator service that provides Add and Subtract methods. Using this contract, a client calls the Add and Subtract methods and waits, respectively, for the sum and difference to return. The Multiply method is not part of the contract because it is not marked by the OperationContractAttribute class and so it is not accessible to clients.

using System.ServiceModel; 

[ServiceContract] 
public interface ICalculator 
{ 
[OperationContract] 
// It would be equivalent to write explicitly:
// [OperationContract(IsOneWay=false)] 
int Add(int a, int b); 

[OperationContract] 
int Subtract(int a, int b); 

int Multiply(int a, int b)
}
  • Marking a service contract interface with the ServiceContractAttribute class and its methods with the OperationContractAttribute class allows the automatic generation of service contract definitions in the Web Services Description Language (WSDL), client contacts, and code once the service is deployed.

  • An externally visible definition of a WCF service contract can be accessed once the service is deployed as a standard WSDL document that specifies the operations and their respective inputs and outputs. The WSDL is displayed by appending the ?wsdl query to the HTTP base address for the service and pointing the browser at the resulting Uniform Resource Identifier (URI).

See Also

Tasks

How to: Create a Duplex Contract

Reference

OperationContractAttribute

Concepts

Designing Service Contracts


© 2007 Microsoft Corporation. All rights reserved.
Last Published: 2010-03-21