Friday, December 5, 2014

.NET WSDL integration array error

I have not had a lot of need for web services over the years, but now and then they have come in handy, so I have had to learn about them; including some of the hair pulling issues that they can result in.

For this post I was trying to integrate a C#.NET solution with SalesForce.com.  A few years ago I had done the same thing using the older Web References which I was more familiar with and which were less flexible and therefore simpler to use.  However this time I had a bigger development window and decided to do things right using the newer Service Reference.

Most of the changes were not too hard, just took some digging since so much of the Sales Force documentation uses the old web references code.  Add to that the fact that Sales Force had made some decent sized changes to their WSDL over the years and it took me a full day to get the code to compile the first time.

When I ran it for the first time I received this odd error :

Error CS0030: Cannot convert type 'TRCWebApp.SalesForceEnterprise.ListViewRecordColumn[]' to 'TRCWebApp.SalesForceEnterprise.ListViewRecordColumn'

The most confusing part initially was that I didn't recognize the class from the basic login code I had written, I had never used it.  Fortunately, I was not the first person to run into this problem, and I was able to easily find the solution here.  What bothered me enough to write this post is that it was not an error in my code, or even in the Sales Force WSDL.  It was actually an error in the .net code generator that created the Reference.cs class from the WSDL.

Apparently the "code generation component cannot handle the XSD definitions that have only one element and the occurrence of the element is unbounded."

There are two solutions listed that I found:
1. "to manually modify the schema and altered the constructors for those 2 classes mentioned above by adding an extra dummy attribute".  The Sales Force specific fix is shown here, of course you would have to regenerate the code after making the change:

2. The option I chose was quite a bit simpler as long as you know where to find the generated Reference.cs file in the Service References folder of your project.  "I had to delete the extra [] from public ListViewRecordColumn[][] records in Reference.cs which is a file that is formed from the WSDL".  I opened the References.cs file in Notepad++ to follow those directions, did a search for ListViewRecordColumn[][] and renamed all two instances of it to ListViewRecordColumn[].

Problem solved, my code compiled and was able to talk to Sales Force.