Microsoft KB Archive/815211

= BUG: &quot;InvalidOperationException&quot; error message occurs when you consume a Web Service with an Out parameter =

Article ID: 815211

Article Last Modified on 9/13/2005

-

APPLIES TO


 * Microsoft Web Services Enhancements for Microsoft .NET 2.0
 * Microsoft Web Services Enhancements for Microsoft .NET 1.1
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



SYMPTOMS
You can use Visual C# .NET to create a Web Service that has a Web Service method with an Out parameter. The Out parameter may appear before the In parameter or the Ref parameter. You must specify the SoapRpcMethodAttribute to the Web Service method to preserve the order of the parameters. The problem occurs when you subsequently use this Web service that you created in Visual C# .NET in a Visual Basic .NET application. You may receive the following error message when you call the Web Service method.

An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll

Additional information: There was an error generating the XML document.



CAUSE
If the Web Service method has an Out parameter, and you generate the proxy by using Visual Basic .NET, then the Out parameter is generated as a ByRef parameter. This occurs because Visual Basic .NET does not support Out parameters. The reflection code cannot differentiate between a real ByRef parameter and a ByRef parameter that corresponds to an Out parameter. In the proxy class, the actual call to the Web service is completed by using the Invoke method. In the Invoke method call, the Out parameter is missing. However, the generated serializer expects the parameter. Therefore, you receive the error.



RESOLUTION
To resolve this problem, you can pass some dummy values for the ByRef parameters that correspond to the out parameters. You can do this in the Invoke method of the Web service proxy class. To do this, follow these steps:  Open the code for the Web service proxy file in Visual Studio .NET. Or, you can use a text editor such as Notepad.

The Web service proxy file is located in the Web References folder in the Web Application directory.  Locate the Web Service method. Add the default values for the missing ByRef parameters to the parameters for the Invoke method.

For example, you can modify the proxy file that is used in the sample code as follows:  _ Public Function SayHelloWorld(ByRef sOutParam As String, ByVal iParam As Integer) As String Dim results As Object = Me.Invoke(&quot;SayHelloWorld&quot;, New Object {Nothing, iParam}) sOutParam = CType(results(1),String) Return CType(results(0),String) End Function Note In the Invoke method parameters, you must initialize the object array with {Nothing, iParam}, instead of only {iParam}. 



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



Steps to Reproduce the Problem
 Create a new ASP.NET Web service application. Use Visual C# .NET to create the application. Name the Web service application HelloWorld . In Microsoft Solution Explorer, right-click Service1.asmx and then click View Code.</li>  Add the following Web Service method to the Web service: [WebMethod, System.Web.Services.Protocols.SoapRpcMethod] public string SayHelloWorld(out string sOutParam, int iParam) {        sOutParam = iParam.ToString; return &quot;Hello World&quot;; } </li> On the Debug menu, click Start.

Note the URL for the Web service.</li> Create a new Visual Basic .NET console application project.</li> In Solution Explorer, right-click References and then click Add Web References.</li>  Add the following code to the Main method: Dim obj As New localhost.Service1 Dim sOut, sResult As String

sResult = obj.SayHelloWorld(sOut, 10) Console.WriteLine(sOut) </li> On the Debug menu, click Start to run the application.

You receive the error message in the &quot;Symptoms&quot; section.</li></ol>

<div class="references_section">