Sage CRM fixing WSDL errors in .NET

Have you ever encountered a runtime error in .NET when using a web reference of CRM?
Something like: "The type initializer for Program threw and exception. There was an error reflecting property".

Good news is this is an easy fix. When the WSDL has been referenced and attached to the project, Visual Studio creates a whole bunch of classes for this reference. You can edit these classes in VS and make them work. In my example I used C#.

All you need to do is go to the classes and modify the faulty ones. Sometimes CRM will import, from the WSDL, multiple of the same field names, and create properties in the classes in C#, which will cause runtime errors. These can be removed manually in the code.

  1. Navigate to your project solution folder. There should be a folder called "Web References"
  2. In this folder is the folder you names your web reference. Open this folder
  3. Look for a file called "Reference.cs". This is the references class that C# generates when the WSDL gets imported
  4. You can edit this class and make the necessary modifications to the properties/methods as required.
  5. You will also note that the WSDL is in this folder, modifiying the WSDL here is also an option, but then you need to reload the WSDL from this source to update the methods and properties for the Reference.cs class.