4

This is an InvalidOperationException when calling the Serialize method on an XmlSerializer object.

The full message of the exception is: "The environment block used to start a process cannot be longer than 65535 bytes. Your environment block is 85754 bytes long. Remove some environment variables and try again."

This happens on a Windows 7 machine in a .NET 4.0 application trying to serialize an object that implements IXmlSerializable. The actual object is irrelevant because it happens with a number of different object, complex and simple but not anything specific. It is also not reproducible and there is not much information on the internet about this particular (quite specific) error message.

  1. Does anyone know why this happens and in what state your machine needs to be in to reproduce this?
  2. What would the appropriate steps be (manually or programmatically) to correct the problem on the client machine.

Thanks in advance for any advice or solutions, this is the first time I've ever seen this occur and one of the few times I haven't found an answer on StackOverflow.

BrutalDev
  • 6,181
  • 6
  • 58
  • 72
  • 2
    The error message indicates there is something up with the machine's environment variables. "Remove some environment variables and try again" Is there something special about this particular Windows 7 box? – xspydr Jan 23 '14 at 19:01
  • I understand the error message but I'm not sure how or why a machine gets into this type of state. I can't tell much about the actual machine or it's configuration since it's an error reported by a client. I am waiting for more information but I thought I would ask in the meantime in case anyone knows something more. – BrutalDev Jan 23 '14 at 19:06
  • It could be some sort of machine corruption or the machine your app is running on is in a bad state. Especially if the app is running in a shared hosting environment with others using the same machine. – xspydr Jan 23 '14 at 19:10
  • The exception message is crystal-clear, very unclear why you are asking this question. XML serialization uses System.CodeDom to run the C# compiler, obviously that's what failed here. Using sgen.exe would be a workaround, no perceivable reason to avoid fixing the real problem. – Hans Passant Jan 23 '14 at 19:27
  • @HansPassant - The application does ship with XmlSerializers assemblies generated with SGEN at build time so that is not a valid workaround, the error still happens with the files in place. I am aware the exception message tells the story and how to correct it. This is what I told the user to do pointing them to the environment variables section through the control panel. The questions (marked 1 and 2) are why and how this can happen and if there is a reasonable programmatic solution to recover? Users don't even know what environment variables are let alone being able to 'remove' some. – BrutalDev Jan 23 '14 at 19:56
  • *Why* and *how* cannot be answered without actually *seeing* all the environment variables. Type `set` on the command line and see for yourself at the first place. – Ondrej Tucny Jan 23 '14 at 20:07

1 Answers1

0

The environment block is where environment variables are stored. Before running the build, have the client check the environment variables. If it is not a large list, then it could be that environment variables are generated as part of the build process. Build systems such as Jenkins or Bamboo tend to generate environment variables, and therefore checking during the build would be the best way to determine what the problem is.

If you are able to capture the error and output the environment (by running "env" in CMD or Powershell) it may lead to further insights, and allow the client to trip their environment down.

Duff
  • 1,389
  • 1
  • 11
  • 21