This week I ran into the most bizarrely annoying problem, and I found that there was very little information available to guide me.

I had a solution that included a BizTalk WCF-NetMsmq receive location for which I had published metadata as well. However, every time I tried to generate proxy code, either using Visual Studio’s add service reference or the Svcutil command line tool, I ended up getting the below error.

Some posts on the blogosphere suggested all the schema’s element form default should be set to qualified. This didn’t sit right with me because I am sure that WCF happily supports different XML semantics.

Other suggestions (for example ) were to play with the options available in the add service reference and Svcutil tools, suggesting to generate message contracts, toggle reusing external references, and even override the serializer that was used. None of these options worked for me.

Another suggestion was to revert back to using .Net 2.0 style web references instead. Once again a suggestion that I did not want to consider

Others mentioned that the problem could be with my WSDL, but i had used the BizTalk WCF publishing wizard. Surely this could not have steered me wrong? I decided to investigate anyways. I attempted to isolate the problem by cutting down on the operations that the WCF service contained till I isolated the problem to a specific operation. I then dove into the schema (which for the record was not created by me :)) and found that there was an element with a Max Occurs set to 0. DOH!!! Fixing this setting allowed for proxy code to be happily generated from my WSDL.  Further investigation shows that if you set Max Occurs to 0 for a record or a any simple element type then you wont run into problems, however if the element has a restriction on it (such as an enumeration or a regular expression pattern) then this could potentially cause problems.

Takeaway is that if you encounter the same error as me, chances are that you have a problem with one of your schemas, as the BizTalk WCF publishing wizard is fairly trustworthy and does a reasonably good job at making sure that your metadata is in a usable state.