How To: Hosting a WCF Service in IIS

How To: Hosting a WCF Service in IIS

In this article we looked at the basics of Windows Communication Foundation (WCF) and how it can be used to host a service within a Windows Form. In reality, many applications today already take advantage of IIS to host HTTP based services like ASMX Web Services. As a host agnostic programming model WCF can also take advantage of IIS. Technically, when using WCF to host services within IIS they are integrated into ASP.NET. This allows WCF to take advantage of the inherent features of ASP.NET. This includes process recycling, idle shutdown, process health monitoring and message based activation. Probably one of the most important advantages of using IIS is that it doesn’t require any additional hosting code as part of the WCF service. The WCF service is automatically activated by IIS using traditional Web hosting techniques. The major difference is that a .svc file is used instead of an .asmx page.

In this article we will build a WCF service that is hosted in IIS.  Once we have built the service we will then build a Windows Form application that connects and retrieves the data from this service.

Note: This sample takes advantage of the Visual Studio 2005 extensions for .NET Framework 3.0 (WCF & WPF), November 2006 CTP that are available here. While you can build this sample without the use of the designer as we did in the previous article.  It definitely makes it easier.

Building the Server

1.       Create a new Web Site and select WCF Service

Note: This sample is using the File System as the location to build and run this sample.

2.       The WCF Service template creates all the necessary files and plumbing needed to expose the service. Once created the project includes the base files needed for this service.

 

3.       The business code is actually written in the service.vb file. The sample file provided by default implements two <OperationContract()>. These expose two operations that the consumer application can use to interact with this service.

Note: If you were building a production application this is the location where you would insert your own business logic code.

4.       At this point we have created the basic service. If you press F5 and run this service you will see the following screen.

 

Note: By default Metadata is disabled for this service. In order to retrieve the service description for the client we will build in the next part we need to turn this on.

 

5.       Turning on MetaData can be done by modifying the Web.Config to include the following

<?xml version="1.0"?>

 

<configuration xmlns="https://schemas.microsoft.com/.NetConfiguration/v2.0">

  <system.serviceModel>

    <services>

      <!-- Before deployment, you should remove the returnFaults behavior configuration to avoid disclosing information in exception messages -->

      <service name="MyService" behaviorConfiguration="MyServiceTypeBehaviors">

        <endpoint contract="IMyService"  binding="mexHttpBinding" address="mex"/>

      </service>

    </services>

    <behaviors>

      <serviceBehaviors>

                                  <behavior name="MyServiceTypeBehaviors" >

                                                  <serviceMetadata httpGetEnabled="true" />

                                  </behavior>

        <behavior name="returnFaults" >

          <serviceDebug includeExceptionDetailInFaults="true" />

        </behavior>

      </serviceBehaviors>

    </behaviors>

  </system.serviceModel>

 

  <system.web>

    <compilation debug="true"/>

  </system.web>

 

</configuration>

 

6.       Save the modified Web.Config and then run the service again. You can see that the MetaData is now available.

 

 

Building the Client

 

Now that we have a WCF service available that has exposed endpoints. We can then build a Windows Form component that can returns the data that is exposed through this service.

 

1.       Add a new Windows Form project to this solution

 

 

2.       Once the projected is created we need a reference to the service that we created earlier. In order to do make sure that the service is started. Once the service is started right click on the Solution Explorer and select Add New Service from the Solution Explorer window.

 

3.       Enter the URI of the service. This can be found in either the service code or the address bar of Internet explorer. Enter this information in the Add Service Reference dialog box.

 

 

4.       When you select Ok this will automatically run the svcutil command line utility. This will contact the running service and return the service information and configuration files needed to connect the Windows Form application.

 

 

5.       Once this is completed the following references are added to the client application

 

6.       Once this is completed we know have the necessary configuration information needed to connect to the WCF service and return the data. Enter the following code into the form load event of the form.

 

Public Class Form1

 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim lvarWCF As New localhost.MyServiceClient

 

        MsgBox(lvarWCF.MyOperation1("hello"))

    End Sub

End Class

 

When this form is run the application starts and communicates with the service and returns the data

In this article we looked at how a WCF service that uses IIS can be built. Also, how this type of service can be connected to from a Windows Form. Of course this was a simple example of some of the features that are available. In later posts we will dive deeper into other areas of WCF. The code example shown here is available for download from here.