Software & Finance





Visual Studio.NET - WCF Hosting in IIS





 

Here is the step by step instructions for hosting WCF service in IIS.

 

WCF Hosting in IIS - Step By Step Instructions

 

  1. Create a "WCF Service Application" under VS2010
  2. Do a refactor to change the service name and implement desired methods.
  3. The files needed to host WCF service on IIS are given below:
    1. ApplicationRoot\TestService.svc
    2. ApplicationRoot\web.config
    3. ApplicationRoot\bin\TestService.dll
  4. The configuration of web.config file is discussed in the next section.
  5. Create a virtual directory under IIS and make it as an application root. otherwise you would not be able to use the web config defined under this virtual directory.
  6. Copy the 3 files given on the step 3 into the virtual directory.
  7. Then you can access the .SVC wcf service using any IE.
  8. You can click the following link to invoke the WCF service which is given in my sample.

         http://www.softwareandfinance.com/wcftestservice/TestService.svc

 

web.config file configuration

 

  1. Under services, you can have a collection of services.
  2. For each service, you can have the service name and multiple endpoints.
  3. For end point configuration: you need Address, Binding and Contract. You can remember with ABC for address, binding and contract
  4. The contract IMetadataExchange with address mex and binding type mexHttpBinding contains the meta data information and is used to expose the list of other services.
  5. If you want to disable meta data, then add the service behaviorconfiguration with <serviceMetadata httpGetEnabled="false"/>
  6. multipleSiteBindingsEnabled should be set to true under   serviceHostingEnvironment to enable multiple bindings in each end points.
  7. The ServiceActivations settings will have the relative address of the .SVC file from the virtual directory application root and the service name.
  8. The following is the sample web.config file used to host WCF service in http://www.softwareandfinance.com/wcftestservice/TestService.svc

 

<?xml version="1.0"?>

<configuration>

 

  <system.web>

    <compilation debug="true" targetFramework="4.0" />

  </system.web>

 

  <system.serviceModel>

 

    <services>

      <service name="Wcf_Test_Service.TestService" behaviorConfiguration="MyServiceTypeBehaviors">

        <endpoint address=""

                 binding="basicHttpBinding"

                 contract="Wcf_Test_Service.ITestService" >

        </endpoint>       

        <endpoint address="mex"

                  binding="mexHttpBinding"

                  contract="IMetadataExchange" />

      </service>

    </services>

   

    <behaviors>

      <serviceBehaviors>

        <behavior name="MyServiceTypeBehaviors">

            <serviceMetadata httpGetEnabled="true"/>

                  <serviceDebug includeExceptionDetailInFaults="true"/>

        </behavior>

      </serviceBehaviors>

    </behaviors>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true">

      <serviceActivations>

        <add relativeAddress="TestService.svc" service="Wcf_Test_Service.TestService"/>

      </serviceActivations>

    </serviceHostingEnvironment>

  

  </system.serviceModel>

 

 <system.webServer>

    <modules runAllManagedModulesForAllRequests="true"/>

  </system.webServer>

 

 

    <system.web>

      <customErrors mode="Off"/>

    </system.web>

 

</configuration>