Číst v angličtině

Sdílet prostřednictvím


Postupy: Implementace operace asynchronní služby

V aplikacích WCF (Windows Communication Foundation) je možné operaci služby implementovat asynchronně nebo synchronně bez diktování klientovi, jak ji volat. Asynchronní operace služby se například dají volat synchronně a synchronní operace služby se dají volat asynchronně. Příklad, který ukazuje, jak volat operaci asynchronně v klientské aplikaci, viz Postupy: Volání operací služby asynchronně. Další informace o synchronních a asynchronních operacích naleznete v tématu Návrh kontraktů služeb a synchronních a asynchronních operací. Toto téma popisuje základní strukturu asynchronní operace služby, kód není dokončen. Úplný příklad stran služby i klienta najdete v části Asynchronní.

Asynchronní implementace operace služby

  1. Ve smlouvě služby deklarujte dvojici asynchronních metod podle pokynů pro asynchronní návrh .NET. Metoda Begin přebírá parametr, objekt zpětného volání a stavový objekt a vrátí System.IAsyncResult a odpovídající End metodu, která přebírá System.IAsyncResult a vrací návratovou hodnotu. Další informace o asynchronních voláních naleznete v tématu Vzory návrhu asynchronního programování.

  2. Označte Begin metodu dvojice asynchronní metody s atributem System.ServiceModel.OperationContractAttribute a nastavte OperationContractAttribute.AsyncPattern vlastnost na true. Například následující kód provede kroky 1 a 2.

    C#
      [OperationContractAttribute(AsyncPattern=true)]
      IAsyncResult BeginServiceAsyncMethod(string msg, AsyncCallback callback, object asyncState);
    
      // Note: There is no OperationContractAttribute for the end method.
      string EndServiceAsyncMethod(IAsyncResult result);
    }
    
  3. Implementujte dvojici Begin/End metod ve třídě služby podle pokynů pro asynchronní návrh. Následující příklad kódu například ukazuje implementaci, ve které je řetězec zapsán do konzoly v Begin End obou částech asynchronní operace služby a návratová End hodnota operace je vrácena klientovi. Úplný příklad kódu najdete v části Příklad.

    C#
    public IAsyncResult BeginServiceAsyncMethod(string msg, AsyncCallback callback, object asyncState)
    {
      Console.WriteLine($"BeginServiceAsyncMethod called with: \"{msg}\"");
      return new CompletedAsyncResult<string>(msg);
    }
    
    public string EndServiceAsyncMethod(IAsyncResult r)
    {
      CompletedAsyncResult<string> result = r as CompletedAsyncResult<string>;
      Console.WriteLine($"EndServiceAsyncMethod called with: \"{result.Data}\"");
      return result.Data;
    }
    

Příklad

Následující příklady kódu ukazují:

  1. Rozhraní kontraktu služeb s:

    1. Synchronní SampleMethod operace.

    2. Asynchronní BeginSampleMethod operace.

    3. Dvojice asynchronních BeginServiceAsyncMethod/EndServiceAsyncMethod operací.

  2. Implementace služby pomocí objektu System.IAsyncResult .

C#
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 \"{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: \"{msg}\"");
      return new CompletedAsyncResult<string>(msg);
    }

    public string EndServiceAsyncMethod(IAsyncResult r)
    {
      CompletedAsyncResult<string> result = r as CompletedAsyncResult<string>;
      Console.WriteLine($"EndServiceAsyncMethod called with: \"{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
  }
}

Viz také