15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Clients ❘ 1305<br />

set the value below to false <strong>and</strong> remove the metadata endpoint above<br />

before deployment — ><br />

<br />

<br />

<br />

<br />

<br />

<br />

Similar to the Add service reference from Visual Studio, the Svcutil utility needs metadata to create the<br />

proxy class. The Svcutil utility can create a proxy from the MEX metadata endpoint, the metadata of<br />

the assembly, or WSDL <strong>and</strong> XSD documentation:<br />

svcutil http://localhost:8080/RoomReservationwsdl /language:<strong>C#</strong> /out:proxy.cs<br />

svcutil CourseRegistration.dll<br />

svcutil CourseRegistration.wsdl CourseRegistration.xsd<br />

After the proxy class is generated, it just needs to be instantiated from the client code, the methods need to<br />

be called, <strong>and</strong> finally the Close() method must be invoked:<br />

var client = new RoomServiceClient();<br />

client.RegisterForCourse(roomReservation);<br />

client.Close();<br />

The generated proxy class derives from the base class ClientBase that wraps the Channel<br />

Factory class. Instead of using a generated proxy class, you can use the ChannelFactory<br />

class directly. The constructor requires the binding <strong>and</strong> endpoint address; next, you can create the channel <strong>and</strong><br />

invoke methods as defined by the service contract. Finally, the factory must be closed:<br />

var binding = new WsHttpBinding();<br />

var address = new EndpointAddress("http://localhost:8080/RoomService");<br />

var factory = new ChannelFactory(binding, address);<br />

IRoomService channel = factory.CreateChannel();<br />

channel.ReserveRoom(roomReservation);<br />

//.<br />

factory.Close();<br />

The ChannelFactory class has several properties <strong>and</strong> methods, as shown in the<br />

following table.<br />

ChannelfaCTory members<br />

Credentials<br />

Endpoint<br />

State<br />

Open()<br />

Close()<br />

Opening Opened Closing<br />

Closed Faulted<br />

desCriPTion<br />

Credentials is a read-only property to access the ClientCredentials<br />

object that is assigned to the channel for authentication with the service. The<br />

credentials can be set with the endpoint.<br />

Endpoint is a read-only property to access the ServiceEndpoint that is<br />

associated with the channel. The endpoint can be assigned in the constructor.<br />

The State property is of type CommunicationState <strong>and</strong> returns the current<br />

state of the channel. CommunicationState is an enumeration with the values<br />

Created, Opening, Opened, Closing, Closed, <strong>and</strong> Faulted.<br />

The Open() method is used to open the channel.<br />

The Close() method closes the channel.<br />

You can assign event h<strong>and</strong>lers to get informed about state changes of the<br />

channel. Events are fired before <strong>and</strong> after the channel is opened, before <strong>and</strong><br />

after the channel is closed, <strong>and</strong> in case of a fault.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!