15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

simple service <strong>and</strong> Client ❘ 1289<br />

Adding a service reference adds references to the assemblies System.Runtime.Serialization <strong>and</strong><br />

System.ServiceModel <strong>and</strong> a configuration file containing the binding information <strong>and</strong> the endpoint<br />

address to the service.<br />

From the data contract the class RoomReservation is generated. This class contains all [DataMember]<br />

elements of the contract. The class RoomServiceClient is the proxy for the client that contains methods that<br />

are defined by the service contract. Using this client, you can send a room reservation to the running service.<br />

private void OnReserveRoom(object sender, RoutedEventArgs e)<br />

{<br />

var reservation = new RoomReservation()<br />

{<br />

RoomName = textRoom.Text,<br />

Event = textEvent.Text,<br />

Contact = textContact.Text,<br />

StartDate = DateTime.Parse(textStartTime.Text),<br />

EndDate = DateTime.Parse(textEndTime.Text)<br />

};<br />

}<br />

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

bool reserved = client.ReserveRoom(reservation);<br />

client.Close();<br />

if (reserved)<br />

MessageBox.Show("reservation ok");<br />

code snippet RoomReservationClient/MainWindow.xaml.cs<br />

By running both the service <strong>and</strong> the client, you can add room reservations to the database. With the<br />

settings of the RoomReservation solution, you can configure multiple startup projects, which should be<br />

RoomReservationClient <strong>and</strong> RoomReservationHost in this case.<br />

diagnostics<br />

When running a client <strong>and</strong> service application, it can be very helpful to know what’s happening behind<br />

the scenes. For this, WCF makes use of a trace source that just needs to be configured. You can configure<br />

tracing using the Service Configuration Editor, selecting Diagnostics, <strong>and</strong> enabling Tracing <strong>and</strong> Message<br />

Logging. Setting the trace level of the trace sources to Verbose produces very detailed information. This<br />

configuration change adds trace sources <strong>and</strong> listeners to the application configuration file as shown here:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!