Hosting and Deploying a WCF Service

For your services to accept requests, they have to be active and running, which means they need to be hosted in some runtime environment. Recall that when we covered web services, they were hosted for us by IIS. You can host your WCF services there, too. However, you do have other options.

You want to pick your host based on your needs. For example, if you have a peer-to-peer application, you might already know that each peer can host its own services. You also need to consider issues such as deployment, flexibility, monitoring, process lifetime management, security, and more. Here is a brief overview of the WCF host options available to you:

Image Self-hosted—A self-hosted service contains the service within a running executable. The executable is managed code you write. You simply embed one or more services within the executable. In this way, the service is self-hosted. It does not require an additional process to execute. Instead, its lifetime is managed by the lifetime of the executable. When the executable is running, the service is listening for requests and responding accordingly. If not, the service is out of commission.

Self-hosted services are great when your clients need to communicate with one another. This is the case with peer-to-peer applications like Microsoft’s Groove. Each client has services that can speak with the other clients.

To create a self-hosted service, you create an instance of the ServiceHost class inside your application. This class is passed an instance of your service. You then call the Open method of ServiceHost to begin hosting the service. When you’re finished, you call the Close method.

Image Windows service—You can host your WCF service inside a Windows service application. A Windows service application is one that is installed as a service on a given machine. A Windows service can be configured to start, stop, and start up again on system reboot. In this way, it is reliable and robust when you need a service to simply stay up and running. It is also supported on all versions of Windows and Windows server.

To create a Windows service to host your WCF service, you create a class derived from ServiceBase. You then override the OnStart and OnStop methods to set up your service host. In the OnStart method, you create a global ServiceHost instance and then call the Open method to begin listening for requests. You then simply call the Close method in the OnStop method.

Finally, you create an Installer class for your service to install it in a machine’s service directory. This class derives from the Installer class. You then compile the code and run installutil to get the service installed on a machine.

Image IIS—IIS can host your WCF services. In this way, you can take advantage of the many features built in to this platform, including monitoring, high availability, high scalability, and more. You saw how to create services hosted in a website in the preceding section.

Image WAS (Windows Process Activation Service)—WAS was introduced with Windows Server 2008. It gives you the benefits of IIS (health monitoring, process recycling, message-based activation, and so on) without the limitations of HTTP only. WAS works with HTTP, TCP, named pipes, and MSMQ. In addition, WAS does not require that you write hosting code (like the self-hosted and Windows Service options do). Instead, you simply configure WAS to be a host of your service (as you would IIS).

As you can see, you have many options for hosting your service. Each has its own plusses and minuses with respect to setup, coding, configuration, and deployment. Depending on your needs, spend some time learning more about your host options. You can find a how-to on each option inside the Microsoft Developer Network (MSDN). Simply search for “WCF hosting options.”

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset