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.
{
[ServiceContract(Namespace = "BasicHttpBindingXmlHttpRequestWcf")]
public interface IService
{
[OperationContract]
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">
</endpoint>
</service>
</services>
</system.serviceModel>
JavaScript File:-
//calling wcf service
function callservice(){
//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
alert(xmlHttp.responseText);
}
};
//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='http://schemas.xmlsoap.org/soap/envelope/'>"
+ "<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.
}
WcfService:-
namespace BasicHttpBindingXmlHttpRequestWcf{
[ServiceContract(Namespace = "BasicHttpBindingXmlHttpRequestWcf")]
public interface IService
{
[OperationContract]
string GetData(string input);
}
}
{
public class Service : IService
{
public string GetData(string input)
{
string output = "You Enter "+ input;
return output;
}
}
}
<system.serviceModel>
<services><service name=" BasicHttpBindingXmlHttpRequestWcf.Service">
<endpoint address="" binding="basicHttpBinding" contract=" BasicHttpBindingXmlHttpRequestWcf.IService">
</endpoint>
</service>
</services>
</system.serviceModel>
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
alert(xmlHttp.responseText);
}
};
xmlHttp.onerror = function (e) {
//alert the user
alert('error occurred…');
};
var url = “http://localhost:4040/Service.svc”;
var body="<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>"
+ "<s:Body>"
+ "<GetData xmlns='BasicHttpBindingXmlHttpRequestWcf'>"
+ "<input>123</input>"
+ "</GetData>"
+ "</s:Body>"
+ "</s:Envelope>";
//here we are passing the method type,url
and operation type(true for async,false for sync)
xmlHttp.open("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.)
xmlHttp.setRequestHeader("soapAction","BasicHttpBindingXmlHttpRequestWcf/IService/GetData");
//calling service function
xmlHttp.send(body);}
Let me know, if you have any feedback. Mail me for source code. Enjoy reading my articles…
sekhartechblog@gmail.com
No comments:
Post a Comment