ServiceContractAttribute.CallbackContract 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置当协定为双工协定时的回调协定类型。
public:
property Type ^ CallbackContract { Type ^ get(); void set(Type ^ value); };
public Type CallbackContract { get; set; }
member this.CallbackContract : Type with get, set
Public Property CallbackContract As Type
属性值
指示回调协定的 A Type 。 默认值为 null。
示例
下面的代码示例演示一个指定回调协定的服务,该协定指示类型的服务 IDuplexHello 必须具有实现类型 IHelloCallbackContract服务的对应方。 此外, IHelloCallbackContract 实现单向回调方法,使服务能够调用客户端,而无需等待回复来支持分布式事件驱动的客户端。
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);
}
}
}
注解
在 CallbackContract 属性中指定一个接口,该接口表示双向(或双工)消息交换中所需的相反协定。 这使客户端应用程序能够侦听服务器端服务应用程序可以独立于客户端活动发送的入站操作调用。 具有单向操作的回调协定表示客户端可以处理的服务的调用。
注释
回调协定上忽略该 ServiceContractAttribute 属性。 若要配置回调对象的运行时行为,请使用 System.ServiceModel.CallbackBehaviorAttribute.