服务说明示例演示服务如何在运行时检索其服务说明信息。本示例基于入门示例,和另一个定义为返回有关服务的说明信息的服务操作。返回的信息列出了服务的基址和终结点。服务使用 OperationContext、ServiceHost 和 ServiceDescription 类提供此信息。
在此示例中,客户端是一个控制台应用程序 (.exe),服务是由 Internet 信息服务 (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.
设置、生成和运行示例
若要生成 C# 或 Visual Basic .NET 版本的解决方案,请按照生成 Windows Communication Foundation 示例中的说明进行操作。
若要用单机配置或跨计算机配置来运行示例,请按照Running the Windows Communication Foundation Samples中的说明进行操作。
![]() |
---|
您的计算机上可能已安装这些示例。在继续操作之前,请先检查以下(默认)目录:
<安装驱动器>:\WF_WCF_Samples
如果此目录不存在,请访问针对 .NET Framework 4 的 Windows Communication Foundation (WCF) 和 Windows Workflow Foundation (WF) 示例(可能为英文网页),下载所有 Windows Communication Foundation (WCF) 和 WF 示例。此示例位于以下目录。
<安装驱动器>:\WF_WCF_Samples\WCF\Basic\Services\ServiceDescription
|