Metadata Exchange, MEX Endpoint in WCF

 

By default your WCF service will not publish its meta data to its clients, but metadata is necessary for client to invoke the service operations. 

Publishing metadata involves significant effort because you have to convert CLR types and create WSDL. There are two options available for metadata publishing, those are you can publish metadata over HTTP-GET protocol or you can use dedicated Endpoint. 

Metadata using HTTP-GET:To publish the metadata over HTTP-GET protocol you have to enable the explicit service behaviour. That means you have to tell whether you want to publish metadata or not while you hosting the WCF Service. 

<system.serviceModel> 

                                <services> 

                                                <service name="WcfService1.Service1" behaviorConfiguration="MEXGET"> 

                                                                <!-- Service Endpoints --> 

                                                                <endpoint address="" binding="wsHttpBinding" contract="WcfService1.IService1"> 

                                                                                <!-- 

              Upon deployment, the following identity element should be removed or replaced to reflect the  

              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity  

              automatically. 

          --> 

                                                                                <identity> 

                                                                                                <dns value="localhost"/> 

                                                                                </identity> 

                                                                </endpoint> 

                                                                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 

                                                </service> 

                                </services> 

                                <behaviors> 

                                                <serviceBehaviors> 

                                                                <behavior name="MEXGET"> 

                                                                                <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 

                                                                                <serviceMetadata httpGetEnabled="true"/> 

                                                                                <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information --> 

                                                                                <serviceDebug includeExceptionDetailInFaults="false"/> 

                                                                </behavior> 

                                                </serviceBehaviors> 

                                </behaviors> 

                </system.serviceModel> 

As shown above we are enabling the explicit behaviour MEXGET, which internally enabling the service metadata attribute httpGetEnabled. 

Metadata using MEX Endpoint: MEX is nothing but Metadata Exchange Endpoint. Because WCF provides different bindings for different types of users, it introduces a metadata Exposure Endpoint. This Endpoint and its relevant classes are present as part of System.ServiceModel.ddl. 

For different category of bindings we have different Endpoint classes. Examples MEX Endpoints bindings are mexHttpBinding, mexTCPBinding..etc. Generally All Endpoints are for communication only. Client must create the proxy for particular Endpoint and will access the service through that proxy. But in the case of MEX Endpoint it is different, MEX Endpoint is not for communication, it provides the information to the client. 

MEX will be called only once i.e. for the first time only. In Web services there is no MEX Endpoint concept, through WSDL it provides the service information. 

  <system.serviceModel> 

    <services> 

      <service name="WcfService1.Service1" behaviorConfiguration="WcfService1.Service1Behavior"> 

        <!-- Service Endpoints --> 

        <endpointaddress="" binding="wsHttpBinding" contract="WcfService1.IService1"> 

          <!-- 

              Upon deployment, the following identity element should be removed or replaced to reflect the  

              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity  

              automatically. 

          --> 

          <identity> 

            <dns value="localhost"/> 

          </identity> 

        </endpoint> 

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 

      </service> 

    </services> 

    ....... 

    ....... 

  </system.serviceModel> 

As shown above, by default we have one MEX Endpoint with binding as mexHttpBinding and address as mex.