Windows Communication Foundation's hosting environment

Windows Communication Foundation (WCF) applications need to have a runtime environment for the management of its object's lifecycle. The host process models allows is a new concept in WCF referring to WCF hosting environment which can have several services, and the same service can be hosted in several host processes. Having a separate process is immaterial who provides the process or what kind of a process is involved. The host can be provided by IIS, by the Widows Activation Service (WAS) on Windows Vista, or by the developer as part of the application.

IIS Hosting is the environment managed by Microsoft Internet Information Server (IIS) web server. This host process is launched automatically upon the first client request, and you rely on IIS to manage the life cycle of the host process and the underlying service's life cycle. The main disadvantage of IIS hosting is that you can only use HTTP services and the restriction of having all services use the same port number. Hosting in IIS is very similar to hosting a classic ASMX web service. You need to create a virtual directory under IIS and supply a service component. In WCF naming convention, you must add the svc extension to the service component's file.

Self Hosting is the environment managed by any Windows process, such as a Windows Forms application, a Console application, or a Windows NT Service. Note that the process must be running before the client calls the service, which typically means you have to pre-launch it, as shown in Listing 1. This is not an issue for NT Services or in-proc.

using System;

using System.Collections.Generic;

using System.Text;

using System.ServiceModel;

 

namespace WCFConsoleApplication

{

    internal class MyServiceHost

    {

        internal static ServiceHost myServiceHost = null;

        internal static void StartService()

        {

            // Consider putting the baseAddress in the configuration system

            // and getting it here with AppSettings

            Uri baseAddress = new Uri("http://localhost:8080/WCFConsoleApplication/testservice");

            // Instantiate new ServiceHost

            myServiceHost = new ServiceHost(typeof(testservice), baseAddress);

            myServiceHost.Open();

        }

        internal static void StopService()

        {

            // Call StopService from your shutdown logic (i.e. dispose method)

            if (myServiceHost.State != CommunicationState.Closed)

                myServiceHost.Close();

        }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

        }

    }

}

Listing 1. A self hosting service in a console application.

You need to create an instance of the class ServiceHost and pass in the constructor the service type and optionally the default base addresses. The set of base addresses can be an empty set. Even if you provide base addresses, the service can be configured to use different base addresses. Note that each ServiceHost instance is associated with a particular service type, and if the host process needs to host multiple types of services, you will need to create several ServiceHost instances. By calling the Open( ) method on the service host, you are activating the WCF runtime which runs several work thread to listen for incoming requests.

It's possible to register multiple addresses as shown in Listing 2.

Uri tcpBaseAddress  = new Uri("net.tcp://localhost:9999/");

Uri httpBaseAddress = new Uri("http://localhost:8888/");

ServiceHost host = new ServiceHost(typeof(TestService),tcpBaseAddress,httpBaseAddress);

Listing 2. A service host listening from multiple address.

You can also register multiple addresses in the application configuration file as shown in Listing 3.

<system.serviceModel>

  <services>

    <service name = "TheNamespace.TestService">

      <host>

        <baseAddresses>

          <add baseAddress = "net.tcp://localhost:9999/"/>

          <add baseAddress = "http://localhost:8888/"/>

        </baseAddresses>

      </host>

    </service>

  </services>

</system.serviceModel>

Listing 3. Application configuration file.

WAS hosting environment uses the Windows Activation Service (WAS) which is a system service available with Windows Vista. WAS is part of IIS7, but can be installed and configured separately. To use the WAS for hosting your WCF service, you need to supply a .svc service component's file, just as with IIS. The main difference between IIS and WAS is that the WAS is not limited to HTTP and can be used with any of the available WCF transports, ports, and queues as well as it has several features such as application pooling, recycling, idle time management, identity management, and isolation.

Conclusion

This article explained different hosting environment supported by Windows Communication Foundation technology to run your applications.

Up Next
    Ebook Download
    View all
    Learn
    View all