ServiceDescription 샘플은 서비스가 런타임에 서비스 설명 정보를 검색하는 방법을 보여 줍니다. 이 샘플은 서비스에 대한 설명 정보를 반환하기 위해 정의된 추가 서비스 작업과 함께 시작에 기반합니다. 반환되는 정보에는 서비스의 기본 주소 및 엔드포인트가 나열됩니다. 서비스는 OperationContext, ServiceHost, 및 ServiceDescription 클래스를 사용하여 이 정보를 제공합니다.
이 샘플에서 클라이언트는 콘솔 애플리케이션(.exe)이며 서비스는 IIS(인터넷 정보 서비스)에서 호스팅됩니다.
비고
이 샘플에 대한 설치 절차 및 빌드 지침은 이 항목의 끝에 있습니다.
이 샘플에는 IServiceDescriptionCalculator
라고 불리는 계산기 계약의 수정된 버전이 있습니다. 계약은 서비스에 대한 기본 주소 또는 주소와 서비스 엔드포인트 또는 엔드포인트를 설명하는 클라이언트에 여러 줄 문자열을 반환하는 추가 GetServiceDescriptionInfo
서비스 작업을 정의합니다.
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface IServiceDescriptionCalculator
{
[OperationContract]
int Add(int n1, int n2);
[OperationContract]
int Subtract(int n1, int n2);
[OperationContract]
int Multiply(int n1, int n2);
[OperationContract]
int Divide(int n1, int n2);
[OperationContract]
string GetServiceDescriptionInfo();
}
구현 코드 GetServiceDescriptionInfo
는 ServiceDescription을 사용하여 서비스 엔드포인트를 나열합니다. 서비스 엔드포인트는 상대 주소를 가질 수 있으므로 먼저 서비스의 기본 주소를 나열합니다. 이 모든 정보를 가져오기 위해 코드는 .를 사용하여 Current해당 작업 컨텍스트를 가져옵니다.
ServiceHost 및 해당 ServiceDescription 개체는 작업 컨텍스트에서 검색됩니다. 서비스의 기본 엔드포인트를 나열하기 위해 코드는 서비스 호스트의 BaseAddresses 컬렉션을 반복합니다. 서비스에 대한 서비스 엔드포인트를 나열하기 위해 코드는 서비스 설명의 엔드포인트 컬렉션을 반복합니다.
public string GetServiceDescriptionInfo()
{
string info = "";
OperationContext operationContext = OperationContext.Current;
ServiceHost host = (ServiceHost)operationContext.Host;
ServiceDescription desc = host.Description;
// Enumerate the base addresses in the service host.
info += "Base addresses:\n";
foreach (Uri uri in host.BaseAddresses)
{
info += " " + uri + "\n";
}
// Enumerate the service endpoints in the service description.
info += "Service endpoints:\n";
foreach (ServiceEndpoint endpoint in desc.Endpoints)
{
info += " Address: " + endpoint.Address + "\n";
info += " Binding: " + endpoint.Binding.Name + "\n";
info += " Contract: " + endpoint.Contract.Name + "\n";
}
return info;
}
샘플을 실행하면 계산기 작업이 수행되고, 이후에는 GetServiceDescriptionInfo
작업에 의해 반환된 서비스 정보가 표시됩니다. 클라이언트 창에서 Enter 키를 눌러 클라이언트를 종료합니다.
Add(15,3) = 18
Subtract(145,76) = 69
Multiply(9,81) = 729
Divide(22,7) = 3
GetServiceDescriptionInfo
Base addresses:
http://<machine-name>/ServiceModelSamples/service.svc
https://<machine-name>/ServiceModelSamples/service.svc
Service endpoints:
Address: http://<machine-name>/ServiceModelSamples/service.svc
Binding: WSHttpBinding
Contract: IServiceDescriptionCalculator
Address: http://<machine-name>/ServiceModelSamples/service.svc/mex
Binding: MetadataExchangeHttpBinding
Contract: IMetadataExchange
Press <ENTER> to terminate client.
샘플을 설정, 빌드 및 실행하려면
Windows Communication Foundation 샘플 에 대한One-Time 설정 절차를 수행했는지 확인합니다.
솔루션의 C# 또는 Visual Basic .NET 버전을 빌드하려면 Windows Communication Foundation 샘플빌드의 지침을 따릅니다.
단일 또는 컴퓨터 간 구성에서 샘플을 실행하려면 Windows Communication Foundation 샘플실행의 지침을 따릅니다.