다음을 통해 공유


WCF 클라이언트를 사용하여 서비스 액세스

서비스를 만들고 나면 다음 단계로 WCF 클라이언트를 만듭니다. 클라이언트 응용 프로그램은 WCF 클라이언트를 사용하여 서비스와 통신합니다. 클라이언트 응용 프로그램은 일반적으로 서비스의 메타데이터를 가져와서 서비스 호출에 사용할 수 있는 WCF 클라이언트 코드를 생성합니다.

WCF 클라이언트를 만드는 기본 단계에는 다음이 포함됩니다.

  1. 서비스 코드를 컴파일합니다.
  2. ServiceModel Metadata Utility Tool (SvcUtil.exe)를 사용하여 WCF 클라이언트를 만듭니다.

ServiceModel Metadata 유틸리티 도구

ServiceModel Metadata Utility Tool (Svcutil.exe)는 메타데이터에서 코드를 생성하기 위한 명령줄 도구입니다. 다음 사용은 기본 Svcutil.exe 명령 예제입니다.

Svcutil.exe <service's Metadata Exchange (MEX) address or HTTP GET address> 

또는 Svcutil.exe에 파일 시스템의 WSDL(웹 서비스 기술 언어) 및 XSD(XML 스키마 정의 언어) 파일을 사용할 수도 있습니다.

Svcutil.exe <list of WSDL and XSD files on file system>

결과는 클라이언트 응용 프로그램이 서비스 호출에 사용할 수 있는 WCF 클라이언트 코드를 포함하는 코드 파일입니다.

이 도구를 사용하여 구성 파일을 생성할 수도 있습니다.

Svcutil.exe <file1 [,file2]>

파일 이름을 제공하면 해당 이름이 출력 파일의 이름이 됩니다. 두 개의 파일 이름을 제공하면 첫 번째 파일은 입력 구성 파일로, 해당 내용이 생성된 구성과 병합되어 두 번째 파일에 기록됩니다. 구성에 대한 자세한 내용은 Windows Communication Foundation 서비스에 대한 바인딩 구성을 참조하십시오.

참고

보안되지 않은 메타데이터 요청은 보안되지 않은 네트워크 요청과 동일한 방식으로 특정 위험을 노출합니다. 통신하는 끝점이 실제로 주장되는 끝점인지 확실하지 않을 경우 검색한 정보가 악성 서비스의 메타데이터일 수 있습니다.

예제

다음 코드 예제에서는 서비스에 대해 만들어진 서비스 계약을 보여 줍니다.

// Define a service contract.
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
    [OperationContract]
    double Add(double n1, double n2);
    // Other methods are not shown here.
}
' Define a service contract.
<ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")> _
Public Interface ICalculator
    <OperationContract()>  _
    Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double 
    ' Other methods are not shown here.
End Interface 

ServiceModel Metadata 유틸리티 도구는 다음 WCF 클라이언트 클래스를 생성합니다. 클래스는 제네릭 ClientBase 클래스에서 상속되며 ICalculator 인터페이스를 구현합니다. 또한 이 도구는 여기에 표시되지 않은 ICalculator 인터페이스를 생성합니다.

public partial class CalculatorClient : System.ServiceModel.ClientBase<ICalculator>, ICalculator
{
    public CalculatorClient(){}
    
    public CalculatorClient(string configurationName) : 
            base(configurationName)
    {}
    
    public CalculatorClient(System.ServiceModel.Binding binding) : 
            base(binding)
    {}
    
    public CalculatorClient(System.ServiceModel.EndpointAddress address,
    System.ServiceModel.Binding binding) : 
            base(address, binding)
    {}
    
    public double Add(double n1, double n2)
    {
        return base.InnerChannel.Add(n1, n2);
    }
}
Partial Public Class CalculatorClient
    Inherits System.ServiceModel.ClientBase(Of ICalculator)
    Implements ICalculator
    
    Public Sub New()
        MyBase.New
    End Sub
    
    Public Sub New(ByVal configurationName As String)
        MyBase.New(configurationName)
    End Sub
    
    Public Sub New(ByVal binding As System.ServiceModel.Binding)
        MyBase.New(binding)
    End Sub
    
    Public Sub New(ByVal address As _
    System.ServiceModel.EndpointAddress, _
    ByVal binding As System.ServiceModel.Binding)
        MyBase.New(address, binding)
    End Sub
    
    Public Function Add(ByVal n1 As Double, ByVal n2 As Double) As _
    Double Implements ICalculator.Add
        Return MyBase.InnerChannel.Add(n1, n2)
    End Function 
End Class

WCF 클라이언트 사용

WCF 클라이언트를 사용하려면 다음 코드와 같이 WCF 클라이언트 인스턴스를 만든 다음 해당 메서드를 호출합니다.

// Create a client object with the given client endpoint configuration.
CalculatorClient calcClient = new CalculatorClient("CalculatorEndpoint"));
// Call the Add service operation.
double value1 = 100.00D;
double value2 = 15.99D;
double result = calcClient.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
' Create a client object with the given client endpoint configuration.
Dim calcClient As CalculatorClient = _
New CalculatorClient("CalculatorEndpoint")

' Call the Add service operation.
Dim value1 As Double = 100.00D
Dim value2 As Double = 15.99D
Dim result As Double = calcClient.Add(value1, value2)
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result)

클라이언트에서 Throw된 예외 디버깅

WCF 클라이언트에서 throw된 많은 예외는 서비스에서의 예외로 인해 발생합니다. 다음은 이를 보여 주는 몇 가지 예입니다.

  • SocketException: 기존 연결이 원격 호스트에 의해 강제로 닫혔습니다.
  • CommunicationException: 기본 연결이 예기치 않게 닫혔습니다.
  • CommunicationObjectAbortedException: 소켓 연결이 중단되었습니다. 이는 메시지 처리 오류, 원격 호스트에 의해 초과되는 수신 제한 시간 또는 기본 네트워크 리소스 문제로 인해 발생할 수 있습니다.

이러한 형식의 예외가 발생할 때 가장 좋은 문제 해결 방법은 서비스에서 추적 기능을 설정하고 발생한 예외를 확인하는 것입니다. 추적 추적추적을 사용하여 응용 프로그램 문제 해결을 참조하십시오.

참고 항목

작업

방법: Windows Communication Foundation 클라이언트 만들기
방법: 이중 계약을 사용하여 서비스 액세스
방법: 비동기적으로 WCF 서비스 작업 호출
방법: 단방향 및 요청-회신 계약을 사용하여 WCF 서비스 액세스
방법: WCF 클라이언트를 사용하여 WSE 3.0 서비스에 액세스
방법: XmlSerializer를 사용하여 WCF 클라이언트 응용 프로그램의 시작 시간 개선

개념

생성된 클라이언트 코드 이해
클라이언트 런타임 동작 지정
클라이언트 동작 구성