简体   繁体   English

xamarin PCL Soap Web服务

[英]xamarin PCL Soap web service

I am new to Xamarin and C# and SOAP. 我是Xamarin和C#和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. 我有一个在Mac上的Visual Studio 2017上创建的Xamarin PCL项目,我需要使用SOAP Web服务-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. 我有与我提到的第二个链接相同的问题,当我向PCL添加Web引用时,该框架显示为灰色并设置为WCF,并且无法将其更改为.Net 2.0。 If I Add Web Reference to Android and IOS project then I can change framework. 如果我将Web参考添加到Android和IOS项目,则可以更改框架。 I am not targeting windows app now, only IOS and android. 我现在不针对Windows应用,仅针对IOS和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? 我是通过尝试将Web引用添加到PCL来正确执行此操作,还是应该将其添加到2个平台项目中?

One way of doing this is using the dependency injection as explained below. 一种方法是使用依赖项注入,如下所述。

Define an interface in the PCL project 在PCL项目中定义接口

public interface ISoapServiceHelper
{

    string PerformSyncSoapServiceRequest(string requestXML);
}

Implement the interface in the native iOS & Android project as shown below. 如下所示,在本机iOS和Android项目中实现界面。 The requestXML is the SOAP service request requestXML是SOAP服务请求

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. 我正在使用Unity IOC进行依赖项注入,并且工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM