@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。您当然可以使用我添加到答案中的基本客户端。