Hosting C# Help

WCF is very flexible when choosing a host to run the service. The host can be a Windows service, a COM+ application, WAS (Windows Activation Services) or lIS, a Windows application, or just a simple console application. When creating a custom host with Windows Forms or WPF, you can easily create a peer-to-peer solution.

Custom Hosting

Let’s start with a custom host. The sample code shows hosting of a service within a console application however, in other custom host types such as Windows services or Windows applications you can
program the service in the same way.

In the Main () method, a Service Host instance is created. After the ServiceHost:t instance is created, the application configuration file is read to define the bindings. You can also define the bindings
programmatically, as shown earlier. Next, the Open() method of the ServiceHost class is invoked, so the vice accepts client calls. With a console application, you need to be careful not to close the main
thread until the service should be closed. Here, the user is asked to “press return” to exit the service. When the user does this, the Close () method is called to actually end the service:

Capture

To abort the service host, you can invoke the Abort() method of the ServiceHost class. To get the current state of the service, the State property returns a value defined by the Communication State enumeration. Possible values are Created, Opening, Opened, Closing, Closed, and Faulted.

If you start the service from within a Windows Forms or WPF application and the service code invokes methods of Windows control, you mUit be sure that only the ,control’s creator thread is allowed to access the methods and properties of the control. With WCF, this behavior can be achieved easily by setting the Use Synchronizaton Context property of the attribute [ServiceBehavior] .

WAS HostIng

With WAS (Windows Activation Services) hosting, you get the features from the WAS worker process such as automatic activation of the service, health monitoring, and process recycling.

To use WAS hosting, you just need to create a Web site and a . svc file with the ServiceHost declaration that includes the language and the name of the service class. The code shown here is using the class Service1. ln addition, you must specify the file that contains the service class. This class is implemented in the same way as you saw earlier when defining a WCF service library.

<iServieeHost language=’C.’ Service=’Service1′
CodeBehind=’·Servicel. svc. cs’ \>

If you use a WCF service library that should be available from WAS hosting, you can create a . svc file that just contains a reference to the class:

With Windows Vista and Windows Server 2008,WAS allows defining .NET TCP and Message Queue bindings. If you are using the previous edition, IIS 6 or lIS 5.1 that is available with Windows Server 2003 and Wmdows XP, activation from a . svc file can be done only with an HTTP binding.

You can also add a WCF service to Enterprise Service components. This is discussed in Chapter 44.

Posted on October 31, 2015 in Uncategorized

Share the Story

Back to Top