What are the different ways to expose WCF Metadata? | WCF Interview Question

, ,
By default, WCF doesn’t expose metadata. We can expose it by choosing one of the following ways:
1.    In configuration file, by enabling metadata exchange as follows:
  <system.serviceModel>
    <services>
           <service name=”MyService.Service1″
                                              behaviorConfiguration=”MyService.Service1″>
                      <endpoint address=””
                                            binding=”wsHttpBinding”
                                            contract=”MyService.IService1″>
                          <identity>
                              <dns value=”localhost”/>
                          </identity>
                      </endpoint>
                     <endpoint address=”mex” binding=”mexHttpBinding”
                                            contract=”IMetadataExchange”/>
            </service>
    </services>
    <behaviors>
            <serviceBehaviors>
                        <behavior name=”MyService.Service1″>
                                    <serviceMetadata httpGetEnabled=”true”/>
                                    <serviceDebug includeExceptionDetailInFaults=”false”/>
                       </behavior>
             </serviceBehaviors>
     </behaviors>
</system.serviceModel>
2.  ServiceHost can expose a metadata exchange endpoint to access metadata at runtime.
   using (ServiceHost host = new ServiceHost(typeof(MyService)))
   {
              ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
              behavior.HttpGetEnabled = true;
              host.Description.Behaviors.Add(behavior);
              host.Open();
              Console.WriteLine(“My Service here……….”);              Console.ReadLine();
              host.Close();
   }