Search content within the blog

Tuesday, August 3, 2010

Adding a mex endpint and httgGetEnabled in wcf service

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.

No comments:

Post a Comment