자습서: Windows Communication Foundation 클라이언트 사용

이 자습서에서는 기본 WCF(Windows Communication Foundation) 애플리케이션을 만드는 데 필요한 다섯 가지 작업 중 마지막 작업에 대해 설명합니다. 자습서에 대한 개요는 자습서: Windows Communication Foundation 애플리케이션 시작를 참조하세요.

WCF(Windows Communication Foundation) 프록시를 만들고 구성한 후에는 클라이언트 인스턴스를 만들고 클라이언트 애플리케이션을 컴파일합니다. 그런 다음, WCF 서비스와 통신하는 데 사용합니다.

이 자습서에서는 다음을 하는 방법을 알아볼 수 있습니다.

  • WCF 클라이언트를 사용할 코드를 추가합니다.
  • WCF 클라이언트를 테스트합니다.

WCF 클라이언트를 사용할 코드를 추가합니다.

클라이언트 코드는 다음 단계를 수행합니다.

  • WCF 클라이언트를 인스턴스화합니다.
  • 생성된 프록시에서 서비스 작업 호출
  • 작업 호출이 완료된 후 클라이언트 닫기

GettingStartedClient 프로젝트에서 Program.cs 또는 Module1.vb 파일을 열고 해당 코드를 다음 코드로 바꿉니다.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GettingStartedClient.ServiceReference1;

namespace GettingStartedClient
{
    class Program
    {
        static void Main(string[] args)
        {
            //Step 1: Create an instance of the WCF proxy.
            CalculatorClient client = new CalculatorClient();

            // Step 2: Call the service operations.
            // Call the Add service operation.
            double value1 = 100.00D;
            double value2 = 15.99D;
            double result = client.Add(value1, value2);
            Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

            // Call the Subtract service operation.
            value1 = 145.00D;
            value2 = 76.54D;
            result = client.Subtract(value1, value2);
            Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);

            // Call the Multiply service operation.
            value1 = 9.00D;
            value2 = 81.25D;
            result = client.Multiply(value1, value2);
            Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);

            // Call the Divide service operation.
            value1 = 22.00D;
            value2 = 7.00D;
            result = client.Divide(value1, value2);
            Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);

            // Step 3: Close the client to gracefully close the connection and clean up resources.
            Console.WriteLine("\nPress <Enter> to terminate the client.");
            Console.ReadLine();
            client.Close();
        }
    }
}
Imports System.Collections.Generic
Imports System.Text
Imports System.ServiceModel
Imports GettingStartedClient.ServiceReference1

Module Module1

    Sub Main()
        ' Step 1: Create an instance of the WCF proxy.
        Dim Client As New CalculatorClient()

        ' Step 2: Call the service operations.
        ' Call the Add service operation.
        Dim value1 As Double = 100D
        Dim value2 As Double = 15.99D
        Dim result As Double = Client.Add(value1, value2)
        Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result)

        ' Call the Subtract service operation.
        value1 = 145D
        value2 = 76.54D
        result = Client.Subtract(value1, value2)
        Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result)

        ' Call the Multiply service operation.
        value1 = 9D
        value2 = 81.25D
        result = Client.Multiply(value1, value2)
        Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result)

        ' Call the Divide service operation.
        value1 = 22D
        value2 = 7D
        result = Client.Divide(value1, value2)
        Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result)

        ' Step 3: Close the client to gracefully close the connection and clean up resources.
        Console.WriteLine()
        Console.WriteLine("Press <Enter> to terminate the client.")
        Console.ReadLine()
        Client.Close()

    End Sub

End Module

GettingStartedClient.ServiceReference1을 가져오는 using(Visual C#의 경우) 또는 Imports(Visual Basic의 경우) 문을 확인하세요. 이 문은 Visual Studio에서 서비스 참조 추가 함수로 생성한 코드를 가져옵니다. 이 코드는 WCF 프록시를 인스턴스화하고 계산기 서비스가 노출하는 각 서비스 작업을 호출합니다. 그런 다음 프록시를 닫고 프로그램을 종료합니다.

WCF 클라이언트 테스트

Visual Studio에서 애플리케이션 테스트

  1. 솔루션을 저장하고 빌드합니다.

  2. GettingStartedClient 폴더를 선택한 다음, 바로 가기 메뉴에서 시작 프로젝트로 설정을 선택합니다.

  3. 시작 프로젝트의 드롭다운 목록에서 GettingStartedClient를 선택한 다음, 실행을 선택하거나 F5 키를 누릅니다.

명령 프롬프트에서 애플리케이션 테스트

  1. 관리자로 명령 프롬프트를 열고 Visual Studio 솔루션 디렉터리로 이동합니다.

  2. 서비스를 시작하려면: GettingStartedHost\bin\Debug\GettingStartedHost.exe를 입력합니다.

  3. 클라이언트를 시작하려면: 다른 명령 프롬프트를 열고 Visual Studio 솔루션 디렉터리로 이동한 다음, GettingStartedClient\bin\Debug\GettingStartedClient.exe를 입력합니다.

    GettingStartedHost.exe는 다음 출력을 생성합니다.

    The service is ready.
    Press <Enter> to terminate the service.
    
    Received Add(100,15.99)
    Return: 115.99
    Received Subtract(145,76.54)
    Return: 68.46
    Received Multiply(9,81.25)
    Return: 731.25
    Received Divide(22,7)
    Return: 3.14285714285714
    

    GettingStartedClient.exe는 다음 출력을 생성합니다.

    Add(100,15.99) = 115.99
    Subtract(145,76.54) = 68.46
    Multiply(9,81.25) = 731.25
    Divide(22,7) = 3.14285714285714
    
    Press <Enter> to terminate the client.
    

다음 단계

이제 WCF 시작 자습서의 모든 작업을 완료했습니다. 이 자습서에서는 다음 작업 방법을 알아보았습니다.

이 자습서에서는 다음을 하는 방법을 알아볼 수 있습니다.

  • WCF 클라이언트를 사용할 코드를 추가합니다.
  • WCF 클라이언트를 테스트합니다.

단계에서 문제나 오류가 있는 경우 문제 해결 문서의 단계에 따라 문제를 해결하세요.