Friday, 6 January 2012

Wcf basicHttpBinding And XMLHttpRequest

We can access the wcf basicHttpBinding service in the XMLHttpRequest,but we must now the SOAP format of the service methods.To know the SOAP format of wcf service mathods we can use wcftestclient which is exist in wcf sdk.

namespace BasicHttpBindingXmlHttpRequestWcf
 [ServiceContract(Namespace = "BasicHttpBindingXmlHttpRequestWcf")]
 public interface IService
          string GetData(string input);

namespace BasicHttpBindingXmlHttpRequestWcf
   public class Service : IService
     public string GetData(string input)
         string output = "You Enter "+ input;
        return output;

     <service name=" BasicHttpBindingXmlHttpRequestWcf.Service">
       <endpoint address="" binding="basicHttpBinding" contract=" BasicHttpBindingXmlHttpRequestWcf.IService">

JavaScript File:-

//calling wcf service
function callservice(){

//create XMLHttpRequest object
var xmlHttp = new XMLHttpRequest();

//event handler for getting result
xmlHttp.onreadystatechange = function () {
    //check the status of the request(if the status is 4 then all the data is received)
      if (xmlHttp.readyState == 4)
      //here you  get the data in SOAP(xml) format.we have to iterate through xml to get the result.      
        //alert the result  

//error event handler
xmlHttp.onerror = function (e) {
    //alert the user
    alert('error occurred…');

//url of the service
var url = “http://localhost:4040/Service.svc;

//here we are constructing the request in SOAP format
var body="<s:Envelope xmlns:s=''>"
                 + "<s:Body>"
                + "<GetData xmlns='BasicHttpBindingXmlHttpRequestWcf'>"
                + "<input>123</input>"
                + "</GetData>"
       + "</s:Body>"
       + "</s:Envelope>";

//here GetData is service method name.xmlns is the namespace name in ServiceContract .   The parameter name(input) must match with parameter name in OperationContract.

//here we are passing the method type,url and operation type(true for async,false for sync)"POST", url, true);

//adding  header for sending data type(xml)
xmlHttp.setRequestHeader("Content-type", "text/xml; charset=utf-8");

//adding  header for SOAPAction(Avialable In WSDL with  Same Name.)

//calling service function

Let me know, if you have any feedback. Mail me for source code. Enjoy reading my articles…

No comments:

Post a Comment