没有 WCF 的 C# SOAP 服务

标签 c# asp.net wcf web-services soap

我试图在 C# 中创建 SOAP 网络服务和网络客户端而不使用 WCF 工具。我的目标是创建一个与很可能不使用 WCF 的 SOAP 服务通信的客户端。 我做了一个概念验证客户端,但目前我不太确定如何编写原始 SOAP XML 以发送到 WCF 服务来测试我的客户端。另外,我可以在客户端即使 SOAP 服务不使用 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 服务。

关于没有 WCF 的 C# SOAP 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18535968/

相关文章:

ASP.NET MVC 2 教程

c# - 清理 Silverlight 输入

c# - wcf 客户端 ip 作为 ipv6

c# - 通过 WCF 服务使用连接字符串建立与 Azure SQL 数据库的连接时出现问题

c# - ASP.Net 本地化(卫星程序集与带有自定义 ResourceProvider 的数据库)

c# - Android 版本支持推送通知

c# - 绑定(bind)在 WPF MVVM C#​​ 中不起作用

c# - 加速 LINQ 对象查询

asp.net - 检测 ASP.NET 网站的出站连接排队

c# - PrecompileBeforePublish 使用 Msbuild