使用SOAP调用WCF服务


问题内容

我正在尝试在不创建WCF客户端的情况下测试WCF服务。我有类似的代码/问题在这里介绍。

我希望对SOAP有效负载进行完全控制,因此我希望能够发出自己的Web请求/响应,并将自己的XML替换为任何方法中的参数。我还希望返回SOAP
XML完全照原样,而不创建响应对象,故障异常对象等。

作为参考,我想确切地执行SoapUI在单击执行按钮并返回响应时的​​操作。我只是假设SoapUI不会创建WCF客户端,构建请求并调用该方法,而是向WCF服务发出完整的SOAP调用并显示结果。

在回答评论中的问题时,我不想创建WCF客户端的原因是,我希望不受服务的任何所有更改的影响,而不必重建引用,修改自己的代码,创建单独的代码每个新服务/方法等的代码,因为此过程在每次构建后都会自动启动,无需任何交互。

因此,我有成千上万的测试XML参数,可以将它们传递给数百种方法,而不必关心它们是什么。我们已经在ASMX
Web服务上完成了多年,并且一种方法(与上面的链接非常相​​似)处理了所有Web服务/方法/测试参数。

切换到WCF时,我会收到内部服务器错误,尤其是在测试无效的XML节点时:缺少必需的节点,名称重复的create方法中的错误等(任何错误情况)。我认为使用WCF以相同的方式执行此操作很简单,这是有道理的。

我想确切地返回SoapUI返回的内容,我只需要知道它的工作方式即可。


问题答案:

@John
Saunders的评论是正确的。无论您使用ASMX做什么,都应该能够使用WCF。实际上,只要您执行适当的SOAP请求,Web服务使用哪种框架/技术都没有关系。

WCF只是一个有助于构建面向服务的应用程序的框架。像任何其他此类框架一样,它使您能够专注于将要提供的实际服务,并照顾了将该服务公开为SOAP
Web服务所需的所有管道功能。

至于SoapUI,它是一个Java工具,可让您测试Web服务。当您向它提供WSDL时,它会动态创建请求样本,然后使用Http
Client
(如果我没记错的话)将其发送到Web服务。

如果您有WCF Web服务,那么什么都不会发生。即使使用这样的基本客户端,仍然可以进行SOAP通信:

public class Program
{
    public static void Main(string[] args)
    {
        // OK, this is not a WCF web service, but that should not matter :D
        string endpoint = "http://www.html2xml.nl/Services/Calculator/Version1/Calculator.asmx";

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
        request.ContentType = "text/xml"; // or application/soap+xml for SOAP 1.2
        request.Method = "POST";
        request.KeepAlive = false;

        //In case you have a proxy to resolve the server name also add these lines
        var proxyServer = new WebProxy("XX.XX.XX.XX", 1234);
        proxyServer.Credentials = CredentialCache.DefaultCredentials; // or username + password
        request.Proxy = proxyServer;

        // you can read these from files
        string payload = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:tem=""http://tempuri.org/"">
                               <soapenv:Header/>
                               <soapenv:Body>
                                  <tem:Add>
                                     <tem:a>1</tem:a>
                                     <tem:b>2</tem:b>
                                  </tem:Add>
                               </soapenv:Body>
                            </soapenv:Envelope>";

        byte[] byteArray = Encoding.UTF8.GetBytes(payload);
        request.ContentLength = byteArray.Length;

        Stream requestStream = request.GetRequestStream();
        requestStream.Write(byteArray, 0, byteArray.Length);
        requestStream.Close();

        HttpWebResponse response = null;
        try
        {
             response = (HttpWebResponse)request.GetResponse();
        }
        catch (WebException ex)
        {
             response = (HttpWebResponse)ex.Response;
        }

        Console.WriteLine(string.Format("HTTP/{0} {1} {2}\n", response.ProtocolVersion, (int)response.StatusCode, response.StatusDescription));

        // you can write this to files
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        Console.WriteLine(reader.ReadToEnd());

        // cleanp
        reader.Close();
        requestStream.Close();
        responseStream.Close();
        response.Close();
    }
}

您将获得一个SOAP响应,在这种情况下,它是:

HTTP/1.1 200 OK

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <AddResponse xmlns="http://tempuri.org/">
            <AddResult>3</AddResult>
        </AddResponse>
    </soap:Body>
</soap:Envelope>

不管是生成它的是ASMX,还是WCF等等。这是对HTTP请求的响应。

相反,如果您发送无效消息,例如:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:Add>
         <tem:a>x</tem:a>
         <tem:b>y</tem:b>
      </tem:Add>
   </soapenv:Body>
</soapenv:Envelope>

您会找回故障,例如:

HTTP/1.1 500 Internal Server Error

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <soap:Fault>
            <faultcode>soap:Client</faultcode>
            <faultstring> ... exception stacktrace here ... </faultstring>
            <detail />
        </soap:Fault>
    </soap:Body>
</soap:Envelope>

您可以使用SoapUI自动化测试,甚至可以将它们与Junit集成,甚至可以使用JMeter之类的东西,尽管它不是专门为Web服务而设计的(例如SoapUI),但它可以测试SOAP。您当然可以使用我添加到答案中的那个基本客户端。