作法:實作非同步服務作業
在 Windows Communication Foundation (WCF) 應用程式中,服務作業可以透過非同步或同步方式實作,不需規定用戶端呼叫服務作業的方式。 例如,非同步服務作業可以利用同步方式呼叫,而同步服務作業可以透過非同步方式呼叫。 如需示範如何在用戶端應用程式中以非同步方式呼叫作業的範例,請參閱 。同步和非同步作業的詳細資訊,請參閱如何:以非同步方式呼叫服務作業。 如需詳細瞭解同步和非同步作業,請參閱設計服務合約和同步和非同步作業。 本主題描述非同步服務作業的基本結構,程式碼尚未完成。 如需服務與用戶端兩者的完整範例,請參閱非同步。
以非同步方式實作服務作業
在您的服務合約中,根據 .NET 非同步設計方針宣告一個非同步方法組。
Begin
方法可接受一個參數、回呼物件和狀態物件,並傳回 System.IAsyncResult 和對應的End
方法,該方法會接受 System.IAsyncResult 並傳回其傳回值。 如需非同步呼叫的詳細資訊,請參閱非同步程式設計模式。使用
Begin
屬性 (Attribute) 來標記非同步方法組中的 System.ServiceModel.OperationContractAttribute 方法,並將 OperationContractAttribute.AsyncPattern 屬性 (Property) 設定為true
。 例如,下列程式碼會執行步驟 1 和 2。[OperationContractAttribute(AsyncPattern=true)] IAsyncResult BeginServiceAsyncMethod(string msg, AsyncCallback callback, object asyncState); // Note: There is no OperationContractAttribute for the end method. string EndServiceAsyncMethod(IAsyncResult result); }
<OperationContractAttribute(AsyncPattern:=True)> _ Function BeginServiceAsyncMethod(ByVal msg As String, ByVal callback As AsyncCallback, ByVal asyncState As Object) As IAsyncResult ' Note: There is no OperationContractAttribute for the end method. Function EndServiceAsyncMethod(ByVal result As IAsyncResult) As String End Interface
根據非同步設計方針,在您的服務類別內實作
Begin/End
方法組。 例如,下列程式碼範例會顯示利用非同步服務作業的Begin
和End
兩個部分,將字串寫入主控台的實作,以及傳回用戶端之End
作業的傳回值。 如需完整的程式碼範例,請參閱<範例>一節。public IAsyncResult BeginServiceAsyncMethod(string msg, AsyncCallback callback, object asyncState) { Console.WriteLine("BeginServiceAsyncMethod called with: \"{0}\"", msg); return new CompletedAsyncResult<string>(msg); } public string EndServiceAsyncMethod(IAsyncResult r) { CompletedAsyncResult<string> result = r as CompletedAsyncResult<string>; Console.WriteLine("EndServiceAsyncMethod called with: \"{0}\"", result.Data); return result.Data; }
Public Function BeginServiceAsyncMethod(ByVal msg As String, ByVal callback As AsyncCallback, ByVal asyncState As Object) As IAsyncResult Implements ISampleService.BeginServiceAsyncMethod Console.WriteLine("BeginServiceAsyncMethod called with: ""{0}""", msg) Return New CompletedAsyncResult(Of String)(msg) End Function Public Function EndServiceAsyncMethod(ByVal r As IAsyncResult) As String Implements ISampleService.EndServiceAsyncMethod Dim result As CompletedAsyncResult(Of String) = TryCast(r, CompletedAsyncResult(Of String)) Console.WriteLine("EndServiceAsyncMethod called with: ""{0}""", result.Data) Return result.Data End Function
範例
下列程式碼範例會顯示:
服務合約介面,其中具有:
一個同步
SampleMethod
作業。一個非同步
BeginSampleMethod
作業。非同步
BeginServiceAsyncMethod
/EndServiceAsyncMethod
作業組。
一個使用 System.IAsyncResult 物件的服務實作。
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Text;
using System.Threading;
namespace Microsoft.WCF.Documentation
{
[ServiceContractAttribute(Namespace="http://microsoft.wcf.documentation")]
public interface ISampleService{
[OperationContractAttribute]
string SampleMethod(string msg);
[OperationContractAttribute(AsyncPattern = true)]
IAsyncResult BeginSampleMethod(string msg, AsyncCallback callback, object asyncState);
//Note: There is no OperationContractAttribute for the end method.
string EndSampleMethod(IAsyncResult result);
[OperationContractAttribute(AsyncPattern=true)]
IAsyncResult BeginServiceAsyncMethod(string msg, AsyncCallback callback, object asyncState);
// Note: There is no OperationContractAttribute for the end method.
string EndServiceAsyncMethod(IAsyncResult result);
}
public class SampleService : ISampleService
{
#region ISampleService Members
public string SampleMethod(string msg)
{
Console.WriteLine("Called synchronous sample method with \"{0}\"", msg);
return "The synchronous service greets you: " + msg;
}
// This asynchronously implemented operation is never called because
// there is a synchronous version of the same method.
public IAsyncResult BeginSampleMethod(string msg, AsyncCallback callback, object asyncState)
{
Console.WriteLine("BeginSampleMethod called with: " + msg);
return new CompletedAsyncResult<string>(msg);
}
public string EndSampleMethod(IAsyncResult r)
{
CompletedAsyncResult<string> result = r as CompletedAsyncResult<string>;
Console.WriteLine("EndSampleMethod called with: " + result.Data);
return result.Data;
}
public IAsyncResult BeginServiceAsyncMethod(string msg, AsyncCallback callback, object asyncState)
{
Console.WriteLine("BeginServiceAsyncMethod called with: \"{0}\"", msg);
return new CompletedAsyncResult<string>(msg);
}
public string EndServiceAsyncMethod(IAsyncResult r)
{
CompletedAsyncResult<string> result = r as CompletedAsyncResult<string>;
Console.WriteLine("EndServiceAsyncMethod called with: \"{0}\"", result.Data);
return result.Data;
}
#endregion
}
// Simple async result implementation.
class CompletedAsyncResult<T> : IAsyncResult
{
T data;
public CompletedAsyncResult(T data)
{ this.data = data; }
public T Data
{ get { return data; } }
#region IAsyncResult Members
public object AsyncState
{ get { return (object)data; } }
public WaitHandle AsyncWaitHandle
{ get { throw new Exception("The method or operation is not implemented."); } }
public bool CompletedSynchronously
{ get { return true; } }
public bool IsCompleted
{ get { return true; } }
#endregion
}
}
Imports System.Collections.Generic
Imports System.ServiceModel
Imports System.Text
Imports System.Threading
Namespace Microsoft.WCF.Documentation
<ServiceContractAttribute(Namespace:="http://microsoft.wcf.documentation")> _
Public Interface ISampleService
<OperationContractAttribute> _
Function SampleMethod(ByVal msg As String) As String
<OperationContractAttribute(AsyncPattern:=True)> _
Function BeginSampleMethod(ByVal msg As String, ByVal callback As AsyncCallback, ByVal asyncState As Object) As IAsyncResult
'Note: There is no OperationContractAttribute for the end method.
Function EndSampleMethod(ByVal result As IAsyncResult) As String
<OperationContractAttribute(AsyncPattern:=True)> _
Function BeginServiceAsyncMethod(ByVal msg As String, ByVal callback As AsyncCallback, ByVal asyncState As Object) As IAsyncResult
' Note: There is no OperationContractAttribute for the end method.
Function EndServiceAsyncMethod(ByVal result As IAsyncResult) As String
End Interface
Public Class SampleService
Implements ISampleService
#Region "ISampleService Members"
Public Function SampleMethod(ByVal msg As String) As String Implements ISampleService.SampleMethod
Console.WriteLine("Called synchronous sample method with ""{0}""", msg)
Return "The synchronous service greets you: " & msg
End Function
' This asynchronously implemented operation is never called because
' there is a synchronous version of the same method.
Public Function BeginSampleMethod(ByVal msg As String, ByVal callback As AsyncCallback, ByVal asyncState As Object) As IAsyncResult Implements ISampleService.BeginSampleMethod
Console.WriteLine("BeginSampleMethod called with: " & msg)
Return New CompletedAsyncResult(Of String)(msg)
End Function
Public Function EndSampleMethod(ByVal r As IAsyncResult) As String Implements ISampleService.EndSampleMethod
Dim result As CompletedAsyncResult(Of String) = TryCast(r, CompletedAsyncResult(Of String))
Console.WriteLine("EndSampleMethod called with: " & result.Data)
Return result.Data
End Function
Public Function BeginServiceAsyncMethod(ByVal msg As String, ByVal callback As AsyncCallback, ByVal asyncState As Object) As IAsyncResult Implements ISampleService.BeginServiceAsyncMethod
Console.WriteLine("BeginServiceAsyncMethod called with: ""{0}""", msg)
Return New CompletedAsyncResult(Of String)(msg)
End Function
Public Function EndServiceAsyncMethod(ByVal r As IAsyncResult) As String Implements ISampleService.EndServiceAsyncMethod
Dim result As CompletedAsyncResult(Of String) = TryCast(r, CompletedAsyncResult(Of String))
Console.WriteLine("EndServiceAsyncMethod called with: ""{0}""", result.Data)
Return result.Data
End Function
#End Region
End Class
' Simple async result implementation.
Friend Class CompletedAsyncResult(Of T)
Implements IAsyncResult
Private data_Renamed As T
Public Sub New(ByVal data As T)
Me.data_Renamed = data
End Sub
Public ReadOnly Property Data() As T
Get
Return data_Renamed
End Get
End Property
#Region "IAsyncResult Members"
Public ReadOnly Property AsyncState() As Object Implements IAsyncResult.AsyncState
Get
Return CObj(data_Renamed)
End Get
End Property
Public ReadOnly Property AsyncWaitHandle() As WaitHandle Implements IAsyncResult.AsyncWaitHandle
Get
Throw New Exception("The method or operation is not implemented.")
End Get
End Property
Public ReadOnly Property CompletedSynchronously() As Boolean Implements IAsyncResult.CompletedSynchronously
Get
Return True
End Get
End Property
Public ReadOnly Property IsCompleted() As Boolean Implements IAsyncResult.IsCompleted
Get
Return True
End Get
End Property
#End Region
End Class
End Namespace