SOLVED: Compile the vSphere Management SDK WSDL for C#.NET leads to CS8078: An expression is too long or complex to compile
If you want to generate a .NET assembly to use the vSphere Management SDK to manage a vSphere environment you may run into difficulties.
VMware (now Broadcom) dropped support for the vSphere Management SDK for .NET stating that developers should use "modern programming languages" such as Java, or pyvmomi and govmomi for Python/Go.
If you want to use the vSphere Management SDK (which is a SOAP web service) you need to import the WSDL into .NET. Ideally you want to compile this into a .NET library of the version of the framework you're using. However this web service is extremely large and can cause several issues. Having spent a day or two on this I decided to document the process.
While you're here why not check out our VMware documentation tool - XIA Configuration Server?
Create the VimService.cs File
- Access the VMware SDK site
https://developer.vmware.com/sdks - Find the vSphere Management SDK.
- Extract the files to a temporary location such as
C:\Temp\VMware - Open the Visual Studio Developer Command Prompt.
- Change directories to the source WSDL files - for example.
C:\temp\VMware\SDK\vsphere-ws\wsdl\vim25 - Run the command
wsdl.exe vim.wsdl vimservice.wsdl /namespace:vSphere - A new file called VimService.cs will be created.
Create the Library
- Create a new Visual Studio .NET Framework Library Project
C:\Temp\VMware\Binaries\vSphere - Ensure the project targets the correct .NET Framework - for example .NET Framework 4.8.
- Add a reference to System.Web and System.Web.Services.
- Set the assembly information as appropriate.
- Create a file called vSphere.cs
- Paste the contents of the VimService.cs file into the vSphere.cs file - this is a large file and may take some time to format.
- Set the solution to “Release”.
- Build the solution.
- The code may fail to compile because of the following error
CS8078: An expression is too long or complex to compile - In the Properties > Build > Output settings set “Generate serialization assembly” to “Off”.
- Rebuild the project.
- The file vSphere.dll should be created.
Improve Performance
The assembly has now been created without any serializers - this will have a bit impact on performance.
- You can now manually create the serializer file with sgen.exe.
- Start the "Developer Command Prompt".
- Change directory to the Release output directory of the Visual Studio project where the vSphere.dll was created.
- Run the following command.
sgen.exe vSphere.dll - This process may take in excess of 6 hours, after it completes a new file vSphere.XmlSerializers.dll should be created.
- Make a backup of the vSphere.XmlSerializers.dll file that was created in another directory.
- Back in Visual Studio comment out the XmlIncludeAttributes by performing a find and replace on the following.
[System.Xml.Serialization.XmlIncludeAttribute
//[System.Xml.Serialization.XmlIncludeAttribute - Add the following attribute to reference the XmlSerializers assembly.
[System.Xml.Serialization.XmlSerializerAssemblyAttribute(AssemblyName = "vSphere.XmlSerializers")]
public partial class VimService : System.Web.Services.Protocols.SoapHttpClientProtocol
{ - Rebuild the project.
- If the vSphere.XmlSerializers.dll is overwritten by the build process replace it with the backup.
COMPLETE! The library can now be used in your .NET projects.
Comments
Post a Comment