Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Friday 24 June 2016

How take multiple response like Json, Xml from a common/single method using endpoints in wcf

 Firstly create a wcf service. To create wcf service follow given steps:
 Open Visual studio goto FileèNewèProcject as shown.



This will create two .cs file name as Service1 and IService1 and .svc file in your by default solution.


Now decorate  your Operation Contract as :
[OperationContract]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "GetData")]
string GetData();

Remove all other  attribute like ‘RequestFormat = WebMessageFormat.Json’, ‘ResponseFormat = WebMessageFormat.Json’ if there.
Set above line above your Service1 class
  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

 

Now you need to set your web.config with following configuration. Just replace your   <system.serviceModel> tag code with code given below. You can change “Name” attribute in “Service” tag if you need. You can also change “Contract” attribute in “endpoint” tag according to your service name.


<system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true"></binding>
      </webHttpBinding>
      <basicHttpBinding>
        <binding name="AvailabilitySoap">
          <security mode="Transport" />
        </binding>
        <binding name="AvailabilitySoap1" />
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="WcfService1.Service1" behaviorConfiguration="ListRequestBehaviour">
        <endpoint address="" binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="web"></endpoint>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp defaultOutgoingResponseFormat="Xml" automaticFormatSelectionEnabled="true" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="ListRequestBehaviour">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

  </system.serviceModel>
Now your service is ready  to give multiple response from a single method. You just need to deploy your service to ge multiple respond.
If you want to check your service with Fidler then run your service on localhost and copy following url in compose tab of fiddler in case you have implement above code.
And also copy given code in Request header to get “Json” response

User-Agent: Fiddler
content-type: application/Json
Content-Length: 0
Host: localhost:5239

To get “Xml” response:
User-Agent: Fiddler
content-type: application/Xml
Content-Length: 0
Host: localhost:5239

You can also test your service using JavaScript code given below:
To get the result in Json format

<script type="text/javascript">
        $(document).ready(function () {

            $.ajax({
                type: "GET",
                url: "http://localhost:5239/Service1.svc/GetData",
               contentType: "application/json; charset=utf-8",
                dataType: "json",
                ProcessData: true,
                success: function (data) {
                    alert(data);
                },
                error: function (err) {
                    alert(err.responseText);
                }
            });
        });   
    
    </script>

To get the result in xml format

<script type="text/javascript">
        $(document).ready(function () {

            $.ajax({
                type: "GET",
                url: "http://localhost:5239/Service1.svc/GetData",
                contentType: "application/json; charset=utf-8",
                dataType: "xml",
                ProcessData: true,
                success: function (data) {
                    alert(data);
                },
                error: function (err) {
                    alert(err.responseText);
                }
            });
        });   
    
    </script>


No comments:

Post a Comment