TypedClient 示例演示如何从 ServiceModel 元数据实用工具工具(Svcutil.exe)生成的类型化客户端获取信息。 此示例基于《入门指南》,实现了计算器服务。 在此示例中,客户端是一个控制台应用程序 (.exe),服务是由 Internet 信息服务 (IIS) 承载的。
注释
本示例的设置过程和生成说明位于本主题末尾。
客户端 Endpoint 的属性允许访问客户端与之通信的服务终结点的相关信息,包括地址、绑定和协定信息。 客户端 InnerChannel 的属性是一个实例 IClientChannel ,允许访问有关基础通道的信息,例如其状态和会话标识符。
// Create a client.
CalculatorClient client = new CalculatorClient();
...
Console.WriteLine("Client - endpoint: " + client.Endpoint.Address);
Console.WriteLine("Client - binding: " + client.Endpoint.Binding.Name);
Console.WriteLine("Client - contract: " + client.Endpoint.Contract.Name);
IClientChannel channel = client.InnerChannel;
Console.WriteLine("Client channel - state: " + channel.State);
Console.WriteLine("Client channel - session identifier: " + channel.SessionId);
//Closing the client gracefully closes the connection and cleans up resources.
client.Close();
运行示例时,操作请求和响应将显示在客户端控制台窗口中。 在客户端窗口中按 Enter 关闭客户端。
Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714
Client - endpoint: http://localhost/servicemodelsamples/service.svc
Client - binding: WSHttpBinding
Client - contract: ICalculator
Client channel - state: Opened
Client channel - session identifier: urn:uuid:ae16fbc4-2964-4e87-9fb1-c5aa78fc567e
Press <ENTER> to terminate client.
设置、生成和运行示例
确保已为 Windows Communication Foundation 示例 执行One-Time 安装过程。
若要生成解决方案的 C# 或 Visual Basic .NET 版本,请按照 生成 Windows Communication Foundation 示例中的说明进行操作。
若要在单台计算机或跨计算机配置中运行示例,请按照 运行 Windows Communication Foundation 示例中的说明进行操作。