Services and the WCF - Creating a Client Proxy (Page 4 of 4 )
Clients use a proxy to consume a service endpoint. A proxy can be created manually using a channel factory, or it can be generated using tools. This lab explores the former and shows you the bare necessities required to communicate with a service:
- The address of the service endpoint
- The protocols required to communicate with the service endpoint, or the binding
- The service contract metadata as described by the service contract associated with the endpoint
Essentially, the client proxy requires information about the service endpoint it wishes to consume. In this lab, you learned how to manually create the proxy usingChannelFactory<T>, as shown here:
EndpointAddress ep = new
EndpointAddress("http://localhost:8000/HelloIndigo/ HelloIndigoService");
IHelloIndigoService proxy = ChannelFactory<IHelloIndigoService>.
CreateChannel(new BasicHttpBinding(), ep);
ChannelFactory<T>is a service model type that can generate the client proxy and underlying channel stack. You provide the address, binding, and service contract type and callCreateChannel()to generate the channel stack discussed earlier. In this lab, you made a copy of the service contract (not the implementation) in the client application in order to supply it as the generic parameter type toChannelFactory<T>. The address and binding supplied matched those of the service. The result is that the client proxy knows where to send messages, what protocols to use, and which
operations it can call.
In order for communication between client and service to succeed, the binding must be equivalent to the binding specified for the service endpoint. Equivalence means that the transport protocol is the same, the message-encoding format is the same, and any additional messaging protocols used at the service to serialize messages are also used at the client. This lab achieves this by applying the same standard binding,BasicHttpBinding, at the client and service--thus, they are equivalent. Another requirement for successful communication is that the service contract used to initialize the proxy has equivalent operation signatures and namespace definitions. This is achieved in this lab by making an exact copy of the service contract at the client.
You may be wondering: how can the client discover the correct address, binding, and contract associated with a service endpoint? In the next lab, you'll learn how to generate client proxies and configuration to consume a service without having access to the service code base.
Generating a Service and Client Proxy
In the previous lab, you created a service and client from scratch without leveraging the tools available to WCF developers. Although this helps you to understand the raw requirements for sending messages between clients and services, in reality, developers need tools to be productive. This time around, I'll show you how to use several such tools that help you to generate services, access metadata, create configuration settings, and generate proxies. Specifically, you'll use the following:
- Visual Studio service templates
- Service Configuration Editor
- ServiceModel Metadata Utility (SvcUtil)
The primary goal of the lab in this section will be to improve your productivity for building clients and services, but several other concepts will be discussed in the process. To begin with, you'll use declarative configuration settings instead of code to configure the service host and client. To enable proxy generation, you'll access service metadata, which involves enabling a service behavior. In addition, you'll learn more about service configuration settings for base addresses, endpoints, bindings and behaviors.
After you complete the lab, I'll spend some time discussing these concepts.
Please check back next week for the continuation of this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
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.
|
|