Microsoft KB Archive/318037

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

Article ID: 318037

Article Last Modified on 2/23/2007

-

APPLIES TO


 * Microsoft Web Services Enhancements for Microsoft .NET 2.0
 * Microsoft Web Services Enhancements for Microsoft .NET 1.1

-



This article was previously published under Q318037



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 &quot;Cause&quot; 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=&quot;c#&quot; Class=&quot;DataType.StringTest&quot; %> 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
 Create a C# Web service project, and then add an .asmx file named StringTest.asmx to the project. Define the Web service class as described earlier in the &quot;More Information&quot; section of this article, and then compile the project. Create a test application.

Any .exe application type will do. Add a Web reference to the Web service that you just created.  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 = &quot;y&quot;; string zParam = &quot;z&quot;; x1.RetStringByRef(null, ref yParam, zParam); </li>  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=&quot;y&quot; y=&quot;z&quot; z=null </li></ol>

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.

Keywords: kbbug kbcaching kbconfig kbctrlcreate kbdatabinding kbdebug kbdeployment kbhttpruntime kblocalization kbperformance kbscalability kbsecurity kbservercontrols kbstate kbupgrade kbvalidation kbwebforms KB318037

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.