방법: 검색 프록시에 등록할 검색 가능한 서비스 구현
이 항목은 검색 프록시를 구현하는 방법에 대해 설명하는 네 항목 중 두 번째 항목입니다. 이전 항목인 방법: 검색 프록시 구현에서 검색 프록시를 구현했습니다. 이 항목에서는 검색 프록시에 알림 메시지(Hello
및 Bye
)를 보내는 WCF 서비스를 만듭니다. 이러한 알림 메시지는 서비스가 검색 프록시에 해당 서비스를 등록하거나 등록 취소하는 데 사용됩니다.
서비스 계약을 정의하려면
DiscoveryProxyExample
솔루션에Service
라는 새 콘솔 애플리케이션 프로젝트를 추가합니다.다음 어셈블리에 대한 참조를 추가합니다.
System.ServiceModel
System.ServiceModel.Discovery
CalculatorService
라는 프로젝트에 새 클래스를 추가합니다.다음
using
지시문을 추가합니다.using System; using System.ServiceModel;
CalculatorService.cs 내에서 서비스 계약을 정의합니다.
// Define a service contract. [ServiceContract(Namespace = "http://Microsoft.Samples.Discovery")] public interface ICalculatorService { [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); }
CalculatorService.cs 내에서 서비스 계약을 구현합니다.
// Service class which implements the service contract. public class CalculatorService : ICalculatorService { public double Add(double n1, double n2) { double result = n1 + n2; Console.WriteLine("Received Add({0},{1})", n1, n2); Console.WriteLine("Return: {0}", result); return result; } public double Subtract(double n1, double n2) { double result = n1 - n2; Console.WriteLine("Received Subtract({0},{1})", n1, n2); Console.WriteLine("Return: {0}", result); return result; } public double Multiply(double n1, double n2) { double result = n1 * n2; Console.WriteLine("Received Multiply({0},{1})", n1, n2); Console.WriteLine("Return: {0}", result); return result; } public double Divide(double n1, double n2) { double result = n1 / n2; Console.WriteLine("Received Divide({0},{1})", n1, n2); Console.WriteLine("Return: {0}", result); return result; } }
서비스를 호스트하려면
프로젝트를 만들 때 생성된 Program.cs 파일을 엽니다.
다음
using
지시문을 추가합니다.using System; using System.ServiceModel; using System.ServiceModel.Description; using System.ServiceModel.Discovery;
Main()
메서드 안에서 다음 코드를 추가합니다.// Define the base address of the service Uri baseAddress = new Uri("net.tcp://localhost:9002/CalculatorService/" + Guid.NewGuid().ToString()); // Define the endpoint address where announcement messages will be sent Uri announcementEndpointAddress = new Uri("net.tcp://localhost:9021/Announcement"); // Create the service host ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress); try { // Add a service endpoint ServiceEndpoint netTcpEndpoint = serviceHost.AddServiceEndpoint(typeof(ICalculatorService), new NetTcpBinding(), string.Empty); // Create an announcement endpoint, which points to the Announcement Endpoint hosted by the proxy service. AnnouncementEndpoint announcementEndpoint = new AnnouncementEndpoint(new NetTcpBinding(), new EndpointAddress(announcementEndpointAddress)); // Create a ServiceDiscoveryBehavior and add the announcement endpoint ServiceDiscoveryBehavior serviceDiscoveryBehavior = new ServiceDiscoveryBehavior(); serviceDiscoveryBehavior.AnnouncementEndpoints.Add(announcementEndpoint); // Add the ServiceDiscoveryBehavior to the service host to make the service discoverable serviceHost.Description.Behaviors.Add(serviceDiscoveryBehavior); // Start listening for messages serviceHost.Open(); Console.WriteLine("Calculator Service started at {0}", baseAddress); Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate the service."); Console.WriteLine(); Console.ReadLine(); serviceHost.Close(); } catch (CommunicationException e) { Console.WriteLine(e.Message); } catch (TimeoutException e) { Console.WriteLine(e.Message); } if (serviceHost.State != CommunicationState.Closed) { Console.WriteLine("Aborting the service..."); serviceHost.Abort(); }
검색 가능한 서비스의 구현을 완료했습니다. 방법: 검색 프록시를 사용하여 서비스를 찾는 클라이언트 애플리케이션 구현을 계속 진행합니다.
예시
다음은 이 항목에서 사용되는 전체 코드 목록입니다.
// CalculatorService.cs
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------
using System;
using System.ServiceModel;
namespace Microsoft.Samples.Discovery
{
// Define a service contract.
[ServiceContract(Namespace = "http://Microsoft.Samples.Discovery")]
public interface ICalculatorService
{
[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);
}
// Service class which implements the service contract.
public class CalculatorService : ICalculatorService
{
public double Add(double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine("Received Add({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
public double Subtract(double n1, double n2)
{
double result = n1 - n2;
Console.WriteLine("Received Subtract({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
public double Multiply(double n1, double n2)
{
double result = n1 * n2;
Console.WriteLine("Received Multiply({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
public double Divide(double n1, double n2)
{
double result = n1 / n2;
Console.WriteLine("Received Divide({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
}
}
// Program.cs
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Discovery;
namespace Microsoft.Samples.Discovery
{
class Program
{
public static void Main()
{
Uri baseAddress = new Uri("net.tcp://localhost:9002/CalculatorService/" + Guid.NewGuid().ToString());
Uri announcementEndpointAddress = new Uri("net.tcp://localhost:9021/Announcement");
ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress);
try
{
ServiceEndpoint netTcpEndpoint = serviceHost.AddServiceEndpoint(typeof(ICalculatorService),
new NetTcpBinding(), string.Empty);
// Create an announcement endpoint, which points to the Announcement Endpoint hosted by the proxy service.
AnnouncementEndpoint announcementEndpoint = new AnnouncementEndpoint(new NetTcpBinding(),
new EndpointAddress(announcementEndpointAddress));
ServiceDiscoveryBehavior serviceDiscoveryBehavior = new ServiceDiscoveryBehavior();
serviceDiscoveryBehavior.AnnouncementEndpoints.Add(announcementEndpoint);
// Make the service discoverable
serviceHost.Description.Behaviors.Add(serviceDiscoveryBehavior);
serviceHost.Open();
Console.WriteLine("Calculator Service started at {0}", baseAddress);
Console.WriteLine();
Console.WriteLine("Press <ENTER> to terminate the service.");
Console.WriteLine();
Console.ReadLine();
serviceHost.Close();
}
catch (CommunicationException e)
{
Console.WriteLine(e.Message);
}
catch (TimeoutException e)
{
Console.WriteLine(e.Message);
}
if (serviceHost.State != CommunicationState.Closed)
{
Console.WriteLine("Aborting the service...");
serviceHost.Abort();
}
}
}
}