Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


WCF Essentials—A Developer's Primer : Page 2

WCF is an SDK for building service-oriented applications on Windows, letting you use classic CLR programming constructs, such as classes and interfaces, to deploy and consume services. The programming model is declarative and largely attribute-driven.




Application Security Testing: An Integral Part of DevOps

The WCF service class cannot exist in a void. Every WCF service must be hosted in a Windows process called the host process. A single host process can host multiple services and the same service type can be hosted in multiple host processes. WCF makes no demand on whether or not the host process is also the client process.

Obviously, having a separate process advocates fault and security isolation. It is also 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
The main advantage of hosting a service in IIS is that the host process is launched automatically upon client requests, and you rely on IIS to manage the lifecycle of the host process. The main disadvantage of IIS hosting is that you can only use HTTP for transport on both IIS5 and IIS6; when using IIS5, you can only rely on port 80. 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 .svc file. The .svc file functions like an .asmx, and is used to identify the service code-behind file and class.

<%@ ServiceHost Language = "C#" Debug = "true" CodeBehind = "~/App_Code/MyService.cs" Service = "MyService" %>

You can even inject the service code in-line in the .svc file, but that is unadvisable, as is the case with ASMX. Once you have the .svc file in place, you can navigate to it using a browser. If all is well, you will get a confirmation page.

Visual Studio 2005 can generate a new IIS-hosted service for you. From the File menu, select New Website and then select WinFX Service from the New Web Site dialog box. This causes Visual Studio 2005 to create a new Web site, service code, and matching .svc file. In addition, the Web site config file must list the types you want to expose as services. You need to use fully-qualified type names, including assembly name, if type comes from an unreferenced assembly.

<system.serviceModel> <services> <service name="MyNamespace.MyService"> ... </service> </services> </system.serviceModel>

Self Hosting
Self hosting is the name for the technique used when the developer is responsible for providing and managing the lifecycle of the host process. Self-hosting is used both in the case of wanting a process (or computer) boundary between the client and the service and when using the service in-proc—that is, in the same process as the client. The process you need to provide can be 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. This is not an issue for NT Services or in-proc.

Similar to IIS hosting, the hosting application config file must list the types of the services you wish to host and expose to the world. In addition, the host process must explicitly register the service types at run time and open the host for client calls. This is typically done in the Main() method using the helper class ServiceHost defined as:

public interface ICommunicationObject : IDisposable { void Open(); void Close(); //More members } public abstract class CommunicationObject : ICommunicationObject {...} public class ServiceHostBase : CommunicationObject,... {...} public class ServiceHost : ServiceHostBase,... { public ServiceHost(Type serviceType, params Uri[]baseAddresses); //More members }

Provide the constructor of ServiceHost with the service type and optionally with default base addresses. The set of base addresses can be an empty set and later, you can configure different base addresses. Having a set of base addresses enables the service to accept calls on multiple addresses and protocols. 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 a matching number of ServiceHost instances. By calling the ServiceHost.Open() method on the host, you allow calls in, and by calling the ServiceHost.Close() method, you gracefully exit the host instance and finish sending data to current clients and yet refuse future client calls even if the host process is still running. Closing is typically done on host process shutdown. For example, to host this service in a Windows Forms application:

[ServiceContract] interface IMyContract {...} class MyService : IMyContract {...}

You can write:

public static void Main() { Uri baseAddress = new Uri("http://localhost:8000/"); ServiceHost serviceHost; serviceHost = new ServiceHost(typeof(MyService), baseAddress); serviceHost.Open(); //Can do blocking calls: Application.Run(new MyForm()); serviceHost.Close(); }

Note that you can do blocking calls after the call to ServiceHost.Open() because the host accepts calls on worker threads. The call to ServiceHost.Open() loads the WCF runtime and enables accepting client calls. The host can register multiple base addresses as long as they differ at least by the transport:

Uri tcpBaseAddress = new Uri("net.tcp://localhost:8001/"); Uri httpBaseAddress = new Uri("http://localhost:8002/"); ServiceHost serviceHost = new ServiceHost(typeof(MyService), tcpBaseAddress,httpBaseAddress);

Visual Studio 2005 allows you to add a WCF service to any application project by selecting WCF service from the Add New Item dialog box. The service added this way is, of course, in-proc toward the host process, but can be accessed by external clients as well.

WAS Hosting
The Windows Activation Service (WAS) 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 file just as with IIS. The WAS offers many advantages over IIS and self-hosting including idle time management, identity management, application pooling, and isolation, and is the host process of choice when available. Still, the self-hosted process offers singular advantages for in-proc hosting, dealing with unknown customer environment, relying on TCP or IPC when only IIS is available, utilizing multiple ports on HTTP when only IIS5 is available, and programmatic access to some advanced hosting features.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date