Home > WCF > WCF # 4 – Metadata Exchange Points (Creating/Exposing Metadata Exchange Point using code and configuration files)

WCF # 4 – Metadata Exchange Points (Creating/Exposing Metadata Exchange Point using code and configuration files)

 

Hi Friends,

We have learnt how to write the web service using WCF,In this article we will see how   Metadata in WCF refers to the information that describes precisely how to communicate with a service.

* Clients can request metadata from a running service to learn about their endpoints and the message formats that they require.

* At design time, clients send a request message defined by the WS-MetadataExchange standard and receive WSDL in return. The WSDL can be used by the client to define a proxy class and configuration file that will later be used at runtime to communicate with the service.

The following figure shows the communication between the client and Metadata Endpoint.

 

image

 

* By default WCF Services do not expose Metadata exchange endpoint.Without knowing the information like address,Binding and contract it is very difficult to communicate with the service.WCF makes it easy to expose a MEX endpoint so that clients can communicate properly with services. The MEX endpoint can be exposed in code or in configuration.

* Following is the Code which will help us in exposing the METADATA EXCHANGE point using code.

NOTE:

To Make the following code run

1)IIS must be there on your machine

2)you have to take the CONSOLE APPLICATION

    and then need to add System.ServiceModel reference to it.

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.ServiceModel;
using
System.ServiceModel.Description;
namespace
WCFusingCode
{
   
class Program
    {
        [ServiceContract]
       
public interface IStockService
        {
            [OperationContract]
           
double GetPrice(string ticker);
       
}

        public class StockService : IStockService
        {
           
public double GetPrice(string ticker)
            {
return 9.99; }
        }

        static void Main(string[] args)
        {
            ServiceHost servicehost
= new ServiceHost(typeof(StockService),
                              
new Uri("http://localhost:8000/WCFusingCode"));
           
servicehost.AddServiceEndpoint(typeof(IStockService),
                                          
new BasicHttpBinding(), "");
           
servicehost.Open();
           
           
//ADDING METADATA EXCHANGE POINT
           
servicehost.AddServiceEndpoint(typeof(IMetadataExchange),
            MetadataExchangeBindings.CreateMexHttpBinding(),
"mex");

            Console.WriteLine("Press Enere to to terminate");
           
Console.ReadLine();
           
servicehost.Close();

        }
    }
}

 

* Exposing METADATA EXCHANGE POINT using Configuration Files

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="MyFirstWCFService.StockService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000//MyFirstWCFService"/>
          </baseAddresses>
        </host>
        <endpoint address=""  binding="basicHttpBinding"
                  contract="MyFirstWCFService.IStockService"></endpoint>

        <endpoint address="mex" binding="mexHttpBinding
                  contract="IMetadataExchange" >
        </endpoint>
      </service>
    </services>
  </system.serviceModel>
</configuration>

Hope this will help you in understanding the METADATAEXCHANGE ENDPOINTS.

Thanks.

Advertisements
Categories: WCF
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: