Export (0) Print
Expand All

Hosting Windows Communication Foundation Services

Robert Green

MCW Technologies

Download

Articles in this series

Introduction

This tutorial is the first in a two-part series on hosting Windows Communication Foundation (WCF) services. In this tutorial, you will explore the role of the service host and review your options for hosting WCF services. You will then host a WCF service using Internet Information Services (IIS) and Windows Process Activation Service (WAS). In the next tutorial, you will see how to host a WCF service in a managed application such as a console application or a Windows service.

Hosting a WCF Service

In order for clients to call a WCF service, you must host the service in a runtime environment. The role of the host application is to start and stop the service, listen for requests from clients, direct those requests to the service and send responses back to the clients.

To host a service, the host application uses the ServiceHost class in the System.ServiceModel namespace. The application uses this class to instantiate the service, configure endpoints, apply security and start listeners that handle requests from clients.

In the previous tutorial (Introduction to Windows Communication Foundation) you saw that an endpoint consists of an address, a binding and a contract. The address identifies where clients can find the service. The binding specifies how the client and service communicate. The contract specifies what operations the service supports.

At runtime, the host application will create a ServiceHost object for each hosted service. If you are hosting more than one service on a host, you will have more than one ServiceHost object.

If clients can communicate with a service over more than one protocol, say HTTP and TCP, you do not need multiple ServiceHost objects. You can define multiple endpoints for the service.

You can host a WCF service in any Windows process that supports managed code. This includes:

  • Internet Information Services (IIS) version 5.1 running on Windows XP with Service Pack 2, version 6.0 running on Windows Server 2003 or version 7.0 running on Windows Vista, Windows 7 or Windows Server 2008.
  • Windows Process Activation Service (WAS) running on Windows Vista, Windows 7 or Windows Server 2008.
  • A managed application such as a console application, a Windows service, a Windows Forms application or a Windows Presentation Foundation application. This option is known as self-hosting.

The following are some differences between these three options:

  • IIS and WAS provide automatic activation. As soon as a message arrives, the service is activated. If you self-host a service, you must create an instance of the ServiceHost class and configure it in code. You will see how to do this in the next tutorial.
  • IIS and WAS will monitor services. They will shut down a service’s process if it is idle or unresponsive. The next time a client calls the service, the process is recycled and restarted. IIS and WAS also provide health monitoring tools.
  • If you host a service using IIS, clients can communicate with it only using HTTP. If you host the service using WAS, clients can communicate using HTTP, TCP, named pipes and Microsoft Message Queueing (MSMQ).

Which hosting option should you use? The answer depends on your operating system, transport protocol needs and scenario. The following are best practice recommendations:

  • If you are running Windows Server 2008, you should deploy WCF services using WAS.
  • If you are running Windows Server 2003 and it is okay to support only HTTP, you should deploy WCF services using IIS.
  • If you are running Windows Server 2003 and you need to support other protocols such as TCP, you should deploy WCF services using a Windows service.
  • Self-hosting a WCF service using a console application is easy and appropriate for demos and testing, but really has no place in production.
  • Self-hosting a WCF service in a Windows Forms or Windows Presentation Foundation application is appropriate for certain scenarios. For example, you may build a chat application that uses a WCF service to receive and broadcast messages.

Review the Sample Application

In Visual Studio 2008, select File | Open | Project/Solution to display the Open Project dialog box. Navigate to the folder when you downloaded this tutorial’s sample project. Select HostingServicesDemo.sln and click OK to open the project. The sample application includes three projects. The ProductServiceLibrary project represents a WCF service that clients can call to retrieve information on a product. The IProductService file contains the following service contract:

[ServiceContract]

public interface IProductService

{

  [OperationContract]

  Product GetProduct(int productId);

}

<ServiceContract()> _

Public Interface IProductService

  <OperationContract()> _

  Function GetProduct(ByVal productId As Integer) As Product

End Interface

The service has a method to retrieve product information. The Product class has properties for ProductId, ProductName and UnitPrice.

The InventoryServiceLibrary project represents a WCF service that clients can call to manage inventory. The IInventoryService file contains the following service contract:

[ServiceContract]

public interface IInventoryService

{

  [OperationContract]

  Inventory GetInventory(int productId);

  [OperationContract]

  bool UpdateInventory(Inventory inventory);

}

<ServiceContract()> _

Public Interface IInventoryService

  <OperationContract()> _

  Function GetInventory(ByVal productId As Integer) As Inventory

  <OperationContract()> _

  Function UpdateInventory(ByVal _inventory As Inventory) _

    As Boolean

End Interface

The service has methods to retrieve and update inventory information. The Inventory class has properties for ProductId, UnitsInStock and UnitsOnOrder.

The WindowsClient project contains the Manage Inventory form (see Figure 1). When the user clicks the Get product button, the form will call the GetProduct method of the ProductService service to retrieve the product name and price. The form will also call the GetInventory method of the InventoryService service to retrieve the units in stock and on order. When the user clicks the Update product button, the form will call the UpdateInventory method of the InventoryService service to update the units in stock and on order.

Figure 1. Use this form to call the WCF service.

Host WCF Services Using Internet Information Services

In this section, you will create a Web site to host the two WCF services. You will then write code to call the services from the form. If you completed the previous tutorial in this series, this will be familiar to you. What is new here is that you will use IIS rather than the ASP.NET Development Server and you will host two services rather than only one.

In the Solution Explorer, right-click the HostingServicesDemo solution and select Add | New Web Site to display the Add New Web Site dialog box. Select WCF Service in the Templates list. Select HTTP from the Location drop-down list. Select Visual Basic or Visual C# from the Language drop-down list. In the Location text box, enter http://localhost/IISHostingDemo. Click OK.

In the Solution Explorer, expand the App_Code folder under the new web site project you just created. Delete the IService and Service files. In the Solution Explorer, right-click the IISHostingDemo project, and select Add Reference from the context menu. In the Projects tab of the Add Reference dialog box, select InventoryServiceLibrary and ProductServiceLibrary. (To select more than one item in the Add Reference dialog select the first item and while holding down the ctrl key select the other.) Click OK to add the reference to the two services.

Rename the Service1.svc file to InventoryService.svc. Open the file by double clicking on the file name in the Solution Explorer.  Since you are referencing an existing service, change the contents of this file to the following:

<%@ ServiceHost Service=

"InventoryServiceLibrary.InventoryService" %>

One of the benefits of hosting a WCF service using IIS is that it automatically creates an instance of the ServiceHost class when a client calls the service. You simply need to specify what service the IIS host should activate by setting the Service property of the ServiceHost directive. Save the changes you made to the InventoryService.svc file.

In the Solution Explorer, make a copy of the InventoryService.svc file by selecting File | Save As and name the copy ProductService.svc. Change the contents of this file to the following:

<%@ ServiceHost Service=

"ProductServiceLibrary.ProductService" %>

To host multiple WCF services on a Web site, you create a .svc file and add a service element to Web.config for each service. Save the changes you made to ProductService.svc.

In the Solution Explorer, double-click Web.config. In the system.web section, make the following change in bold to enable debugging of the service at runtime:

<compilation debug="true" strict="false" explicit="true">

<compilation debug="true">

Scroll down to the system.serviceModel section. Make the following changes in bold to specify both services:

<services>

  <service name="InventoryServiceLibrary.InventoryService"

           behaviorConfiguration="ServiceBehavior">

    <!-- Service Endpoints -->

    <endpoint address="" binding="wsHttpBinding"

              contract="InventoryServiceLibrary.IInventoryService">

      <identity>

        <dns value="localhost"/>

      </identity>

    </endpoint>

    <endpoint address="mex" binding="mexHttpBinding"

              contract="IMetadataExchange"/>

  </service>

  <service name="ProductServiceLibrary.ProductService"

           behaviorConfiguration="ServiceBehavior">

    <!-- Service Endpoints -->

    <endpoint address="" binding="wsHttpBinding"

              contract="ProductServiceLibrary.IProductService">

      <identity>

        <dns value="localhost"/>

      </identity>

    </endpoint>

    <endpoint address="mex" binding="mexHttpBinding"

              contract="IMetadataExchange"/>

  </service>

</services>

Save your changes and select Build | Build Web Site. In the Solution Explorer, right-click on InventoryService.svc and select View in Browser. You should see the service test page in your browser. Do the same for ProductService.svc.

Call the Services from the Client Application

The next step in this tutorial is to call the WCF services from the client application. In the Solution Explorer window, right-click on the WindowsClient project and select Add Service Reference to display the Add Service Reference dialog box. In the previous tutorial, you clicked Discover to search for services that the current solution contains. You could do that here as well, but often you will call services that are maintained elsewhere.

Enter http://localhost/IISHostingDemo/InventoryService.svc in the Address text box and click Go. In the Services pane, select InventoryService. Expand the InventoryService node. Expand the InventoryService node and select IInventoryService. You should see the available operations/methods in the Operations pane (see Figure 2).

 

Figure 2. Select the WCF service that you want to use.

Enter InventoryService in the Namespace text box and click OK. Add a second service reference for the other service. Enter http://localhost/IISHostingDemo/ProductService.svc in the Address text box and click Go. Enter ProductService in the Namespace text box and click OK. You should have two service references in the WindowsClient project (see Figure 3).

 

Figure 3. You have references to two WCF services in the WindowsClient project.

In the Solution Explorer, double-click the WindowsClient project’s app.config file. You should see the following XML defining two endpoints, one for each service:

<client>

    <endpoint

        address=

          "http://<computer>/IISHostingDemo/InventoryService.svc"

        binding="wsHttpBinding"

        bindingConfiguration="WSHttpBinding_IInventoryService"

        contract="InventoryService.IInventoryService"

        name="WSHttpBinding_IInventoryService">

        <identity>

            <dns value="localhost" />

        </identity>

    </endpoint>

    <endpoint

        address=

          "http://<computer>/IISHostingDemo/ProductService.svc"

        binding="wsHttpBinding"

        bindingConfiguration="WSHttpBinding_IProductService"

        contract="ProductService.IProductService"

        name="WSHttpBinding_IProductService">

        <identity>

            <dns value="localhost" />

        </identity>

    </endpoint>

</client>

In the Solution Explorer, double-click Form1, opening the form in the form designer. Select View | Code. At the top of the file, add the following statements:

using System.ServiceModel;

using WindowsClient.InventoryService;

using WindowsClient.ProductService;

Imports System.ServiceModel

Imports WindowsClient.InventoryService

Imports WindowsClient.ProductService

In the form’s class, add the following variable declarations:

private Inventory inventory = null;

private Product product = null;

private InventoryServiceClient inventoryProxy = null;

private ProductServiceClient productProxy = null;

private decimal inStockValue = 0M;

private decimal onOrderValue = 0M;

Private _inventory As Inventory = Nothing

Private _product As Product = Nothing

Private inventoryProxy As InventoryServiceClient = Nothing

Private productProxy As ProductServiceClient = Nothing

Private inStockValue As Decimal = 0D

Private onOrderValue As Decimal = 0D

In the Form1_Load event handler, add the following code to create a new instance of the proxy class for each service:

inventoryProxy = new InventoryServiceClient(

  "WSHttpBinding_IInventoryService");

productProxy = new ProductServiceClient(

  "WSHttpBinding_IProductService");

inventoryProxy = New InventoryServiceClient( _

  "WSHttpBinding_IInventoryService")

productProxy = New ProductServiceClient ( _

  "WSHttpBinding_IProductService")

In the getProductButton_Click event handler, add the following code to retrieve the product and inventory information for a product:

product = new Product();

product = productProxy.GetProduct(

  Convert.ToInt32(productIdTextBox.Text));

productNameLabel.Text = product.ProductName;

unitPricelabel.Text = product.UnitPrice.ToString("C");

inventory = new Inventory();

inventory = inventoryProxy.GetInventory(

  Convert.ToInt32(productIdTextBox.Text));

inStockTextBox.Text = inventory.UnitsInStock.ToString();

inStockValue = product.UnitPrice *

  Convert.ToDecimal(inventory.UnitsInStock);

inStockValueLabel.Text = inStockValue.ToString("C");

onOrderTextBox.Text = inventory.UnitsOnOrder.ToString();

onOrderValue = product.UnitPrice *

  Convert.ToDecimal(inventory.UnitsOnOrder);

onOrderValueLabel.Text = onOrderValue.ToString("C");

_product = New Product()

_product = productProxy.GetProduct( _

  Convert.ToInt32(productIdTextBox.Text))

productNameLabel.Text = _product.ProductName

unitPricelabel.Text = _product.UnitPrice.ToString("C")

_inventory = New Inventory()

_inventory = inventoryProxy.GetInventory( _

  Convert.ToInt32(productIdTextBox.Text))

inStockTextBox.Text = _inventory.UnitsInStock.ToString()

inStockValue = _product.UnitPrice * _

  Convert.ToDecimal(_inventory.UnitsInStock)

inStockValueLabel.Text = inStockValue.ToString("C")

onOrderTextBox.Text = _inventory.UnitsOnOrder.ToString()

onOrderValue = _product.UnitPrice * _

  Convert.ToDecimal(_inventory.UnitsOnOrder)

onOrderValueLabel.Text = onOrderValue.ToString("C")

In the updateProductButton_Click event handler, add the following code to update inventory information for a product:

inventory.UnitsInStock = Convert.ToInt16(inStockTextBox.Text);

inventory.UnitsOnOrder = Convert.ToInt16(onOrderTextBox.Text);

if (inventoryProxy.UpdateInventory(inventory))

{

  MessageBox.Show("Your changes were saved");

  inStockValue = product.UnitPrice *

    Convert.ToDecimal(inventory.UnitsInStock);

  inStockValueLabel.Text = inStockValue.ToString("C");

  onOrderValue = product.UnitPrice *

    Convert.ToDecimal(inventory.UnitsOnOrder);

  onOrderValueLabel.Text = onOrderValue.ToString("C");

}

else

{

  MessageBox.Show("Your changes were not saved");

}

_inventory.UnitsInStock = Convert.ToInt16(inStockTextBox.Text)

_inventory.UnitsOnOrder = Convert.ToInt16(onOrderTextBox.Text)

If inventoryProxy.UpdateInventory(_inventory) Then

  MessageBox.Show("Your changes were saved")

  inStockValue = _product.UnitPrice * _

    Convert.ToDecimal(_inventory.UnitsInStock)

  inStockValueLabel.Text = inStockValue.ToString("C")

  onOrderValue = _product.UnitPrice * _

    Convert.ToDecimal(_inventory.UnitsOnOrder)

  onOrderValueLabel.Text = onOrderValue.ToString("C")

Else

  MessageBox.Show("Your changes were not saved")

End If

Save your changes and build the solution. Press F5 to run the application. You may have noticed the WcfSvcHost popup and two WCF Service Host icons in the Notification Area. When you run the application in debug mode, Visual Studio automatically starts an instance of the WCF Service host for each service in the solution. To avoid this, you can start the application by pressing Ctrl+F5.

You can also instruct Visual Studio to not start the WCF Service Host. Close the form. In the Solution Explorer, select the InventoryServiceLibrary project. Select Project | InventoryServiceLibrary Properties to display the Project Designer. In the WCF Options tab, uncheck Start WCF Service Host when debugging another project in the same solution. Note that this tab will not appear if you are not running Visuao Studio 2008 Service Pack 1. Close the Project Designer and save your changes. Do the same for the ProductServiceLibrary project.

Press F5 to run the application. In the Manage Inventory form, enter 1 in the Product text box and click Get product. The form calls the GetProduct method of the ProductService WCF service and retrieves the product’s information. The form then calls the GetInventory method of the InventoryService WCF service and retrieves the inventory information (see Figure 4).

 

Figure 4. You called two WCF services to retrieve product and inventory information.

Enter 20 in the On Order text box and click Update product. Dismiss the dialog box informing you your changes were saved. Close the form. Press F5 to run the application again. In the Manage Inventory form, enter 1 in the Product text box and click Get product. You should see that 20 units are on order for this product. Close the form.

Host WCF Services Using Windows Process Activation Service

Windows Process Activation Service (WAS) is a feature of IIS 7.0. It provides service hosting, process activation and monitoring for protocols other than HTTP. Specifically, it provides these for TCP, named pipes, and MSMQ. WAS uses a listener architecture and provides listener adapters for each of the supported protocols. WAS is available in Windows Vista, Windows 7 and Windows Server 2008.

When you install IIS 7 you also install WAS. However, to use WAS you must enable it and configure it to support additional protocols. To enable WAS, use the Programs and Features applet in the Control Panel and click the Turn Windows features on or off link. In the Windows Features dialog box, enable Windows Process Activation Service (see Figure 5) and non-HTTP activation of WCF services (see Figure 6).

 

Figure 5. Enable Windows Process Activation Service.

 

Figure 6. Enable non-HTTP activation of WCF services.

Now that you have enabled WAS, you must configure it to support additional protocols. You can do this in the IIS Manager (inetmgr.exe) by selecting the Default Web Site in the Connections pane on the left and selecting the Bindings link in the Actions pane on the right. This displays the Site Bindings dialog box.

In this tutorial, you will host a WCF service on WAS using TCP. If you already see a net.tcp binding in the Site Bindings dialog box, click Close. Otherwise, click Add to display the Add Site Binding dialog box. Select net.tcp from the Type drop-down list and enter 9100:* in the Binding information text box (see Figure 7). Click OK to add the binding. You should see net.tcp in the Site Bindings dialog box (see Figure 8). If there is already a net.tcp binding set up on your machine with a different port number, that is okay. Click Close to dismiss that dialog box.

 

Figure 7. Add a site binding for TCP.

 

Figure 8. You have configured WAS to support TCP.

Configuration for IIS 7, and therefore for WAS, is stored in the applicationHost.config file in the Windows\System32\inetsrv\config folder. If you open this file, you will see the following inside the sites element:

<bindings>

  <binding protocol="http" bindingInformation="*:80:" />

  <binding protocol="https" bindingInformation="*:443:" />

  <binding protocol="net.tcp" bindingInformation="9100:*" />

</bindings>

You will now create a Web site to host the two WCF services using WAS. In the Solution Explorer, right-click the HostingServicesDemo solution and select Add | New Web Site to display the Add New Web Site dialog box. Select WCF Service in the Templates list. Select HTTP from the Location drop-down list. Select Visual Basic or Visual C# from the Language drop-down list. In the Location text box, enter http://localhost/WASHostingDemo. Click OK.

Before moving on, configure the WASHostingDemo Web site to support TCP. You can do this in the IIS Manager (inetmgr.exe) by expanding the Default Web Site node in the Connections pane on the left and selecting WASHostingDemo. Then select the Advanced Settings link in the Actions pane on the right. This displays the Advanced Settings dialog box. Set the Enabled Protocols setting to http,net.tcp (see Figure 9). Click OK.

 

Figure 9. You have configured the WASHostingDemo Web site to support TCP.

In the applicationHost.config file, you will see the following inside the sites element:

<application path="/WASHostingDemo"

  applicationPool="DefaultAppPool" enabledProtocols="http,net.tcp">

  <virtualDirectory path="/"

    physicalPath="C:\inetpub\wwwroot\WASHostingDemo" />

</application>

Return to Visual Studio. In the Solution Explorer, expand the App_Code folder in the WASHostingDemo project. Delete the IService and Service files. In the Solution Explorer, right-click the WASHostingDemo project, and select Add Reference from the context menu. In the Projects tab of the Add Reference dialog box, select InventoryServiceLibrary and ProductServiceLibrary. Click OK to add the reference to the two services.

Rename the Service1.svc file to InventoryService.svc. Double click the file to edit it . Since you are referencing an existing service, change the contents of this file to the following:

<%@ ServiceHost Service="InventoryServiceLibrary.InventoryService" %>

Save the InventoryService.svc file. In the Solution Explorer, make a copy of the InventoryService.svc file (again, by using File.SaveAs) and name the copy to ProductService.svc. Double click the file to edit it. Change the contents of this file to the following:

<%@ ServiceHost Service="ProductServiceLibrary.ProductService" %>

Save the ProductService.svc file. In the Solution Explorer, double-click Web.config. In the system.web section, make the following change in bold to enable debugging of the service at runtime:

<compilation debug="true" strict="false" explicit="true">

<compilation debug="true">

Scroll down to the system.serviceModel section. Replace the services section with the following:

<services>

  <service name="InventoryServiceLibrary.InventoryService"

           behaviorConfiguration="ServiceBehavior">

    <!-- Service Endpoints -->

    <endpoint

      address="" binding="wsHttpBinding"

      contract="InventoryServiceLibrary.IInventoryService"/>

    <endpoint

      address="" binding="netTcpBinding"

      contract="InventoryServiceLibrary.IInventoryService"/>

    <endpoint address="mex" binding="mexHttpBinding"

              contract="IMetadataExchange"/>

  </service>

  <service name="ProductServiceLibrary.ProductService"

          behaviorConfiguration="ServiceBehavior">

    <!-- Service Endpoints -->

    <endpoint address="" binding="wsHttpBinding"

              contract="ProductServiceLibrary.IProductService"/>

    <endpoint address="" binding="netTcpBinding"

              contract="ProductServiceLibrary.IProductService"/>

    <endpoint address="mex" binding="mexHttpBinding"

              contract="IMetadataExchange"/>

  </service>

</services>

You have configured WAS to host both services using both HTTP and TCP. This means clients can call either service using either protocol. This is a common scenario. Internal clients might communicate with the service using TCP while external clients might communicate using HTTP.

Save your changes and select Build | Build Web Site. In the Solution Explorer, right-click on InventoryService.svc and select View in Browser. You should see the service test page in your browser. Do the same for ProductService.svc.

Call the Services from the Client Application

The final step in this tutorial is to modify the client application to call the WAS hosted WCF services. In the Solution Explorer window, right-click on WindowsClient and select Add Service Reference to display the Add Service Reference dialog box. Enter http://localhost/WASHostingDemo/InventoryService.svc in the Address text box and click Go. Enter WASInventoryService in the Namespace text box and click OK.

Add a second service reference for the other WAS hosted service. Enter http://localhost/WASHostingDemo/ProductService.svc in the Address text box and click Go. Enter WASProductService in the Namespace text box and click OK. You should now have four service references in the WindowsClient project.

In the Solution Explorer, double-click the WindowsClient project’s app.config file. You should see the following six endpoints:

  • WSHttpBinding_IInventoryService. This endpoint represents the InventoryService service hosted in IIS.
  • WSHttpBinding_IProductService. This endpoint represents the ProductService service hosted in IIS.
  • WSHttpBinding_IInventoryService1. This endpoint represents the InventoryService service hosted in WAS using HTTP.
  • NetTcpBinding_IInventoryService. This endpoint represents the InventoryService service hosted in WAS using TCP.
  • WSHttpBinding_IProductService1. This endpoint represents the ProductService service hosted in WAS using HTTP.
  • NetTcpBinding_IProductService. This endpoint represents the ProductService service hosted in WAS using TCP.

In the Solution Explorer, right-click Form1 and select View Code. At the top of the file, make the following changes in bold: Note you should modify the existing using/imports statements rather than adding new ones.

using WindowsClient.WASInventoryService;

using WindowsClient.WASProductService;

Imports WindowsClient.WASInventoryService

Imports WindowsClient.WASProductService

In the Form1_Load event handler, make the following changes in bold to create proxies based on the services hosted in WAS using HTTP:

inventoryProxy = new InventoryServiceClient(

  "WSHttpBinding_IInventoryService1");

productProxy = new ProductServiceClient(

  "WSHttpBinding_IProductService1");

inventoryProxy = New InventoryServiceClient( _

  "WSHttpBinding_IInventoryService1")

productProxy = New ProductServiceClient ( _

  "WSHttpBinding_IProductService1")

Save your changes and build the solution. Press F5 to run the application. In the Manage Inventory form, enter 1 in the Product text box and click Get product. You should see the product and inventory information you saw previously.

Enter 20 in the On Order text box and click Update product. Dismiss the dialog box informing you your changes were saved. Close the form.

Return to the form’s code file. In the Form1_Load event handler, make the following changes in bold to create proxies based on the services hosted in WAS using TCP: Note there is no '1' at the end of the endpoint name.

inventoryProxy = new InventoryServiceClient(

  "NetTcpBinding_IInventoryService");

productProxy = new ProductServiceClient(

  "NetTcpBinding_IProductService");

inventoryProxy = New InventoryServiceClient( _

  "NetTcpBinding_IInventoryService")

productProxy = New ProductServiceClient ( _

  "NetTcpBinding_IProductService")

Press F5 to run the application again. In the Manage Inventory form, enter 1 in the Product text box and click Get product. You should see that 20 units are on order for this product. Close the form.

Return to the form’s code file. In the Form1_Load event handler, make the following changes in bold to create proxies based on the services hosted in WAS using both HTTP and TCP:

inventoryProxy = new InventoryServiceClient(

  "WSHttpBinding_IInventoryService1");

productProxy = new ProductServiceClient(

  "NetTcpBinding_IProductService");

inventoryProxy = New InventoryServiceClient( _

  "WSHttpBinding_IInventoryService1")

productProxy = New ProductServiceClient ( _

  "NetTcpBinding_IProductService")

Press F5 to run the application again. In the Manage Inventory form, enter 1 in the Product text box and click Get product. You should see that 20 units are on order for this product. Close the form.

Conclusion

In this tutorial, you explored hosting WCF services using both Internet Information Services (IIS) and Windows Process Activation Service (WAS). Both of these provide automatic activation and monitoring of the service.

Both IIS and WAS can host a service and communicate with clients using HTTP. You saw that a major benefit to using WAS is the ability to communicate with clients using other protocols. You also saw that changing a client to communicate with TCP as opposed to HTTP requires nothing more than using a different endpoint.

The ability to communicate using a variety of protocols and the ease with which you can change from one to another is a major advantage of using WCF services over Web services.

About the Author

Robert Green is a developer, writer, and trainer. He is a senior consultant with MCW Technologies. Robert is a Visual Studio Tools for the Office system MVP and is a co-author of AppDev courseware for Microsoft Visual Basic, Microsoft Visual C#, LINQ and Microsoft Windows Workflow Foundation. Before joining MCW, Robert worked at Microsoft as a Product Manager and also a Program Manager.

Show:
© 2014 Microsoft