You receive an InvalidProgramException after compiling a Visual Basic .NET application and trying to run it

Posted: (EET/GMT+2)

 

I was today working on client's Visual Basic .NET application, and ran into an interesting issue: after building and trying to run the application outside Visual Studio 2013's debugger, the application crashed in with an InvalidProgramException. This was unexpected, as the application has worked fine just minutes ago.

It turned out it was some kind of a issue with the Visual Basic compiler. After trying another build command, the problem did not go away, but after doing a full rebuild, the problem went away. I suspect invalid IL code was generated by the compiler (or linker), when some of the build files had become stale.

In case you receive a similar error, do the classic thing: first, clean your solution, and then rebuild it. You might also try restarting Visual Studio in between.

There's also a Microsoft support article about this error that gives some background information on the error.