Export (0) Print
Expand All

Channel Factory

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.

Note Note

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

This sample uses the ChannelFactory<TChannel> class to create a channel to a service endpoint. Typically, to create a channel to a service endpoint you generate a client type with the ServiceModel Metadata Utility Tool (Svcutil.exe) and create an instance of the generated type. You can also create a channel by using the ChannelFactory<TChannel> 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("http://localhost/servicemodelsamples/service.svc");
WSHttpBinding binding = new WSHttpBinding();
ChannelFactory<ICalculator> factory = new 
                    ChannelFactory<ICalculator>(binding, address);
ICalculator channel = factory.CreateChannel();
Important noteImportant

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, 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("http://localhost/servicemodelsamples/service.svc");
    

Important note Important

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

<InstallDrive>:\WF_WCF_Samples

If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.

<InstallDrive>:\WF_WCF_Samples\WCF\Basic\Client\ChannelFactory

Show:
© 2014 Microsoft