Exposing the metadata using HTTP-GET has a disadvantage, such that there is no guarantee that other platforms you interact will support it. There is other way of exposing the using special endpoint is called as Metadata Exchange Endpoint. You can have as many metadata exchange endpoints as you want.
Address
It is basically Uri to identify the metadata. You can specify as address in the endpoint but append with "mex" keyword. For example "http://localhost:9090/MyCalulatorService/mex"Binding
There are four types of bindings supported for metadata exchange. They are mexHttpBinding, mexHttpsBinding, mexNamedPipesBinding, mexTcpBinding.Contract
IMetadataExchange is the contract used for MEX endpoint. WCF service host automatically provides the implementation for this IMetadataExcahnge while hosting the service.You can create the Metadata Exchange Endpoint either Administrative (configuration file) or programmatically.
Administrative (Configuration file):
In the configuration file of the hosting application, you can add metadata exchange endpoint as shown below.<system.serviceModel> <services> <service name="MyService"> <endpoint address="http://localhost/IISHostedService/MyService.svc" binding="wsHttpBinding" contract="IMyService"> <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> </system.serviceModel>
Programming Model:
In the following code I have mention about creating the Metadata Exchange Endpoint through coding. Steps to create the metadata endpoint are- Create the ServiceMetadataBehavior object and add to Service host description.
- Create the metadata binding object using MetadataExchangeBinding
- 3. Add the endpoint to the service host with address, binding and contract.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); host.Description.Behaviors.Add(smb);
Binding mexBinding = MetadataExchangeBindings.CreateMexHttpBinding ();
host.AddServiceEndpoint(typeof(IMetadataExchange), mexBinding, "mex");
Complete code for hosting the service with metadata exchange endpoint is shown below.
//Create a URI to serve as the base address Uri httpUrl = new Uri("http://localhost:8090/MyService/SimpleCalculator"); //Create ServiceHost ServiceHost host = new ServiceHost(typeof(MyCalculatorService.SimpleCalculator), httpUrl); //Add a service endpoint host.AddServiceEndpoint (typeof(MyCalculatorService.ISimpleCalculator), new WSHttpBinding(), ""); //Enable metadata exchange ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); host.Description.Behaviors.Add(smb); Binding mexBinding = MetadataExchangeBindings.CreateMexHttpBinding (); //Adding metadata exchange endpoint host.AddServiceEndpoint(typeof(IMetadataExchange), mexBinding, "mex"); //Start the Service host.Open(); Console.WriteLine("Service is host at " + DateTime.Now.ToString()); Console.WriteLine("Host is running... Press key to stop"); Console.ReadLine();
No comments:
Post a Comment