WCF Crib Sheet – Configuring a Service and Hosting on IIS 5/6

WCF on IIS 5/6

  • IIS manages the lifecycle of the host processes.
  • IIS 5/6 only supports HTTP/HTTPS.

Hosting a WCF service to IIS

  • Create a virtual directory.
  • Create a .SVC file, detailing the service class and assembly, and deploy to the virtual directory.
<!--ProductService.svc-->
<%@ ServiceHost Language="C#" Debug="true" Service="BikeStore.WcfServiceLibrary.ProductService" %>
<%@ Assembly Name="BikeStore.WcfServiceLibrary" %>
  • It is good practice to implement the service in a WCF Service Library, keeping it separate from the service hosting application.

Configuring a WCF service

  • Create a web.config file and deploy to the virtual directory.

Services

  • Add a <service> element for each service, with the name attribute equal to the fully-qualified service class.
  • An optional behaviorConfiguration attribute can be used to assign a set of service behaviors to a service. If this attribute is missing or equal to “” then the service will assume the default set of behaviors
<system.serviceModel>
    <services>
      <service name="BikeStore.WcfServiceLibrary.ProductService">
        ...
      </service>
    </services>
</system.serviceModel>

Endpoints

  • For each service, add one or more <endpoint> elements, detailing addresses and binding protocols available to clients.
  • Each <endpoint> element has an address attribute specifying a URI for contacting the service. This can be an absolute address or one relative to the base address of the service.
  • Each <endpoint> has a binding attribute detailing the type of transport, security, and encoding. IIS 5/6 only supports Http bindings –
    • basicHttpBinding – backwards compatibility for ASMX (SOAP 1.1) clients. Supports HTTP and HTTPS, but does not incorporate WS-* standards.
    • wsHttpBinding – SOAP 1.2. Supports HTTP and HTTPS, and incorporates WS-Addressing, WS-Security and WS-ReliableMessaging.
<system.serviceModel>
  <services>
    <service name="BikeStore.WcfServiceLibrary.ProductService">
      <endpoint address="" binding="basicHttpBinding" contract="BikeStore.WcfServiceLibrary.IProductService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
      <host>
        <baseAddresses>
          <add baseAddress="http://localhost:8733/Design_Time_Addresses/BikeStore.WcfServiceLibrary/ProductService/" />
        </baseAddresses>
      </host>
    </service>
  </services>

Behaviours

  • <behavior> elements define sets of behaviors for the endpoints and services.
  • If a <behavior> has a name attribute, then it is assigned to endpoints or services that have their behaviorConfigution attribute set equal to the name.
  • If a <behavoir> does not have a name attribute then it contributes to the default behavior of endpoints or services. Any endpoint or service that does not has a behaviorConfiguration attribute will assume these defaults.
<system.serviceModel>
  <behaviors>
    <endpointBehaviors>
      <behavior name="myBehavior">
         <callbackDebug includeExceptionDetailInFaults="true" />
       </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
      <behavior>
        <serviceMetadata httpGetEnabled="true" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <services>
    ...
  </services>
</system.serviceModel>

Bindings

  • <binding> elements enable configuration of the endpoint bindings.
  • <binding> elements are grouped by binding type, and are associated with endpoints that have their binding attribute set equal to the binding type.
  • If a <binding> element has a name attribute then it is only associated with endpoints that have, in addition to their binding attribute set equal to the binding type, also have their bindingConfiguration attribute set equal to the name.
<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="myBindingConfiguration1" closeTimeout="00:01:00" />
      <binding name="myBindingConfiguration2" closeTimeout="00:02:00" />
      <binding closeTimeout="00:03:00" />  <!—- Default binding for basicHttpBinding -->
    </basicHttpBinding>
  </bindings>
  <services>
    ...
  </services>
</system.serviceModel>

Publishing Service Metadata

  • Add a WS-MetadataExchange (MEX) endpoint to the service
  • Add a ServiceMetadataBehavior service behavior.
<system.serviceModel>
  <services>
    <service name="BikeStore.WcfServiceLibrary.ProductService">
      ...
      <endpoint address="mex"
                binding="mexHttpBinding"
                contract="IMetadataExchange" />
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior>
        <serviceMetadata httpGetEnabled="True" policyVersion="Policy15" />
      </behavior>
    </serviceBehaviors>
  </behavoirs>
</system.serviceModel>
  • Clients can retrieve the metadata using a WS-Transfer GET request or an HTTP/GET request using the ?wsdl query string.
http://localhost:57481/ProductService.svc?wsdl
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