이 예제에서는 계산기 서비스를 사용하기 위해 클라이언트가 만들어지고 해당 클라이언트에 대한 바인딩이 코드에서 명령적으로 지정됩니다. 클라이언트는 인터페이스를 CalculatorService
구현하는 ICalculator
에 액세스하고 서비스와 클라이언트 모두 클래스를 BasicHttpBinding 사용합니다.
이 절차에서는 계산기 서비스가 실행 중이라고 가정합니다. 서비스 빌드에 대한 자세한 내용은 방법: 구성에서 서비스 바인딩 지정을 참조하세요. 또한 ServiceModel 메타데이터 유틸리티 도구(Svcutil.exe)WCF(Windows Communication Foundation)를 사용하여 클라이언트 구성 요소를 자동으로 생성합니다. 이 도구는 서비스에 액세스하기 위한 클라이언트 코드를 생성합니다.
클라이언트는 두 부분으로 빌드됩니다. Svcutil.exe은 ICalculator
인터페이스를 구현하는 ClientCalculator
을 생성합니다. 그런 다음, 이 클라이언트 애플리케이션은 인스턴스 ClientCalculator
를 생성한 다음 코드에서 서비스의 바인딩 및 주소를 지정하여 생성됩니다.
이 예제의 원본 복사본은 BasicBinding 샘플을 참조하세요.
코드에서 사용자 지정 바인딩을 지정하려면
명령줄의 Svcutil.exe 사용하여 서비스 메타데이터에서 코드를 생성합니다.
Svcutil.exe <service's Metadata Exchange (MEX) address or HTTP GET address>
생성된 클라이언트에는 클라이언트 구현이 충족해야 하는 서비스 계약을 정의하는 #D0 인터페이스가 포함되어 있습니다.
[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({value1},{value2}) = {result}"); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine($"Subtract({value1},{value2}) = {result}"); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine($"Multiply({value1},{value2}) = {result}"); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine($"Divide({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
클라이언트를 컴파일하고 실행합니다.
참고하십시오
- #B0 바인딩을 사용하여 서비스를 구성하고 클라이언트를 #C1 설정