BUG: "InvalidOperationException" error message occurs when you consume a Web Service with an Out parameter (815211)



The information in this article applies to:

  • Microsoft Web Services (included with the .NET Framework) 1.0
  • Microsoft Web Services (included with the .NET Framework 1.1)
  • Microsoft Visual C# .NET (2002)
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual Basic .NET (2003)

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:
  1. 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.
  2. 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:
            <System.Web.Services.Protocols.SoapRpcMethodAttribute("http://tempuri.org/SayHelloWorld", RequestNamespace:="http://tempuri.org/", ResponseNamespace:="http://tempuri.org/")>  _
            Public Function SayHelloWorld(ByRef sOutParam As String, ByVal iParam As Integer) As String
             Dim results() As Object = Me.Invoke("SayHelloWorld", 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.

MORE INFORMATION

Steps to Reproduce the Problem

  1. Create a new ASP.NET Web service application. Use Visual C# .NET to create the application. Name the Web service application HelloWorld.
  2. In Microsoft Solution Explorer, right-click Service1.asmx and then click View Code.
  3. 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 "Hello World";
          }
  4. On the Debug menu, click Start.

    Note the URL for the Web service.
  5. Create a new Visual Basic .NET console application project.
  6. In Solution Explorer, right-click References and then click Add Web References.
  7. 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)
  8. On the Debug menu, click Start to run the application.

    You receive the error message in the "Symptoms" section.

REFERENCES

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

309013 HOW TO: Create and Test an XML Web Service in Visual Basic .NET


Modification Type:MinorLast Reviewed:9/13/2005
Keywords:kbvs2002sp1sweep kbXML kbJIT kbDev kbClientProtocols kbProxyDev kbInetDev kbNameSpace kbProgramming kbbug KB815211 kbAudDeveloper