Metadata Exchange Endpoint

16. October 2006 21:43

This has probably been covered elsewhere, but I am going to post it anyway.

The Metadata Exchange Endpoint (MEX) is a special endpoint in WCF that exposes metadata used to describe a service.  In previous releases of WCF, the MEX was implicitly added to all services.  However, due to security reasons, it is no longer exposed by default as of RC1.  Without the MEX, you will not be able to use svcutil.exe to automatically generate a proxy class.  Fortunately, it is a simply process to enable the MEX for your service.

The MEX can be exposed programatically or via configuration (just like any other endpoint).

To programatically expose the MEX:

using System.ServiceModel;
using System.ServiceModel.Description;

ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
host.Description.Behaviors.Add(behavior);

host.AddServiceEndpoint(
    typeof(IMetadataExchange),
    MetadataExchangeBindings.CreateMexHttpBinding(),
    "http://localhost/MyService/mex/");

Please note that ServiceMetadataBehavior, IMetadataExchange, and MetadataExchangeBindings reside within the System.ServiceModel.Description namespace.  This threw me off when I was first searching for them.

To expose the MEX via configuration:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>   
      <service name="MyService" 
               behaviorConfiguration="DefaultBehavior">
        <endpoint address=http://localhost/MyService
                  binding="basicHttpBinding" 
                  contract="IMyService" />
        <endpoint address="http://localhost/MyService/mex"
                  binding="mexHttpBinding" 
                  contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="DefaultBehavior">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

Depending upon your binding, the corresponding MEX binding will vary. There is support for HTTP, TCP, and Named Pipes.  If you are adding the MEX programatically, there are corresponding methods in the MetadataExchangeBindings object.  If you adding the MEX via configuration, the equivalent binding is fairly obvious to figure out.  Essentially, use mex?Binding and replace the ? with whatever you are using.

It should be noted that you are not required to enable HttpGet.  SvcUtil will still be able to access the MEX without it.  However, it is useful if you want to view the WSDL from a browser by going to the address of the service.  You cannot do so without enabling the HttpGet.

Comments are closed

About Me

I'm a passionate software developer and advocate of the Microsoft .NET platform.  In my opinion, software development is a craft that necessitates a conscious effort to continually improve your skills rather than falling into the trap of complacency.  I was also awarded as a Microsoft MVP in Connected Systems in 2008, 2009, and 2010.


Can’t code withoutThe best C# & VB.NET refactoring plugin for Visual Studio
Follow jeff_barnes on Twitter

View Jeff Barnes's profile on LinkedIn

 

Shared Items

Disclaimer

Anything you read or see on this site is solely based on my own thoughts.  The material on this site does not necessarily reflect the views of my employer or anyone else.  In other words, I don't speak for anyone other than myself.  So, don't assume I am the official spokesperson for anyone.