This tip is useful when you need to check if a WCF service is hosted in IIS or WAS (Windows Process Activation Service).
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.
/// It’s not hosted.
- If you want to check the virtual path for this service, you may access the VirtualPath property.