XML Serialization Surprise in VS 2005

In a post last year, I wrote about the pitfalls when generating classes using the xsd tool in .Net framework 1.1. And some preliminary tests had suggested that the issues of xsd not generating the correct serializable class had been fixed in framework 2.0. A recent project at a client site had proved this to be wrong.

As it turned out, while the .net framework 2.0 solved some of the xml serialization problems that plagued developers using framework 1.0, the same problems manifest themselves in different ways in framework 2.0. And the symptom of this problem can be mysterious and hard to identify.

In my recent consulting position, I was developing some code using Visual Studio 2005 to integrate with some third party web services. But when I added a web reference to the service and wrote a simple one line program to test it, I got this exception: (note, I have replaced the class name, project name and path with {…}, so that I don’t have to reveal any information that might be related to the project.)

Server Error in ‘/{class name} Application.

Unable to generate a temporary class (result=1).
error CS0029: Cannot implicitly convert type ‘string’ to ‘string[]’

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Unable to generate a temporary class (result=1).
error CS0029: Cannot implicitly convert type ‘string’ to ‘string[]’

Source Error:

Line 59:

Line 60: /// <remarks/>

Line 61: public {Method name}() {

Line 62: string urlSetting = System.Configuration.ConfigurationManager.AppSettings[“WebReference.{service name}”];

Line 63: if ((urlSetting != null)) {


Source File: c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\{project path}\d8d15552\e16aa6bd\App_WebReferences.kd_c-r9m.0.cs Line: 61

Stack Trace:

[InvalidOperationException: Unable to generate a temporary class (result=1).

error CS0029: Cannot implicitly convert type ‘string’ to ‘string[]’

]

System.Xml.Serialization.Compiler.Compile(Assembly parent, String ns, CompilerParameters parameters, Evidence evidence) +1389

System.Xml.Serialization.TempAssembly.GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence, CompilerParameters parameters, Assembly assembly, Hashtable assemblies) +3416

System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location, Evidence evidence) +171

System.Xml.Serialization.XmlSerializer.FromMappings(XmlMapping[] mappings, Type type) +241

System.Web.Services.Protocols.SoapClientType..ctor(Type type) +546

System.Web.Services.Protocols.SoapHttpClientProtocol..ctor() +243

WebReference.{service name}..ctor() in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\{path name}\d8d15552\e16aa6bd\App_WebReferences.kd_c-r9m.0.cs:61

_Default.Test() in C:\Documents and Settings\kwong\My Documents\Visual Studio 2005\WebSites\{path name}\Default.aspx.vb:11

_Default.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\kwong\My Documents\Visual Studio 2005\WebSites\{project name}\Default.aspx.vb:7

System.Web.UI.Control.OnLoad(EventArgs e) +88

System.Web.UI.Control.LoadRecursive() +74

System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3035


Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\{project name}\d8d15552\e16aa6bd\ App_WebReferences.kd_c-r9m.0.cs

At first, this looked like a permission issue. Since I had run into similar asp.net related permission issues before, this assumption sounded reasonable. After a quick search of internet, I found these two articles (http://support.microsoft.com/kb/896181, http://support.microsoft.com/kb/908158) on Microsoft’s website, and both articles suggested that I had some permission issue, even though the errors were not quite the same.

But after double checking my permission settings and trying on different machines, I was convinced that this was caused by permissions. So what could it be?

I tried to make sense of the error message, and the line “Cannot implicitly convert type ‘string’ to ‘string[]’” all of a sudden made me believe that this was due to similar issues as I experienced with Visual Studio 2003. Because by default Visual Studio 2005 does not generate the stub class and put it with the web project, but rather dynamically generates it during compile time (because of this, you see file name like “C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\{project name}\d8d15552\e16aa6bd\ App_WebReferences.kd_c-r9m.0.cs” in the error message.), the only way we might be able to correct this problem is to generate the proxy ourselves and forget about trusting the IDE to generate the class for you.

So I used wsdl.exe to generate the proxy, and sure enough, when I inspected the generated class, I found incorrect use of jagged arrarys ([][]) at places where a regular one dimensional arrays were needed. And after correcting these problems, I was able to consume the web service. By the way, when you use wsdl to generate the web service proxy you only need to include the generated class in your project and do not need to worry about adding the web reference, the proxy class is the reference to the web service.

Be Sociable, Share!

Leave a Reply