A client application needs a proxy to access a service. There are three ways to create a proxy for the client:
Visual Studio Add Service Reference – This utility creates a proxy class from the metadata of the service.
ServiceModel Metadata Utility tool (Svcutil.exe) – You can create a proxy class with the svcutil utility. This utility reads metadata from the service to create the proxy class.
ChannelFactory class – This class is used by the proxy generated from svcutil; however, it can also be used to create a proxy programmatically.
Adding a service reference from Visual Studio requires accessing a WSDL document. The WSDL document is created by a MEX endpoint that needs to be configured with the service. With the following configuration, the endpoint with the relative address mex is using the mexHttpBinding and implements the contract IMetadataExchange. For accessing the metadata with an HTIP GET request, the behaviorConfiguration MexServiceBehaviorisconfigured.
Similar to the Add service reference from Visual Studio, the Svcutil utility needs metadata to create the proxy class. The Svcutil utility can create a proxy from the MEX metadata endpoint, the metadata of the assembly, or WSDL and XSD documentation:
After the proxy class is generated, it just needs to be instantiated from the client code, the methods need to be called, and finally the Close () method must be invoked:
The generated proxy class derives from the base class ClientBase<TChannel> that wraps the ChannelFactory<TChannel> class. Instead of using a generated proxy class, you can use the
ChannelFactory<TChannel> class directly. The constructor requires the binding and endpoint address; next, you can create the channel and invoke methods as defined by the service contract. Finally, the
factory must be closed:
The ChannelFactory<TChannel> class has several properties and methods, as shown in the following table.