這個範例示範具有單向服務作業的服務合約。This sample demonstrates a service contact with one-way service operations. 與雙向服務作業的情況不同,用戶端不會等候服務作業完成。The client does not wait for service operations to complete as is the case with two-way service operations. 這個範例是以消費者入門為基礎,並使用 wsHttpBinding 系結。This sample is based on the Getting Started and uses the wsHttpBinding binding. 這個範例中的服務是自我裝載的主控台應用程式,您可以用來觀察接收和處理要求的服務。The service in this sample is a self-hosted console application to enable you to observe the service that receives and processes requests. 用戶端也是主控台應用程式。The client is also a console application.


此範例的安裝程序與建置指示位於本主題的結尾。The setup procedure and build instructions for this sample are located at the end of this topic.

若要建立單向服務合約,請定義服務合約、將 OperationContractAttribute 類別套用至每一項作業,然後將 IsOneWay 設定為 true,如下列範例程式碼所示:To create a one-way service contract, define your service contract, apply the OperationContractAttribute class to each operation, and set IsOneWay to true as shown in the following sample code:

public interface IOneWayCalculator  
    void Add(double n1, double n2);  
    [OperationContract(IsOneWay = true)]  
    void Subtract(double n1, double n2);  
    [OperationContract(IsOneWay = true)]  
    void Multiply(double n1, double n2);  
    [OperationContract(IsOneWay = true)]  
    void Divide(double n1, double n2);  

為了示範用戶端不等待服務作業完成的狀況,本範例中的服務程式碼實作了五秒鐘的延遲,如下列範例程式碼所示:To demonstrate that the client does not wait for the service operations to complete, the service code in this sample implements a five-second delay, as shown in the following sample code:

// This service class implements the service contract.  
// This code writes output to the console window.  
 [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple,  
    InstanceContextMode = InstanceContextMode.PerCall)]  
public class CalculatorService : IOneWayCalculator  
    public void Add(double n1, double n2)  
        Console.WriteLine("Received Add({0},{1}) - sleeping", n1, n2);  
        System.Threading.Thread.Sleep(1000 * 5);  
        double result = n1 + n2;  
        Console.WriteLine("Processing Add({0},{1}) - result: {2}", n1, n2, result);  

當用戶端呼叫服務時,該呼叫會逕自傳回,而不等待服務作業完成。When the client calls the service, the call returns without waiting for the service operation to complete.

當您執行範例時,用戶端與服務活動都會顯示在服務與用戶端主控台視窗中。When you run the sample, the client and service activities are displayed in both the service and client console windows. 您可以查看來自用戶端的服務接收訊息。You can see the service receive messages from the client. 請在每個主控台視窗中按下 ENTER,以關閉服務與用戶端。Press ENTER in each console window to shut down both the service and the client.

用戶端會比服務先完成,表示用戶端不會等候單向服務作業完成。The client finishes ahead of the service, demonstrating that a client does not wait for one-way service operations to complete. 用戶端輸出如下:The client output is as follows:

Press <ENTER> to terminate client.  

服務輸出如下:The following service output is shown:

The service is ready.  
Press <ENTER> to terminate service.  
Received Add(100,15.99) - sleeping  
Received Subtract(145,76.54) - sleeping  
Received Multiply(9,81.25) - sleeping  
Received Divide(22,7) - sleeping  
Processing Add(100,15.99) - result: 115.99  
Processing Subtract(145,76.54) - result: 68.46  
Processing Multiply(9,81.25) - result: 731.25  
Processing Divide(22,7) - result: 3.14285714285714  


根據定義,HTTP 是一種要求/回應通訊協定;當要求提出時,就會傳回回應。HTTP is, by definition, a request/response protocol; when a request is made, a response is returned. 即便是在 HTTP 上公開的單向服務作業,也是如此。This is true even for a one-way service operation that is exposed over HTTP. 呼叫作業時,服務會在服務作業執行以前傳回 202 的 HTTP 狀態碼。When the operation is called, the service returns an HTTP status code of 202 before the service operation has executed. 這個狀態碼表示已接受要求進行處理,但是處理尚未完成。This status code means that the request has been accepted for processing, but the processing has not yet been completed. 呼叫此項作業的用戶端會封鎖,直到從服務收到 202 回應為止。The client that called the operation blocks until it receives the 202 response from the service. 當使用設定為使用工作階段的繫結來傳送多則單向訊息時,這可能會產生某種未預期的行為。This can cause some unexpected behavior when multiple one-way messages are sent using a binding that is configured to use sessions. 根據預設,這個範例中使用的 wsHttpBinding 繫結會設定為使用工作階段,以便建立安全性內容。The wsHttpBinding binding used in this sample is configured to use sessions by default to establish a security context. 在預設情況下,將保證工作階段中的訊息依照傳送的順序抵達。By default, messages in a session are guaranteed to arrive in the order in which they are sent. 因此,傳送工作階段中的第二則訊息時,就不會在第一則訊息處理完成之前加以處理。Because of this, when the second message in a session is sent, it is not processed until the first message has been processed. 因此,除非完成處理上一則的訊息,否則,用戶端將不會收到訊息的 202 回應。The result of this is that the client does not receive the 202 response for a message until the processing of the previous message has been completed. 所以用戶端會顯示為封鎖每次呼叫後續的作業。The client therefore appears to block on each subsequent operation call. 為了避免這種行為,此範例將執行階段設定為同時分派訊息至不同執行個體以進行處理。To avoid this behavior, this sample configures the runtime to dispatch messages concurrently to distinct instances for processing. 範例將 InstanceContextMode 設定為 PerCall,讓個別訊息可以分別由不同執行個體來處理。The sample sets InstanceContextMode to PerCall so that each message can be processed by a different instance. ConcurrencyMode 會設定為 Multiple,以允許多個執行緒同時分派訊息。ConcurrencyMode is set to Multiple to allow more than one thread to dispatch messages at a time.

若要安裝、建置及執行範例To set up, build, and run the sample

  1. 請確定您已針對 Windows Communication Foundation 範例執行一次安裝程式Ensure that you have performed the One-Time Setup Procedure for the Windows Communication Foundation Samples.

  2. 若要建置方案的 C# 或 Visual Basic .NET 版本,請遵循 Building the Windows Communication Foundation Samples中的指示。To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. 若要在單一或跨電腦設定中執行範例,請遵循執行Windows Communication Foundation 範例中的指示。To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.


請先執行服務,然後才執行用戶端;先關閉用戶端,再關閉服務。Run the service before you run the client and shut down the client before shutting down the service. 這可以避免當用戶端因服務消失而無法正常關閉安全性工作階段時,所發生的用戶端例外狀況。This avoids a client exception when the client cannot close the security session cleanly because the service is gone.


這些範例可能已安裝在您的電腦上。The samples may already be installed on your machine. 請先檢查下列 (預設) 目錄,然後再繼續。Check for the following (default) directory before continuing.


如果此目錄不存在,請移至.NET Framework 4 的 Windows Communication Foundation (wcf)和 Windows Workflow Foundation (WF)範例,以下載所有 WINDOWS COMMUNICATION FOUNDATION (wcf)和 WFWF 範例。If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WFWF samples. 此範例位於下列目錄。This sample is located in the following directory.