Am learning WCF, i have been using webservices (.asmx) to send requests and receive responses to other webservices. On the webservices, i was able to invoke my webmethods and test them.
public string PrnNumber(string prnNumber)
{
bool flag = false;
try
{
XmlDocument soapEnvelop = new XmlDocument();
soapEnvelop.LoadXml(@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/1999/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/1999/XMLSchema""><SOAP-ENV:Body><HelloWorld xmlns=""http://tempuri.org/"" SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""><int1 xsi:type=""xsd:integer"">12</int1><int2 xsi:type=""xsd:integer"">32</int2></HelloWorld></SOAP-ENV:Body></SOAP-ENV:Envelope>");
flag = SendSOAP(soapEnvelop);
}
catch (Exception ex)
{
Util.LogMessage(ex.Message + ex.StackTrace, "Error", "err");
}
return "Success";
}
public bool SendSOAP(soapEnvelop)
{
bool flag = false;
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http:***url");
req.Headers.Add("Accept-Encoding", "gzip,deflate");
req.Headers.Add("SOAPAction", "urn:lookupPRN");
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
req.Proxy = null;
try
{
using (Stream stm = req.GetRequestStream())
{
using (StreamWriter stmw = new StreamWriter(stm))
{
stmw.Write(xml);
}
}
WebResponse response = req.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
string responseString = reader.ReadToEnd();
Util.LogMessage(responseString, "Response", "res");
}
catch (Exception ex)
{
flag = false;
Util.LogMessage(ex.Message + ex.StackTrace, "Error", "err");
}
return flag;
}
finally
{
}
}
So i initially did this in .asmx and i would log both the requests and responses, How can i achive this using WCF? exactly the same logic, constructing the SOAP XML as plain XML in my code and invoke the partners's url for the response.
Suggest you spend some time reviewing a few WCF
"Getting Started" articles.
The following provide a good, basic overview:
https://learn.microsoft.com/en-us/dotnet/framework/wcf/getting-started-tutorial
http://www.codeproject.com/Articles/33995/Getting-Started-with-WCF
http://www.codeproject.com/Articles/42643/Creating-and-Consuming-Your-First-WCF-Service