Microsoft Dot Net Master

Microsoft Dot Net Master
Microsoft Dot Net Master

Monday, September 24, 2012

HTTP_GET Enabled Metadata

We will use ServiceBehaviour to publish the metadata using HTTP-GET. This can be configures either administratively or Programmatically. Http and Https can expose by appending "?wsdl" to the end of the service address. For example service address is http://localhost:9090/MyCalulatorService , HTTP-Get metadata address is given by http://localhost:9090/MyCalulatorService?wsdl.

Administrative (Configuration file):

In the below mention configuration information, you can find the behavior section in the ServiceBehavior. You can expose the metadata using ServiceMetadata node with httpGetEnable='True'.
<system.serviceModel>
   <services>
 <service behaviorConfiguration="ServiceBehavior" name="MyService">
    <endpoint address="http://localhost/IISHostedService/MyService.svc"
     binding="wsHttpBinding" contract="IMyService">
  <identity>
  <dns value="localhost"/>
  </identity>
     </endpoint>
 </service>
  </services>
  <behaviors>
    <serviceBehaviors>
 <behavior name="ServiceBehavior">
   <!-Setting httpGetEnabled you can publish the metadata -->
  <serviceMetadata httpGetEnabled="true"/>
  </behavior>
     </serviceBehaviors>
   </behaviors>
</system.serviceModel>

Progarmming Model:

Using ServiceMetadataBehavior you can enable the metadata exchange. In the following code, I have created the ServiceMetadataBehavior object and assigned HttpGetEnabled property to true. Then you have to add the behavior to host description as shown. This set of code will publish the metadata using HTTP-GET.
 //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();

            //Enable metadata exchange using HTTP-GET
            smb.HttpGetEnabled = true;

            host.Description.Behaviors.Add(smb);
            //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