Building Blocks for a WCF Service Web Site - Proxy Initialization
(Page 3 of 4 )
For a client to invoke service operations, it must open a communication channel to a particular service endpoint. This channel is bound to a particular endpoint--its address, binding, and contract. This is done by creating a proxy.
In the first lab, a proxy is created directly by using the channel factory:
IHelloIndigoService proxy =
ChannelFactory<IHelloIndigoService>. CreateChannel(new BasicHttpBinding(), new
EndpointAddress("http://localhost:8000/HelloIndigo/ HelloIndigoService"));
This approach assumes that:
- You have prior knowledge of the endpoint address.
- A copy of the service contract definition is locally available.
- You have prior knowledge of the required protocols or binding configuration.
If you own both sides (client and service), it is feasible to share an assembly that contains service metadata and to separately communicate the address and binding requirements. When you don't own both sides, generating the proxy is a more effective way to import the metadata necessary to construct the client channel. In this lab, the proxy is generated with SvcUtil. This proxy includes a generated copy of the service contract and a channel wrapper to simplify the client code necessary to consume the service. SvcUtil also generates the service model configuration necessary to initialize the proxy.
The service contract generated by SvcUtil looks similar to the contract at the service with the exception of additional details specified in theServiceContractAttributeandOperationContractAttribute, shown inExample 1-12 .
Example 1-12. Service contract generated by SvcUtil
[System.ServiceModel.ServiceContractAttribute(Namespace= "http://www.thatindigogirl.com/samples/2006/06", ConfigurationName="Client.localhost. IHelloIndigoService")]
public interface IHelloIndigoService
{
[System.ServiceModel.OperationContractAttribute(Action=
"http://www.thatindigogirl.com/samples/2006/ 06/IHelloIndigoService/HelloIndigo", ReplyAction= "http://www.thatindigogirl.com/samples/2006/06
/IHelloIndigoService/HelloIndigoResponse")]
string HelloIndigo();
}
The namespace specified by theServiceContractAttributeis the same as at the service. This is critical to compatible message serialization.
The SvcUtil that generated the client-side service contract also generated a proxy type. The proxy type is a partial class that inheritsClientBase<T>from theSystem.ServiceModelnamespace (T is the service contract type). As shown in bold in Example 1-13, the proxy exposes service contract operations and internally uses its reference to the client communication channel to invoke each service operation. In fact, this inner channel reference is like the one you previously created with the channel factory.
Example 1-13. Proxy type generated by SvcUtil
public partial class HelloIndigoServiceClient: System.ServiceModel.ClientBase<Client. localhost.IHelloIndigoService>, Client.localhost.IHelloIndigoService
{
...overloaded constructors
public string HelloIndigo()
{
return base.Channel.HelloIndigo();
}
}
When the first operation is invoked on this proxy, the inner channel is created based on the endpoint configuration for the proxy. Since in this lab only one endpoint is available, the construction of the proxy looks something like this:
HelloIndigoServiceClient proxy = new HelloIndigoServiceClient();
If there are several endpoints to choose from in the<client>configuration section, you are required to provide an endpoint configuration name to the proxy constructor:
// Proxy construction
HelloIndigoServiceClient proxy = new HelloIndigoServiceClient("basicHttp");
// Endpoint configuration
<endpoint address=http://localhost:8001/HelloIndigo/ HelloIndigoService
binding="basicHttpBinding" name="basicHttp" contract="Host.IHelloIndigoService" />
Once the client channel has been used (by invoking an operation), the proxy is bound to the endpoint configuration that initialized it. The same proxy instance cannot be used to invoke another service endpoint, and no changes to protocols or behaviors are allowed. A new proxy (channel) must be constructed if such changes are required.
Next: Hosting a Service in IIS >>
More BrainDump Articles
More By O'Reilly Media
|
This article is excerpted from chapter 1 of the book Learning WCF A Hands-on Guide, written by Michele Leroux Bustamante (O'Reilly, 2007; ISBN: 0596101627). Check it out today at your favorite bookstore. Buy this book now.
|
|