Exposing WCF Service Metadata — a Configuration example.

A WCF service can expose metadata either through WSDL or MEX. For MEX, you would need a mex endpoint. In the following example, I am using VS2010 and .Net4.0. Here’s how my sample “App.config and the service in action” looks like.

img2

Basically my WCF library has 2 services IService1 and IService2 with 2 serviceBehaviors behaviour1 and behaviour2 respectively. Service1 has 2 endpoints and Service2 has just 1 endpoint. Out of the 2 serviceBehaviors, behaviour1 uses mexendpoint whereas behaviour2 uses regular http based wsdl. Let’s understand the “Service1/behaviour1” in little details.
It has 2 endpoints (k1 and k2) and two base address (let’s just say baseAddress1=
http://localhost:8732/Design_Time_Addresses/TestingTCPConfig/Service1/ and baseAddress2=net.tcp://localhost:8733/Design_Time_Addresses/TestingTCPConfig/Service1/)
EndPoint k1 => (Address=baseAddress1 + binding=wsHttpBinding + contract=”TestingTCPConfig.IService1″);
EndPoint k2 => (Address=baseAddress2 + binding=netTcpBinding + contract=”TestingTCPConfig.IService1″);
Since the binding got changed, the address has to be different. Both the endpoints on this service expose metadata through MEX, hence in the behavior section under behavior1, I have set the httpGetEnabled as false. Some more stuff from msdn.

“For a service to expose metadata, the ServiceMetadataBehavior must be configured on the service. When this behavior is present, you can publish metadata by configuring an endpoint to expose the IMetadataExchange contract as an implementation of a WS-MetadataExchange (MEX) protocol. As a convenience, this contract has been given the abbreviated configuration name of “IMetadataExchange”. This sample uses the mexHttpBinding, which is a convenience standard binding that is equivalent to the wsHttpBinding with the security mode set to None. A relative address of “mex” is used in the endpoint, which when resolved against the services base address results in an endpoint address of http://localhost/servicemodelsamples/service.svc/mex.”

mexHttpsBinding is secured though.Plus MEX endpoints are configurable and can run on TCP, HTTP,HTTPS.

Next is “Service2/behaviour2”:
This behaviour exposes Service2 metadata using WSDL.Hence you would see the httpGetEnabled is set to true. This also means, the service will expose it’s metadata using Http Get.When httpGetEnabled is set to true, the metadata is available through Http Get and it needs an Http base address. WSDL is needed to work with non-.Net, old ASMX clients. This endpoint has an absolute address and netTcpBinding. In addition to that absolute address, this service still has a base address, which is needed for WSDL/HTTP. The port numbers are different though.Here’s the App.config file.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <compilation debug="true" />
  </system.web>
  
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="behaviour1" name="TestingTCPConfig.Service1">
        <endpoint name="k1" address=""
          binding="wsHttpBinding" contract="TestingTCPConfig.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint name="k2"  address=""
          binding="netTcpBinding" contract="TestingTCPConfig.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/TestingTCPConfig/Service1/" />
            <add baseAddress="net.tcp://localhost:8733/Design_Time_Addresses/TestingTCPConfig/Service1/"/>
          </baseAddresses>
        </host>
      </service>
      <service behaviorConfiguration="behaviour2" name="TestingTCPConfig.Service2">
        <endpoint address="net.tcp://localhost:8734/Design_Time_Addresses/TestingTCPConfig/Service2/"
          binding="netTcpBinding" contract="TestingTCPConfig.IService2">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/TestingTCPConfig/Service2/" />
          </baseAddresses>
        </host>
      </service>
    </services>
 <behaviors>
   <serviceBehaviors>
     <behavior name="behaviour1">
        <serviceMetadata httpGetEnabled="false" />
        <serviceDebug includeExceptionDetailInFaults="false" />
     </behavior>
     <behavior name="behaviour2">
        <serviceMetadata httpGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="false" />
      </behavior>     
      </serviceBehaviors>
 </behaviors>
    
</system.serviceModel>
</configuration>

That’s all about it. Comments/Suggestions welcome. Thanks for reading.

Advertisements
This entry was posted in WCF. Bookmark the permalink.

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