ServiceContractAttribute.CallbackContract Property
Gets or sets the type of callback contract when the contract is a duplex contract.
Namespace: System.ServiceModel
Assembly: System.ServiceModel (in System.ServiceModel.dll)
Specify an interface in the CallbackContract property that represents the required opposite contract in a two-way (or duplex) message exchange. This enables client applications to listen for inbound operation calls that the server-side service application can send independently of client activity. Callback contracts that have one-way operations represent calls from the service that the client can handle.
Note |
|---|
The ServiceContractAttribute attribute is ignored on callback contracts. To configure runtime behavior of callback objects, use the System.ServiceModel.CallbackBehaviorAttribute. |
The following code example shows a service that specifies a callback contract, which indicates that a service of type IDuplexHello must have a correspondent that implements a service of type IHelloCallbackContract. In addition, IHelloCallbackContract implements a one-way callback method, enabling the service to call the client without waiting for a reply to support a distributed, event-driven client.
using System; using System.Collections.Generic; using System.ServiceModel; using System.Threading; namespace Microsoft.WCF.Documentation { [ServiceContract( Name = "SampleDuplexHello", Namespace = "http://microsoft.wcf.documentation", CallbackContract = typeof(IHelloCallbackContract), SessionMode = SessionMode.Required )] public interface IDuplexHello { [OperationContract(IsOneWay = true)] void Hello(string greeting); } public interface IHelloCallbackContract { [OperationContract(IsOneWay = true)] void Reply(string responseToGreeting); } public class DuplexHello : IDuplexHello { public DuplexHello() { Console.WriteLine("Service object created: " + this.GetHashCode().ToString()); } ~DuplexHello() { Console.WriteLine("Service object destroyed: " + this.GetHashCode().ToString()); } public void Hello(string greeting) { Console.WriteLine("Caller sent: " + greeting); Console.WriteLine("Session ID: " + OperationContext.Current.SessionId); Console.WriteLine("Waiting two seconds before returning call."); // Put a slight delay to demonstrate asynchronous behavior on client. Thread.Sleep(2000); IHelloCallbackContract callerProxy = OperationContext.Current.GetCallbackChannel<IHelloCallbackContract>(); string response = "Service object " + this.GetHashCode().ToString() + " received: " + greeting; Console.WriteLine("Sending back: " + response); callerProxy.Reply(response); } } }
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note