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.

Hosting ❘ 1303<br />

To abort the service host, you can invoke the Abort() method of the ServiceHost class. To get the current<br />

state of the service, the State property returns a value defi ned by the CommunicationState enumeration.<br />

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

If you start the service from within a Windows Forms or WPF application <strong>and</strong><br />

the service code invokes methods of Windows controls, you must be sure that<br />

only the control’s creator thread is allowed to access the methods <strong>and</strong> properties<br />

of the control. With WCF, this behavior can be achieved easily by setting the<br />

UseSynchronizationContext property of the attribute [ServiceBehavior].<br />

Was hosting<br />

With WAS (Windows Activation Services) hosting, you get the features from the WAS worker process such<br />

as automatic activation of the service, health monitoring, <strong>and</strong> process recycling.<br />

To use WAS hosting, you just need to create a web site <strong>and</strong> a .svc fi le with the ServiceHost declaration<br />

that includes the language <strong>and</strong> the name of the service class. The code shown here is using the class<br />

Service1 . In addition, you must specify the fi le that contains the service class. This class is implemented in<br />

the same way that you saw earlier when defi ning a WCF service library.<br />

< %@ServiceHost language="<strong>C#</strong>" Service="Service1" CodeBehind="Service1.svc.cs" % ><br />

If you use a WCF service library that should be available from WAS hosting, you can create a .svc fi le that<br />

just contains a reference to the class:<br />

< %@ ServiceHost Service="Wrox.ProCSharp.WCF.Services.RoomReservationService" % ><br />

Since the introduction of Windows Vista <strong>and</strong> Windows Server 2008, WAS allows defi ning .<strong>NET</strong> TCP <strong>and</strong><br />

Message Queue bindings. If you are using the previous edition, IIS 6 or IIS 5.1, which is available with<br />

Windows Server 2003 <strong>and</strong> Windows XP, activation from a .svc fi le can be done only with an HTTP binding.<br />

You can also add a WCF service to Enterprise Service components. This is discussed in<br />

Chapter 51.<br />

Preconfigured host Classes<br />

To reduce the confi guration necessities, WCF also offers some hosting classes with preconfi gured<br />

bindings. One example is located in the assembly System.ServiceModel.Web in the namespace System<br />

.ServiceModel.Web with the class WebServiceHost . This class creates a default endpoint for HTTP <strong>and</strong><br />

HTTPS base addresses if a default endpoint is not confi gured with the WebHttpBinding . Also, this class<br />

adds the WebHttpBehavior if another behavior is not defi ned. With this behavior, simple HTTP GET <strong>and</strong><br />

POST , PUT , DELETE (with the WebInvoke attribute) operations can be done without additional setup.<br />

Uri baseAddress = new Uri("http://localhost:8000/RoomReservation");<br />

var host = new WebServiceHost(typeof(RoomReservationService), baseAddress);<br />

host.Open();<br />

Console.WriteLine("service running");<br />

Console.WriteLine("Press return to exit...");<br />

Console.ReadLine();<br />

if (host.State == CommunicationState.Opened)<br />

host.Close();<br />

code snippet RoomReservationWebServiceHost/Program.cs<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!