The Client


The ServiceModel layer on the sender is simpler than the ServiceModel layer on the receiver as a result of the relative simplicity of sending a Message versus receiving and dispatching a Message. Even though the tasks are much simpler, the ServiceModel infrastructure on the sender has some symmetry with the ServiceModel infrastructure on the receiver. As mentioned earlier in this chapter, much of the ServiceModel layer infrastructure on the sender is called the client. Like the dispatcher, the client is not composed of one type, but rather is a mosaic of other types, and the subtasks required to send a Message are delegates to these types.

When describing the dispatcher, we start by describing how a Message is read from the channel stack and how the channel listener is managed. From the perspective of the receiving application, the receipt of a Message initiates work in the dispatcher. With the client, user code initiates action within the client. The client then uses a binding, an EndpointAddress, and contractual information to send a Message. As you now know about channels, there must be a channel stack in place before we send a Message. And the only way to create sending channels is via an IChannelFactory-derived type. The client infrastructure manages all of this. In a manner consistent with what you’ve learned so far, the client uses a binding to create a stack of channel factories and then uses that stack of channel factories to create a channel stack. Once the channel stack is in place, the client then creates a Message and sends it to the channel stack for delivery to another messaging participant.

The only twist in the sequence of events is how the client exposes types that are consistent with the contract of the service that it sends messages to. There are two types central in making the client infrastructure consistent with the service contract of the receiving application. They are the System.ServiceModel.ChannelFactory<TChannel> type and the System.ServiceModel.ClientBase<TChannel> type. Do not confuse the ChannelFactory<TChannel> type with the stack of channel factory objects that creates the channel stack. The ChannelFactory<TChannel> creates the client infrastructure required to send a message to another endpoint and can be created by user code. The channel factory objects on the other hand, build the channel stack and can be created only by a Binding or a BindingElement.

Let’s look at how we can use the ChannelFactory<TChannel> type and then discuss how it works internally. The following code snippet shows how to use the ChannelFactory<TChannel> type:

 using System; using System.ServiceModel; using System.ServiceModel.Channels; using System.Runtime.Remoting;  internal sealed class Sender{   static void Main() {     // instantiate a binding     BasicHttpBinding binding = new BasicHttpBinding();     // create an EndpointAddress     EndpointAddress address =       new EndpointAddress("http://localhost:4000/IRestaurantService");     // instantiate a ChannelFactory, passing binding and EndpointAddress     ChannelFactory<IRestaurantService3> factory =       new ChannelFactory<IRestaurantService3>(binding, address);     // create the client infastructure     IRestaurantService3 client = factory.CreateChannel();     Boolean trans = RemotingServices.IsTransparentProxy(client);     // prints "true"     Console.WriteLine("IsTransparentProxy: {0}", trans);     // invoke a method on the client, and retrieve the result     Int32? result =       client.RequestReservation(new RequestReservationParams(DateTime.Now,                                                              "Dusty's BBQ",                                                              "Justin"));   } }

As you can see from this example, the ChannelFactory<TChannel> type accepts a Binding and an EndpointAddress as arguments, and a service contract can be the TChannel generic parameter. When the CreateChannel method is called, the ChannelFactory<TChannel> type uses reflection to generate transparent proxy that is of type TChannel. Note that this method does not actually create any channels. When we call one of the methods on the transparent proxy, the binding is used to create the channel factory stack and the subsequent channel stack.




Inside Windows Communication Foundation
Inside Windows Communication Foundation (Pro Developer)
ISBN: 0735623066
EAN: 2147483647
Year: 2007
Pages: 106
Authors: Justin Smith

Similar book on Amazon
Learning WCF: A Hands-on Guide
Learning WCF: A Hands-on Guide
Essential Windows Communication Foundation (WCF): For .NET Framework 3.5
Essential Windows Communication Foundation (WCF): For .NET Framework 3.5
Programming WCF Services: Mastering WCF and the Azure AppFabric Service Bus
Programming WCF Services: Mastering WCF and the Azure AppFabric Service Bus
Microsoft  Windows  Communication Foundation Step by Step (Step By Step Developer Series)
Microsoft Windows Communication Foundation Step by Step (Step By Step Developer Series)

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net