WAS Hosting - (WCF)
Windows Process Activation Service (WAS) hosting is a method of hosting Windows Communication Foundation (WCF) services in IIS. WAS hosting is ideal for environments that require high levels of scalability and availability.
Syntax
To host a WCF service using WAS hosting, you will need to configure IIS to use the WAS listener adapter. Here is a sample configuration file for WAS hosting:
<configuration>
<system.serviceModel>
<services>
<service behaviorConfiguration="MyServiceBehavior" name="MyService">
<endpoint address="" binding="wsHttpBinding" contract="IMyService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/MyService" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<directoryBrowse enabled="true" />
<handlers>
<add name="svc-Integrated" path="*.svc" verb="*" modules="ProtocolSupportModule" type="System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</handlers>
<defaultDocument>
<files>
<add value="MyService.svc" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
Example
Here's an example of how to configure IIS to use WAS hosting for a WCF service:
- Install IIS and the WAS feature on your server or development machine.
- Open IIS Manager and create a new website or application.
- Copy the configuration file above into an empty file named
web.config
in the root directory of your website or application. - Create a new WCF service file named
MyService.svc
in the root directory of your website or application, with the following contents:
<%@ ServiceHost Language="C#" Debug="true" Service="MyService" CodeBehind="MyService.svc.cs" %>
- Create a new code file named
MyService.svc.cs
in the same directory, with the following contents:
using System.ServiceModel;
[ServiceContract]
public interface IMyService
{
[OperationContract]
string GetMessage();
}
public class MyService : IMyService
{
public string GetMessage()
{
return "Hello, World!";
}
}
- Save and close all files, and then browse to your website or application to verify that the service is working.
Output
When you browse to your website or application, you should see a page that displays the default document specified in the web.config
file, and should be able to access the WCF service by appending /MyService.svc
to the URL. For example:
http://localhost/MyWebsite/MyService.svc
Explanation
In the sample configuration file and example code, we define a WCF service with a single operation that returns a simple string. We use the DataContract
and DataMember
attributes to specify the data contract for the operation, and use ServiceBehavior
, Endpoint
, and Host
elements to configure the service behavior, endpoints, and host address. In the example, we also create a separate code file for the service implementation, and use the ServiceHost
directive to specify the type of service to host. In IIS, we use the web.config
file to specify the service metadata, behavior, and endpoints, and use the WAS listener adapter to process incoming messages.
Use
WAS hosting is well-suited for hosting WCF services in IIS, especially in environments that require scalability and availability. Using WAS hosting, you can take advantage of the built-in IIS features, such as authentication, authorization, and security, to protect your services and control access.
Important Points
- WAS hosting is a method of hosting WCF services in IIS that provides scalability and availability.
- You can use the
web.config
file to specify the service metadata, behavior, and endpoints for your WCF service. - You can use the WAS listener adapter to process incoming messages for your WCF service.
Summary
In this page, we discussed how to host WCF services using WAS hosting in IIS. We covered the syntax, example, output, explanation, use, important points, and summary of using WAS hosting for WCF services. WAS hosting is a powerful way to scale and host WCF services in IIS, and provides many built-in features for protecting your services and controlling access.