방법: 코드에서 클라이언트 바인딩 지정
이 예제에서는 계산기 서비스를 사용할 클라이언트를 만들고 해당 클라이언트에 대한 바인딩을 코드를 사용하여 명령적으로 지정합니다. 클라이언트는 CalculatorService
인터페이스를 구현하는 ICalculator
에 액세스하고, 서비스 및 클라이언트 모두 BasicHttpBinding 클래스를 사용합니다.
이 절차에서는 계산기 서비스를 실행 중인 것으로 가정합니다. 서비스 빌드에 대한 자세한 내용은 방법: 구성에서 서비스 바인딩 지정을 참조하세요. 또한 WCF(Windows Communication Foundation)에서 제공하는 ServiceModel 메타데이터 유틸리티 도구(Svcutil.exe)를 사용하여 클라이언트 구성 요소를 자동으로 생성합니다. 이 도구는 서비스에 액세스하기 위한 클라이언트 코드를 생성합니다.
클라이언트는 두 가지 부분에 빌드됩니다. Svcutil.exe는 ClientCalculator
인터페이스를 구현하는 ICalculator
를 생성합니다. 그런 다음 ClientCalculator
의 인스턴스를 구성한 후 코드를 사용하여 서비스에 대한 주소와 바인딩을 지정하여 이 클라이언트 애플리케이션을 구성합니다.
이 예제의 소스 복사에 대해서는 BasicBinding 샘플을 참조하세요.
코드에서 사용자 지정 바인딩을 지정하려면
명령줄에서 Svcutil.exe를 사용하여 서비스 메타데이터에서 코드를 생성합니다.
Svcutil.exe <service's Metadata Exchange (MEX) address or HTTP GET address>
생성된 클라이언트에는 클라이언트 구현에서 충족해야 하는 서비스 계약을 정의하는
ICalculator
인터페이스가 포함되어 있습니다.[ServiceContract] public interface ICalculator { [OperationContract] double Add(double n1, double n2); [OperationContract] double Subtract(double n1, double n2); [OperationContract] double Multiply(double n1, double n2); [OperationContract] double Divide(double n1, double n2); }
<ServiceContract()> _ Public Interface ICalculator <OperationContract()> _ Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double <OperationContract()> _ Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double <OperationContract()> _ Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double <OperationContract()> _ Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double End Interface
또한 생성된 클라이언트에는
ClientCalculator
의 구현이 포함되어 있습니다.public class CalculatorClient : System.ServiceModel.ClientBase<Microsoft.ServiceModel.Samples.ICalculator>, Microsoft.ServiceModel.Samples.ICalculator { public CalculatorClient() { } public CalculatorClient(string endpointConfigurationName) : base(endpointConfigurationName) { } public CalculatorClient(string endpointConfigurationName, string remoteAddress) : base(endpointConfigurationName, remoteAddress) { } public CalculatorClient(string endpointConfigurationName, EndpointAddress remoteAddress) : base(endpointConfigurationName, remoteAddress) { } public CalculatorClient(Binding binding, EndpointAddress remoteAddress) : base(binding, remoteAddress) { } public double Add(double n1, double n2) { return base.Channel.Add(n1, n2); } public double Subtract(double n1, double n2) { return base.Channel.Subtract(n1, n2); } public double Multiply(double n1, double n2) { return base.Channel.Multiply(n1, n2); } public double Divide(double n1, double n2) { return base.Channel.Divide(n1, n2); } }
Public Class CalculatorClient Inherits System.ServiceModel.ClientBase(Of Microsoft.ServiceModel.Samples.ICalculator) Implements Microsoft.ServiceModel.Samples.ICalculator Public Sub New() End Sub Public Sub New(ByVal endpointConfigurationName As String) MyBase.New(endpointConfigurationName) End Sub Public Sub New(ByVal endpointConfigurationName As String, _ ByVal remoteAddress As String) MyBase.New(endpointConfigurationName, remoteAddress) End Sub Public Sub New(ByVal endpointConfigurationName As String, _ ByVal remoteAddress As EndpointAddress) MyBase.New(endpointConfigurationName, remoteAddress) End Sub Public Sub New(ByVal binding As Binding, _ ByVal remoteAddress As EndpointAddress) MyBase.New(binding, remoteAddress) End Sub Public Function Add(ByVal n1 As Double, _ ByVal n2 As Double) As Double Implements Microsoft.ServiceModel.Samples.ICalculator.Add Return MyBase.Channel.Add(n1, n2) End Function Public Function Subtract(ByVal n1 As Double, _ ByVal n2 As Double) As Double Implements Microsoft.ServiceModel.Samples.ICalculator.Subtract Return MyBase.Channel.Subtract(n1, n2) End Function Public Function Multiply(ByVal n1 As Double, _ ByVal n2 As Double) As Double Implements Microsoft.ServiceModel.Samples.ICalculator.Multiply Return MyBase.Channel.Multiply(n1, n2) End Function Public Function Divide(ByVal n1 As Double, _ ByVal n2 As Double) As Double Implements Microsoft.ServiceModel.Samples.ICalculator.Divide Return MyBase.Channel.Divide(n1, n2) End Function End Class
클라이언트 애플리케이션에서
ClientCalculator
클래스를 사용하는 BasicHttpBinding의 인스턴스를 만든 다음 지정된 주소에서 서비스 작업을 호출합니다.//Client implementation code. class Client { static void Main() { //Specify the binding to be used for the client. BasicHttpBinding binding = new BasicHttpBinding(); //Specify the address to be used for the client. EndpointAddress address = new EndpointAddress("http://localhost/servicemodelsamples/service.svc"); // Create a client that is configured with this address and binding. CalculatorClient client = new CalculatorClient(binding, address); // 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); //Closing the client gracefully closes the connection and cleans up resources client.Close(); Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine(); } } }
'Client implementation code. Friend Class Client Shared Sub Main() 'Specify the binding to be used for the client. Dim binding As New BasicHttpBinding() 'Specify the address to be used for the client. Dim address As New EndpointAddress("http://localhost/servicemodelsamples/service.svc") ' Create a client that is configured with this address and binding. Dim client As New CalculatorClient(binding, address) ' Call the Add service operation. Dim value1 = 100.0R Dim value2 = 15.99R Dim result = client.Add(value1, value2) Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result) ' Call the Subtract service operation. value1 = 145.0R value2 = 76.54R result = client.Subtract(value1, value2) Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result) ' Call the Multiply service operation. value1 = 9.0R value2 = 81.25R result = client.Multiply(value1, value2) Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result) ' Call the Divide service operation. value1 = 22.0R value2 = 7.0R result = client.Divide(value1, value2) Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result) 'Closing the client gracefully closes the connection and cleans up resources client.Close() Console.WriteLine() Console.WriteLine("Press <ENTER> to terminate client.") Console.ReadLine() End Sub End Class End Namespace
클라이언트를 컴파일하고 실행합니다.