Posted by: Cirilo Meggiolaro | 04/29/2009

Tip of the day #197 – Checking if a WCF service is hosted in IIS or WAS

This tip is useful when you need to check if a WCF service is hosted in IIS or WAS (Windows Process Activation Service).

How to…

Basically when you host a WCF service in either IIS or WAS an extension from the VirtualPathExtension type (available under the System.ServiceModel.Activation namespace) is going to be available. So, the idea of this code snippet is to check if the extension is available or not:

  • The first step we need to perform is to create a ServiceHostBase object that represents the host in use;
  • The ServiceHostBase object exposes a property that is a collection of extensions. Since we have the collection available it is just a matter of lookup for an extension of type VirtualPathExtension. The code must be similar to the following:

ServiceHostBase hostBase = OperationContext.Current.Host;
VirtualPathExtension virtualPathExtension = hostBase.Extensions.Find<VirtualPathExtension>();

if (virtualPathExtension != null)
{
    /// It’s hosted.
}
else
{
    /// It’s not hosted.
}

  • If you want to check the virtual path for this service, you may access the VirtualPath property.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: