다음을 통해 공유


방법: 대기 기술을 사용하여 비동기 웹 서비스 클라이언트 구현

이 항목은 레거시 기술과 관련된 것입니다. 이제 XML Web services와 XML Web services 클라이언트는 다음을 사용하여 만들어야 합니다. Windows Communication Foundation.

대기 기술은 웹 서비스 메서드(동기 액세스용인 메서드 포함)와 비동기적으로 통신하기 위해 웹 서비스 클라이언트를 구현하는 한 방법입니다. 이 기술에 대한 자세한 내용은 XML Web Services와 비동기적으로 통신 항목을 참조하십시오.

이 예제는 Factorize 메서드를 사용하는 PrimeFactorizer 웹 서비스 클래스를 기반으로 하며, Wsdl.exe 도구는 BeginFactorizeEndFactorize의 두 가지 비동기 클라이언트 프록시 메서드를 생성합니다.

대기 기술을 구현하려면

  1. 웹 서비스 클라이언트는 생성된 프록시 클래스의 Begin 메서드를 호출합니다.

    IAsyncResult ar = pf.BeginFactorize(factorizableNum, null, null);
    
    Dim ar As IAsyncResult = pf.BeginFactorize(factorizableNum, _
        Nothing, Nothing)
    
  2. 웹 서비스 클라이언트는 반환된 IAsyncResultAsyncWaitHandle 속성을 통해 WaitHandle 개체에 액세스합니다. 클라이언트는 WaitHandle 클래스의 대기 메서드 중 하나를 호출하고 하나 이상의 동기화 개체가 신호를 받을 때까지 기다립니다.

    클라이언트가 이 기술을 사용하여 단 하나의 웹 서비스 메서드를 비동기적으로 호출할 경우 WaitOne을 호출하여 메서드 처리가 완료될 때까지 기다릴 수 있습니다. 다른 대기 메서드에는 WaitAnyWaitAll이 있습니다.

    ar.AsyncWaitHandle.WaitOne();
    
    ar.AsyncWaitHandle.WaitOne()
    
  3. 대기 메서드가 반환되면 클라이언트는 End 메서드를 호출하여 결과를 가져옵니다.

    results = pf.EndFactorize(ar);
    
    results = pf.EndFactorize(ar)
    

예제

// -----------------------------------------------------------------------// Async Variation 2.
// Asynchronously invoke the Factorize method, 
//without specifying a call back.
using System;
using System.Runtime.Remoting.Messaging;
// MyFactorize, is the name of the namespace in which the proxy class is
// a member of for this sample.
using MyFactorize;  

class TestCallback
 {          
      public static void Main(){
            long factorizableNum = 12345;
            PrimeFactorizer pf = new PrimeFactorizer();

          // Begin the Async call to Factorize.
            IAsyncResult ar = pf.BeginFactorize(factorizableNum, null, null);

          // Wait for the asynchronous operation to complete.
          ar.AsyncWaitHandle.WaitOne();

          // Get the completed results.
          long[] results;     
          results = pf.EndFactorize(ar);
          
          //Output the results.
            Console.Write("12345 factors into: ");
            int j;
            for (j = 0; j<results.Length;j++){
                  if (j == results.Length - 1)
                      Console.WriteLine(results[j]);
                  else 
                      Console.Write(results[j] + ", ");
            }
        }
}
Imports System
Imports System.Runtime.Remoting.Messaging
Imports MyFactorize     ' Proxy class namespace

Public Class TestCallback
      Public Shared Sub Main()
            Dim factorizableNum As Long = 12345
            Dim pf As PrimeFactorizer = new PrimeFactorizer()

          ' Begin the Async call to Factorize.
            Dim ar As IAsyncResult = pf.BeginFactorize(factorizableNum, Nothing, Nothing)

          ' Wait for the asynchronous operation to complete.
          ar.AsyncWaitHandle.WaitOne()

          ' Get the completed results.
          Dim results() as Long
          results = pf.EndFactorize(ar)
          
          'Output the results.
            Console.Write("12345 factors into: ")
            Dim j as Integer
            For j = 0 To results.Length - 1
                  If  j = (results.Length - 1) Then
                      Console.WriteLine(results(j) )
                  Else 
                      Console.Write(results(j).ToString + ", ")
                    End If
          Next j         
      End Sub
End Class

참고 항목

작업

방법: 콜백 기술을 사용하여 비동기 웹 서비스 클라이언트 구현
방법: 웹 서비스 클라이언트에서 비동기 호출 수행

개념

XML Web services와 비동기적으로 통신
XML Web services 클라이언트 빌드

기타 리소스

XML Web services에 대한 클라이언트 만들기