简体   繁体   English

测试Web服务是否缺少.NET中的必填字段

[英]Testing a web service for missing mandatory fields in .NET

Part of my current work involves using an external web service, for which I have generated client proxy code (using the WSDL.exe tool). 我当前工作的一部分涉及使用外部Web服务,为此我生成了客户端代理代码(使用WSDL.exe工具)。

I need to test that the web service correctly handles the absence of mandatory fields. 我需要测试Web服务可以正确处理缺少必填字段的情况。 For instance, Surname and Forename are mandatory - if they are absent from the call, then a SOAP fault block should be returned. 例如,姓和名是必需的-如果呼叫中不存在它们,则应返回SOAP故障块。

As you may have guessed, I cannot exclude any mandatory fields from my web service call when using the auto-generated proxy code because of compile-time checking against the schema. 您可能已经猜到了,由于对架构进行了编译时检查,因此在使用自动生成的代理代码时,我无法从Web服务调用中排除任何必填字段。

What I have done instead is to use HttpWebRequest and HttpWebResponse to send/receive a manually-formatted SOAP envelope to the web service. 相反,我要做的是使用HttpWebRequest和HttpWebResponse向Web服务发送/接收手动格式的SOAP信封。 This works, but because the service returns a 500 HTTP status code, an exception is raised on the client and the response (containing that SOAP fault block I need) is null. 这行得通,但是因为服务返回了500 HTTP状态代码,所以在客户端上引发了一个异常,并且响应(包含我需要的SOAP故障块)为空。 Basically I need the return stream to get at the error data so I can complete my unit test. 基本上,我需要返回流来获取错误数据,以便可以完成单元测试。 I know the correct data is being returned because I can see it on my Fiddler trace, but I just can't get at it in my code. 我知道返回了正确的数据,因为我可以在Fiddler跟踪中看到它,但是我无法在代码中得到它。

Here's what I'm doing for the manual call, with the names changed to protect the innocent: 这是我为手动通话所做的事情,为了保护无辜者,更改了名称:

private INVALID_POST = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
    "<soap:Envelope ...rest of SOAP envelope contents...";

private void DoInvalidRequestTest()
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://myserviceurl.svc");
    request.Method = "POST";
    request.Headers.Add("SOAPAction",
        "\"https://myserviceurl.svc/CreateTestThing\"");
    request.ContentType = "text/xml; charset=utf-8";
    request.ContentLength = INVALID_POST.Length;
    request.KeepAlive = true;

    using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
    {
        writer.Write(invalidPost);
    }

    try
    {
        // The following line will raise an exception because of the 500 code returned
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        if (response.StatusCode == HttpStatusCode.OK)
        {
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                string reply = reader.ReadToEnd();
            }
        }
    }
    catch (Exception ex)
    {
        ... My exception handling code ...
    }
}

Please note that I am not using WCF, just WSE 3. 请注意,我不使用WCF,而仅使用WSE 3。

What about write your own SoapHttpClientProtocol and using SoapExtensions? 如何编写自己的SoapHttpClientProtocol并使用SoapExtensions?

    [WebServiceBinding()]
    public class WebService
    : System.Web.Services.Protocols.SoapHttpClientProtocol
    {
        [SoapTrace]
        public object[] MethodTest(string param1, string param2)
        {
            object[] result = this.Invoke("MethodTest", new object[] { param1, param2 });
            return (object[])result[0];
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM