@John Saunders在他的评论中是正确的。无论你使用ASMX做什么,你都应该可以使用WCF。事实上,只要您执行适当的SOAP请求,您的Web服务就会使用哪种框架/技术并不重要。
WCF只是一个帮助构建面向服务的应用程序的框架。像其他任何类型的框架一样,它可以让您专注于您将提供的实际服务,同时关注将该服务作为SOAP Web服务公开所需的所有管道功能。
至于SoapUI,它是一个Java工具,允许您测试Web服务。当你为它提供一个WSDL时,it dynamically creates请求样本然后发送到Web服务(如果我没有弄错的话)Http Client。
如果您有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>
可以automate the tests with SoapUI甚至与Junit整合它们,你可以甚至使用类似JMeter的东西,虽然不是专门为Web服务设计的(如SoapUI)it can test SOAP。你可以在课程中使用我添加到我的答案中的基本客户端。
我不确定你的意思。就像您所说的那样,通过使用POST Web请求发送SOAP XML来进行调用。 WCF将处理事物的序列化和Web请求/响应级别。 – lockstock
@s mac:我建议你解释你正在尝试做什么,而不是你目前如何尝试。删除所有的评论,而是建立一个简单,简洁的问题。你提到你是一个测试人员。你想测试一个WCF Web服务并且想要完全控制SOAP有效载荷。是吗?为什么你想在低级别而不是使用WCF客户端或SoapUI? – JohnDoDo
好的,约翰......我再次尝试。由于我不熟悉WCF或SOAP,这是我知道如何解释它的唯一方法。我相信有一个明显的方法可以做到(对你和其他人),我只是不知道它是什么,我已经寻找好几天了。如果您有更多问题,请告诉我。谢谢。 –