I am new to Xamarin and C# and SOAP.
I have found similar questions such as this and especially this.
I have a Xamarin PCL project I created on Visual Studio 2017 for Mac, and I need to consume a SOAP web service - WSDL.
I have the same issue as the second link I mentioned, when I add a web reference to the PCL the framework is greyed out and set to WCF and I cant change it to .Net 2.0.
If I Add Web Reference to Android and IOS project then I can change framework. I am not targeting windows app now, only IOS and android.
Am I doing this correctly by trying to add the web reference to the PCL, or should be be added to the 2 platform projects?
0
votes
1 Answers
0
votes
One way of doing this is using the dependency injection as explained below.
Define an interface in the PCL project
public interface ISoapServiceHelper
{
string PerformSyncSoapServiceRequest(string requestXML);
}
Implement the interface in the native iOS & Android project as shown below. The requestXML is the SOAP service request
public string PerformSyncSoapServiceRequest(string requestXML)
{
var ServiceResult = string.Empty;
try
{
HttpWebRequest request = CreateSOAPWebRequest();
XmlDocument SOAPReqBody = createSOAPReqBody(requestXML);
using (Stream stream = request.GetRequestStream())
{
SOAPReqBody.Save(stream);
}
using (WebResponse Serviceres = request.GetResponse())
{
using (StreamReader rd = new StreamReader(Serviceres.GetResponseStream()))
{
ServiceResult = rd.ReadToEnd();
}
}
}
catch (Exception e)
{
throw;
}
return ServiceResult;
}
public HttpWebRequest CreateSOAPWebRequest()
{
HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(@"your_URL");
Req.Headers.Add(@"SOAP:Action");
Req.ContentType = "text/xml;charset=\"utf-8\"";
Req.Accept = "text/xml";
Req.Method = "POST";
Req.Timeout = 20000;
Req.ReadWriteTimeout = 20000;
return Req;
}
XmlDocument createSOAPReqBody(string requestXML)
{
XmlDocument SOAPReqBody = new XmlDocument();
SOAPReqBody.LoadXml(requestXML);
return SOAPReqBody;
}
I am using Unity IOC for dependency injection and it is working fine.