One of the changes in a recent version was that MEX endpoints no longer get added to your service host by default. Instead, you either need to supply the ServiceMetadataBehavior or supply the ServiceMetadataBehavior and manually add your favorite type of MEX endpoint. Let's talk about the case where you want to specifically add your own MEX endpoint.

Adding your own MEX endpoint is a two step process. First, you need to add a ServiceMetadataBehavior to the service host.

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

This step is required because the service host validates the contract type you specify for endpoints. Normally, the contract of the endpoint must be compatible with the contract declared as the type parameter for your service host. However, you probably don't have a service that uses the IMetadataExchange contract for your business logic. Inside the service host, if the contract for the endpoint does not match the contract of the host, the service host will then look at the collection of registered behaviors. If there is a ServiceMetadataBehavior present, then the service host checks the contract for the endpoint against the list of acceptable metadata contracts held by ServiceMetadataBehavior. Right now, that list of acceptable contracts contains only IMetadataExchange. In any case, this allows you to add a MEX endpoint to the service while still strongly validating the contract types of endpoints.

The second step is to add the MEX endpoint. You need to choose a compatible MEX binding using MetadataExchangeBindings and a MEX address. You can choose a non-standard MEX binding as well but you then have the problem of communicating the configuration for that binding to your clients. There's no special magic in the MEX address as compared to the normal address of an endpoint.

host.AddServiceEndpoint(
typeof(IMetadataExchange),
MetadataExchangeBindings.CreateMexNamedPipeBinding(),
"net.pipe://localhost/service/mex/" );

MetadataExchangeBindings supports HTTP, HTTPS, TCP, and named pipes for MEX transports. The HTTP(S) MEX bindings use the WSHttp binding with the SecurityMode set appropriately. The TCP and named pipe MEX bindings are custom bindings containing just the transport. All of the bindings are in the http://schemas.microsoft.com/ws/2005/02/mex/bindings namespace.

You should also see An Unanticipated Addendum for Certain MEX Scenarios if you're using port sharing.

Next time: Why is the RequestContext Null?