Clients


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.

The Svcutil utility needs metadata to create the proxy class. You’ve seen similar utilities: wsdl.exe with Web services and Soapsuds with .NET Remoting. The Svcutil utility can create a proxy from the MEX metadata endpoint, the metadata of the assembly, or WSDL and XSD documentation:

 svcutil http://localhost:8080/RoomReservation?wsdl /language:C# /out:proxy.cs svcutil CourseRegistration.dll svcutil CourseRegistration.wsdl CourseRegistration.xsd

The generated proxy class just needs to be instantiated, methods called, and finally the Close() method must be invoked:

  RoomServiceClient client = new RoomServiceClient(); client.RegisterForCourse(roomReservation); client.Close(); 

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.

  WsHttpBinding binding = new WsHttpBinding(); EndpointAddress address =       new EndpointAddress("http://localhost:8080/RoomService"); ChannelFactory<IRoomService> factory =       new ChannelFactory<IStateService>(binding, address); IRoomService channel = factory.CreateChannel(); channel.ReserveRoom(roomReservation); //... factory.Close(); 

The ChannelFactory<TChannel> class has several properties and methods, as shown in the following table.

Open table as spreadsheet

ChannelFactory Members

Description

Credentials

Credentials is a read-only property to access the ClientCredentials object that is assigned to the channel for authentication with the service. The credentials can be set with the endpoint.

Endpoint

Endpoint is a read-only property to access the ServiceEndpoint that is associated with the channel. The endpoint can be assigned in the constructor.

State

The State property is of type CommunicationState to return the current state of the channel. CommunicationState is an enumeration with the values Created, Opening, Opened, Closing, Closed, and Faulted.

Open()

The Open() method is used to open the channel.

Close()

The Close() method closes the channel.

Opening Opened Closing Closed Faulted

You can assign event handlers to get informed about state changes of the channel. Events are fired before and after the channel is opened, before and after the channel is closed, and in case of a fault.




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

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