次の方法で共有


Channel Factory

Download sample

This sample demonstrates how a client application can create a channel with the ChannelFactory class instead of a generated client. This sample is based on the Getting Started Sample that implements a calculator service.

NoteNote:

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

This sample uses the ChannelFactory class to create a channel to a service endpoint. Typically, to create a channel to a service endpoint a client type is generated with the Service Model Metadata Utility Tool (Svcutil.exe) and creates an instance of the generated type. You can also create a channel by using the ChannelFactory class, as demonstrated in this sample. The service created by the following sample code is identical to the service in the Getting Started Sample.

EndpointAddress address = new EndpointAddress("https://localhost/servicemodelsamples/service.svc");
WSHttpBinding binding = new WSHttpBinding();
ChannelFactory<ICalculator> factory = new 
                    ChannelFactory<ICalculator>(binding, address);
ICalculator channel = factory.CreateChannel();
Note Note:

If you are running this sample in a cross-machine scenario, you must replace "localhost" in the preceding code with the fully-qualified name of the machine that is running the service. This sample does not use configuration to set the endpoint address, so this must be done in code.

Once the channel is created, its service operations can be invoked just as with a generated client.

// Call the Add service operation.
double value1 = 100.00D;
double value2 = 15.99D;
double result = channel.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

To close the channel, it must first be cast to an IClientChannel interface. This is because the channel as generated is declared in the client application using the ICalculator interface, which has methods like Add and Subtract but not Close. The Close method originates on the ICommunicationObject interface.

// Close the channel.
 ((IClientChannel)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 application.

Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714

Press <ENTER> to terminate client.

To set up, build, and run the sample

  1. Ensure that you have performed the One-Time Setup 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. Note that this sample does not enable metadata publishing. You must first enable metadata publishing for this sample to regenerate the client type.

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

To run the sample cross machine

  • Replace "localhost" in the following code with the fully-qualified name of the machine that is running the service.

    EndpointAddress address = new EndpointAddress("https://localhost/servicemodelsamples/service.svc");
    

Footer image

Send comments about this topic to Microsoft.
© Microsoft Corporation. All rights reserved.