Udostępnij za pośrednictwem


Typed Client

Download sample

The sample demonstrates how to obtain information from a typed client generated by the Service Model Metadata Utility Tool (Svcutil.exe). This sample is based on the Getting Started Sample that implements a calculator service. In this sample, the client is a console application (.exe) and the service is hosted by Internet Information Services (IIS).

Note

The WCF samples may already be installed on your machine. Check for the following (default) directory before continuing.

<InstallDrive>:\Samples\WCFWFCardspace

If this directory does not exist, click the download sample link at the top of this page. Note that this downloads and installs all of the WF, WCF, and CardSpace samples. The sample is located in the following directory.

<InstallDrive>:\Samples\WCFWFCardSpace\WCF\Basic\Client\TypedClient

Note

The set-up procedure and build instructions for this sample are located at the end of this topic.

The client's Endpoint property allows access to information about the service endpoint that the client is communicating with—including address, binding and contract information. The client's InnerChannel property is an instance of IClientChannel that allows access to information about the underlying channel, such as its state and session identifier.

// 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();

When you run the sample, the operation requests and responses are displayed in the client console window. Press ENTER in the client window to shut down the client.

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:  https://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.

To set up, build, and run the sample

  1. Ensure that you have performed the One-Time Set Up Procedure for the Windows Communication Foundation Samples.

  2. To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.

© 2007 Microsoft Corporation. All rights reserved.