OperationContractAttribute.IsOneWay 属性

定义

获取或设置一个值,该值指示操作是否返回答复消息。Gets or sets a value that indicates whether an operation returns a reply message.

public:
 property bool IsOneWay { bool get(); void set(bool value); };
public bool IsOneWay { get; set; }
member this.IsOneWay : bool with get, set
Public Property IsOneWay As Boolean

属性值

Boolean

如果此方法收到请求消息,但未返回任何答复消息,则为 true;否则为 falsetrue if this method receives a request message and returns no reply message; otherwise, false. 默认值为 falseThe default is false.

示例

下面的示例演示了一个服务,该服务实现指定三个操作的服务协定。The following example is a service that implements a service contract that specifies three operations. 其中两种方法实现双向操作,即无论返回值是什么,它们都将基础响应消息返回至调用方。Two of the methods implement two-way operations, which return underlying response messages to the caller no matter what the return value is. 第三种方法实现一个操作,该操作接收一个调用(基础入站消息),但不返回任何基础响应消息。The third method implements an operation that receives a call (an underlying inbound message) but returns no underlying response message.

[ServiceContract]  
public class OneAndTwoWay  
{  
  // The client waits until a response message appears.  
  [OperationContract]  
  public int MethodOne (int x, out int y)  
  {  
    y = 34;  
    return 0;  
  }  

  // The client waits until an empty response message appears.  
  [OperationContract]  
  public void MethodTwo (int x)  
  {  
    return;  
  }  

  // The client returns as soon as an outbound message  
  // is queued for dispatch to the service; no response  
  // message is generated or sent.  
  [OperationContract(IsOneWay=true)]  
  public void MethodThree (int x)  
  {  
    return;  
  }  
}  

注解

使用 IsOneWay 属性指示操作不返回答复消息。Use the IsOneWay property to indicate that an operation does not return a reply message. 这种类型的操作对通知或事件样式通信十分有用,特别是双向通信。This type of operation is useful for notifications or event-style communication, especially in two-way communication. 如果不等待基础响应消息,则单向操作的调用方在处理请求消息时无法直接检测错误。Without waiting for an underlying response message, callers of one-way operations have no direct way to detect a failure in processing the request message. (使用可靠的信道和单向操作的服务应用程序可在信道级别检测消息传递失败。(Service applications that use reliable channels and one-way operations can detect a message delivery failure at the channel level. 有关详细信息,请参阅 可靠会话概述。 ) For details, see Reliable Sessions Overview.)

在面向双工(或双向)服务的应用程序中,客户端和服务器相互独立地通信,并且客户端信道可使用其方法中的 IsOneWay 属性指示服务可单向调用客户端(该客户端可作为事件处理)。In duplex (or two-way) service-oriented applications in which the client and server communicate with each other independently, a client channel can use the IsOneWay property on its methods to indicate that the service can make one-way calls to the client that the client can treat as events. 这不会返回调用或生成消息,因为该服务不需要任何响应消息。No return call or message is generated because the service does not expect any response message.

如果 IsOneWay 属性设置为 false(默认值),即使返回 void 的方法也会生成答复消息。If the IsOneWay property is set to false (the default), even methods that return void result in a reply message. 在此种情况下,基础结构将创建并发送一条空消息,以向调用方指示该方法已返回内容。In this case, the infrastructure creates and sends an empty message to indicate to the caller that the method has returned. 使用此方法 (使基础结构能够将 SOAP 错误发送回客户端。 ) 设置 IsOneWaytrue 是取消创建和调度响应消息的唯一方法。(Using this approach enables the infrastructure to send SOAP faults back to the client.) Setting IsOneWay to true is the only way to cancel the creation and dispatch of a response message.

单向方法不得返回一个值或具有 refout 参数;否则将引发 System.InvalidOperationException 异常。One-way methods must not return a value or have ref or out parameters; otherwise a System.InvalidOperationException exception is thrown.

指定操作是单向操作,只表示它没有响应消息。Specifying that an operation is a one-way operation means only that there is no response message. 如果无法建立连接、出站消息非常大或该服务无法足够快地读取入站信息,则可能会阻止。It is possible to block if a connection cannot be made, or the outbound message is very large, or if the service cannot read inbound information fast enough. 如果客户端要求非阻止调用,则会生成 AsyncPattern 操作。If a client requires a non-blocking call, generate AsyncPattern operations. 有关详细信息,请参阅单向 服务使用 WCF 客户端访问服务For more information, see One-Way Services and Accessing Services Using a WCF Client.

适用于