WCF Web Service Hosting Part 1 – wsHttpBinding and IIS

This, and follow up posts, is about hosting WCF web services in IIS using different bindings. This post deals with the simplest scenario – wsHttpBinding.

Create Test Service and Test Client

The first step is to create a web service using Visual Studio 2010. I created a project called “TestService”. The following App.config is generated:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <!– When deploying the service library project, the content of the config file must be added to the host’s
  app.config file. System.Configuration does not support config files for libraries. –>
  <system.serviceModel>
    <services>
      <service name="TestService.Service1" behaviorConfiguration="TestService.Service1Behavior">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8732/Design_Time_Addresses/TestService/Service1/" />
          </baseAddresses>
        </host>
        <!– Service Endpoints –>
        <!– Unless fully qualified, address is relative to base address supplied above –>
        <endpoint address ="" binding="wsHttpBinding" contract="TestService.IService1">
          <!–
              Upon deployment, the following identity element should be removed or replaced to reflect the
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity
              automatically.
          –>
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <!– Metadata Endpoints –>
        <!– The Metadata Exchange endpoint is used by the service to describe itself to clients. –>
        <!– This endpoint does not use a secure binding and should be secured or removed before deployment –>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="TestService.Service1Behavior">
          <!– To avoid disclosing metadata information,
          set the value below to false and remove the metadata endpoint above before deployment –>
          <serviceMetadata httpGetEnabled="True"/>
          <!– To receive exception details in faults for debugging purposes,
          set the value below to true.  Set to false before deployment
          to avoid disclosing exception information –>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

As you can see, VS chose wsHttpBinding. The base address looks kind of funny, but it is used in the WCF Service Host, which is started when you press F5 (Debug). You can view the service in Internet Explorer using http://localhost:8732/Design_Time_Addresses/TestService/Service1/.

After creating a test client and adding a service reference, the following client app.config is generated:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IService1" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" establishSecurityContext="true" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8732/Design_Time_Addresses/TestService/Service1/"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1"
                contract="ServiceReference1.IService1" name="WSHttpBinding_IService1">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

Add the following code to Main:

Service1Client client = new Service1Client();
Console.WriteLine(client.GetData(1));
client.Close();

Set test client as startup project and press F5.

Host in IIS

Now, to move this service to IIS, perform the following steps:

1. If you like, you can remove the base address in App.config. It is not used by IIS.

2. Publish the service to IIS. Right-click on TestService and select Publish. Create a new web application and call it WCFTest.

image

Now you can test the IIS hosted web service in IE using address http://localhost/WcfTest/TestService.Service1.svc.

If you get an error saying .svc is not recognized you probably installed IIS after WCF. You must run
“C:WindowsMicrosoft.NETFramework64v3.0Windows Communication FoundationServiceModelReg.exe” –i
For details, see IIS Hosted Service Fails.

3. Modify your test client by adding a new endpoint in app.config:

<endpoint address="http://localhost/WCFTest/TestService.Service1.svc" binding="wsHttpBinding" contract="ServiceReference1.IService1" name="IIS_wsHttp"/>

You must now supply the endpoint name to constructor to Service1Client in order for the client to know which endpoint to use:

Service1Client client = new Service1Client("IIS_wsHttp");

Advertisements

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