BUG: Web Method Parameters Are Mismatched if You Pass a Null Parameter When You Call a Web Service

Isenção de Responsabilidade para Conteúdo da KB Desativado

Este artigo foi escrito sobre produtos para os quais a Microsoft já não fornece suporte. Por conseguinte, este artigo é oferecido "tal como está" e não será mais atualizado.

Symptoms

Note The following .NET Framework Class Library namespaces are referenced in this article:

System.Web.Services

ASP.NET XML Web services allow you to control the formatting and call style of methods in a Web service. You can control this behavior by using attributes applied to the Web methods in the Web service. However, values that are incorrectly matched with the parameters in the Web service may be passed to the Web service by a .NET client. For example, the Web service may serialize the second parameter passed by the client into the first parameter of the Web service.

Cause

One particular call style and formatting selection can cause the values passed to the Web service by a .NET client to be incorrectly matched with the parameters in the Web service. This issue can occur if you specify a Document style Web method with Encoded and Bare parameters. Additionally, the Web method must have multiple parameters of the same type. If a client application uses a .NET client proxy to call such a Web service, and one of the parameters that is not at the end of the parameter list is null (or Nothing, if you are using Microsoft Visual Basic .NET), this issue occurs. The positions of the parameters after the null parameter that the Web service receives are shifted down the parameter list, and the wrong values are serialized into the parameters of the Web method.

The Web method parameters in SOAP messages that clients send for the Web method style described in the first paragraph of the "Cause" section of this article have no information associated with them that indicates which Web method parameters they correspond to. Therefore, ASP.NET Web services rely on the position of the parameters in the message to match them with the correct Web method parameters. If a client passes null for one of them, nothing is sent for that parameter. Because the null parameter is missing, the following parameter assumes the position of the missing parameter, and therefore ASP.NET incorrectly matches the parameter in that position with the corresponding Web method parameter.

Resolution

To work around this issue, do not specify the Encoded and Bare parameters for your Document style Web methods. If you need to implement those kinds of Web methods, avoid giving them multiple parameters of the same type, or make sure that client applications do not pass null for method parameters when they make calls.

Status

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

More Information

Consider a Web service with a Web method that has three string parameters defined as follows:
namespace DataType
{
using System;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Services.Description;
using System.Xml;


public class StringTest: WebService
{
[WebMethod]
[SoapDocumentMethod(Use=SoapBindingUse.Encoded,ParameterStyle=SoapParameterStyle.Bare)]
public void RetStringByRef(string x, ref string y,string z)
{
string x_value;
string y_value;
string z_value;
x_value=x;
y_value=y;
z_value=z;
}
}
}
The .asmx file corresponding to this Web Class is defined as follows:
<%@ WebService Language="c#" Class="DataType.StringTest" %>
If a client application calls the RetStringByRef() Web method and passes null for the first parameter (x), the value that the client passed for the y parameter now appears in the x parameter, and the z value now appears in the y parameter.

Steps to Reproduce the Problem

  1. Create a C# Web service project, and then add an .asmx file named StringTest.asmx to the project.
  2. Define the Web service class as described earlier in the "More Information" section of this article, and then compile the project.
  3. Create a test application.

    Any .exe application type will do.
  4. Add a Web reference to the Web service that you just created.
  5. Call the Web service by passing null for the first parameter. The following C# code demonstrates this procedure:
    localhost.StringTest x1;
    x1 = new localhost.StringTest();
    string yParam = "y";
    string zParam = "z";
    x1.RetStringByRef(null, ref yParam , zParam);
  6. Debug the Web service.

    Note that the parameters have shifted. The values from the preceding call that the Web method receives are now as follows:
    x="y"
    y="z"
    z=null
Because you are sending a null parameter, the DOCUMENT/ENCODED/BARE format shifts the parameters, and therefore the parameters no longer match what is specified for the Web method.
Propriedades

ID do Artigo: 318037 - Última Revisão: 24 de nov de 2008 - Revisão: 1

Comentários