방법: 검색 프록시를 사용하여 서비스를 찾는 클라이언트 애플리케이션 구현
이 항목은 검색 프록시를 구현하는 방법에 대해 설명하는 세 항목 중 세 번째 항목입니다. 이전 항목인 방법: 검색 프록시에 등록하는 검색 가능한 서비스 구현에서는 검색 프록시에 자신을 등록하는 WCF 서비스를 구현했습니다. 이 항목에서는 검색 프록시를 사용하여 WCF 서비스를 찾는 WCF 클라이언트를 만듭니다.
클라이언트 구현
DiscoveryProxyExample
솔루션에Client
라는 새 콘솔 애플리케이션 프로젝트를 추가합니다.다음 어셈블리에 대한 참조를 추가합니다.
System.ServiceModel
System.ServiceModel.Discovery
이 항목 아래쪽의 GeneratedClient.cs를 이 프로젝트에 추가합니다.
참고 항목
일반적으로 이 파일은 Svcutil.exe와 같은 도구를 사용하여 생성됩니다. 이 파일은 작업을 단순화하기 위해 이 항목에 제공됩니다.
Program.cs 파일을 열고 다음 메서드를 추가합니다. 이 메서드는 엔드포인트 주소를 받고 이를 사용하여 서비스 클라이언트(프록시)를 초기화합니다.
static void InvokeCalculatorService(EndpointAddress endpointAddress) { // Create a client CalculatorServiceClient client = new CalculatorServiceClient(new NetTcpBinding(), endpointAddress); Console.WriteLine("Invoking CalculatorService at {0}", endpointAddress.Uri); Console.WriteLine(); double value1 = 100.00D; double value2 = 15.99D; // Call the Add service operation. double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result); Console.WriteLine(); // Closing the client gracefully closes the connection and cleans up resources client.Close(); }
Main
메서드에 다음 코드를 추가합니다.public static void Main() { // Create a DiscoveryEndpoint that points to the DiscoveryProxy Uri probeEndpointAddress = new Uri("net.tcp://localhost:8001/Probe"); DiscoveryEndpoint discoveryEndpoint = new DiscoveryEndpoint(new NetTcpBinding(), new EndpointAddress(probeEndpointAddress)); // Create a DiscoveryClient passing in the discovery endpoint DiscoveryClient discoveryClient = new DiscoveryClient(discoveryEndpoint); Console.WriteLine("Finding ICalculatorService endpoints using the proxy at {0}", probeEndpointAddress); Console.WriteLine(); try { // Search for services that implement ICalculatorService FindResponse findResponse = discoveryClient.Find(new FindCriteria(typeof(ICalculatorService))); Console.WriteLine("Found {0} ICalculatorService endpoint(s).", findResponse.Endpoints.Count); Console.WriteLine(); // Check to see if endpoints were found, if so then invoke the service. if (findResponse.Endpoints.Count > 0) { InvokeCalculatorService(findResponse.Endpoints[0].Address); } } catch (TargetInvocationException) { Console.WriteLine("This client was unable to connect to and query the proxy. Ensure that the proxy is up and running."); } Console.WriteLine("Press <ENTER> to exit."); Console.ReadLine(); }
클라이언트 애플리케이션의 구현을 완료했습니다. 방법: 검색 프록시 테스트로 계속 진행합니다.
예시
다음은 이 항목에서 사용되는 전체 코드 목록입니다.
// GeneratedClient.cs
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.1434
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Microsoft.Samples.Discovery
{
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace = "http://Microsoft.Samples.Discovery", ConfigurationName = "ICalculatorService")]
public interface ICalculatorService
{
[System.ServiceModel.OperationContractAttribute(ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign, Action = "http://Microsoft.Samples.Discovery/ICalculatorService/Add", ReplyAction = "http://Microsoft.Samples.Discovery/ICalculatorService/AddResponse")]
double Add(double n1, double n2);
[System.ServiceModel.OperationContractAttribute(ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign, Action = "http://Microsoft.Samples.Discovery/ICalculatorService/Subtract", ReplyAction = "http://Microsoft.Samples.Discovery/ICalculatorService/SubtractResponse")]
double Subtract(double n1, double n2);
[System.ServiceModel.OperationContractAttribute(ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign, Action = "http://Microsoft.Samples.Discovery/ICalculatorService/Multiply", ReplyAction = "http://Microsoft.Samples.Discovery/ICalculatorService/MultiplyResponse")]
double Multiply(double n1, double n2);
[System.ServiceModel.OperationContractAttribute(ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign, Action = "http://Microsoft.Samples.Discovery/ICalculatorService/Divide", ReplyAction = "http://Microsoft.Samples.Discovery/ICalculatorService/DivideResponse")]
double Divide(double n1, double n2);
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public interface ICalculatorServiceChannel : ICalculatorService, System.ServiceModel.IClientChannel
{
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class CalculatorServiceClient : System.ServiceModel.ClientBase<ICalculatorService>, ICalculatorService
{
public CalculatorServiceClient()
{
}
public CalculatorServiceClient(string endpointConfigurationName) :
base(endpointConfigurationName)
{
}
public CalculatorServiceClient(string endpointConfigurationName, string remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public CalculatorServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public CalculatorServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.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);
}
}
}
// Program.cs
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------
using System;
using System.Reflection;
using System.ServiceModel;
using System.ServiceModel.Discovery;
namespace Microsoft.Samples.Discovery
{
class Client
{
public static void Main()
{
// Create a DiscoveryEndpoint that points to the DiscoveryProxy
Uri probeEndpointAddress = new Uri("net.tcp://localhost:8001/Probe");
DiscoveryEndpoint discoveryEndpoint = new DiscoveryEndpoint(new NetTcpBinding(), new EndpointAddress(probeEndpointAddress));
DiscoveryClient discoveryClient = new DiscoveryClient(discoveryEndpoint);
Console.WriteLine("Finding ICalculatorService endpoints using the proxy at {0}", probeEndpointAddress);
Console.WriteLine();
try
{
// Find ICalculatorService endpoints
FindResponse findResponse = discoveryClient.Find(new FindCriteria(typeof(ICalculatorService)));
Console.WriteLine("Found {0} ICalculatorService endpoint(s).", findResponse.Endpoints.Count);
Console.WriteLine();
// Check to see if endpoints were found, if so then invoke the service.
if (findResponse.Endpoints.Count > 0)
{
InvokeCalculatorService(findResponse.Endpoints[0].Address);
}
}
catch (TargetInvocationException)
{
Console.WriteLine("This client was unable to connect to and query the proxy. Ensure that the proxy is up and running.");
}
Console.WriteLine("Press <ENTER> to exit.");
Console.ReadLine();
}
static void InvokeCalculatorService(EndpointAddress endpointAddress)
{
// Create a client
CalculatorServiceClient client = new CalculatorServiceClient(new NetTcpBinding(), endpointAddress);
Console.WriteLine("Invoking CalculatorService at {0}", endpointAddress.Uri);
Console.WriteLine();
double value1 = 100.00D;
double value2 = 15.99D;
// Call the Add service operation.
double result = client.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
// Call the Subtract service operation.
result = client.Subtract(value1, value2);
Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
// Call the Multiply service operation.
result = client.Multiply(value1, value2);
Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
// Call the Divide service operation.
result = client.Divide(value1, value2);
Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
Console.WriteLine();
// Closing the client gracefully closes the connection and cleans up resources
client.Close();
}
}
}