簡體   English   中英

沒有WCF的C#SOAP服務

[英]C# SOAP Service without WCF

我正在嘗試使用CCF不使用WCF工具的情況下創建SOAP Web服務和Web客戶端。 我的目標是創建一個與很可能沒有使用WCF的SOAP服務通信的客戶端。 我做了一個概念證明客戶端,但目前我還不確定如何編寫原始SOAP XML以發送到WCF服務來測試我的客戶端。 此外,即使SOAP服務不使用WCF,我可以在客戶端使用WCF嗎? 謝謝!

SOAP客戶端:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Net;
using System.Text;
using System.IO;

namespace HttpSoapPoc.Models
{
    public class SoapClient
    {
        protected virtual WebRequest CreateRequest(ISoapMessage soapMessage)
        {
            var wr = WebRequest.Create(soapMessage.Uri);
            wr.ContentType = "text/xml;charset=utf-8";
            wr.ContentLength = soapMessage.ContentXml.Length;

            wr.Headers.Add("SOAPAction", soapMessage.SoapAction);
            wr.Credentials = soapMessage.Credentials;
            wr.Method = "POST";
            wr.GetRequestStream().Write(Encoding.UTF8.GetBytes(soapMessage.ContentXml), 0, soapMessage.ContentXml.Length);

            return wr;
        }

        public interface ISoapMessage
        {
            string Uri { get; }
            string ContentXml { get; }
            string SoapAction { get; }
            ICredentials Credentials { get; }
        }

        public static string CallWebService()
        {
            var _url = "https://passport.iso.com/PPWebListener/services/SOAPListenerV2";
            var _action = "https://passport.iso.com/PPWebListener/services/SOAPListenerV2?op=HelloWorld";

            XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
            HttpWebRequest webRequest = CreateWebRequest(_url, _action);
            InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

            // begin async call to web request.
            IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

            // suspend this thread until call is complete. You might want to
            // do something usefull here like update your UI.
            asyncResult.AsyncWaitHandle.WaitOne();

            // get the response from the completed web request.
            string soapResult;
            using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
            {
                using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                {
                    soapResult = rd.ReadToEnd();
                }
                return soapResult;
            }

        }

        private static HttpWebRequest CreateWebRequest(string url, string action)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Headers.Add("SOAPAction", action);
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            return webRequest;
        }

        private static XmlDocument CreateSoapEnvelope()
        {
            XmlDocument soapEnvelop = new XmlDocument();
            String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
            xml += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
            xml += "<soap:Body>";
            xml += "<CelsiusToFahrenheit xmlns=\"http://tempuri.org/\">";
            xml += "<Celsius>20</Celsius>";
            xml += "</CelsiusToFahrenheit>";
            xml += "</soap:Body>";
            xml += "</soap:Envelope>";
            soapEnvelop.LoadXml(xml);
            return soapEnvelop;
        }

        private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
        {
            using (Stream stream = webRequest.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }
        }
    }
}

SOAP服務:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Activation;

namespace SoapTutorial
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class SoapService : ISoapService
    {
        public Employee[] GetEmployees()
        {
            return new Employee[]
            {
                new Employee() { EmpNo=101, EmpName="Mahesh", DeptName="CTD" },
                new Employee() { EmpNo=102, EmpName="Akash", DeptName="HRD" }
            };
        }
    }
}

您可以將WCF與basicHttpBinding一起使用。 這將創建一個非WCF客戶端可互操作的SOAP服務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM