6

I have recently installed Visual Studio 2012.

However, after the installation, a project that was previously working correctly in Visual Studio 2010 began throwing and crashing on the following error:

'System.TypeInitializationException: The type initializer for 'MyType' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'MyAssembly.XmlSerializers' or one of its dependencies. The system cannot find the file specified.

at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)

at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)

I did not upgrade the project to VS2012, I am running it out of VS2010 as before. I have not made any changes to the project itself. The only change was that I installed VS2012 (and .NET 4.5) on the machine.

What could be causing this unexpected problem?

user472875
  • 3,115
  • 4
  • 37
  • 68
  • Have you checked for that assembly in the project References? If it's there, look at the path it's looking for it in in the Properties window and make sure it's correct. – itsme86 Jan 30 '14 at 19:14
  • *.XmlSerializers.dll is not a standard Reference. It is a reference that is generated on the fly by .Net when needed. – user472875 Jan 30 '14 at 19:16
  • Same thing then. Are you sure the path that it points to when it's generated on the fly is valid? – itsme86 Jan 30 '14 at 19:18
  • The file never gets generated, so the path is not valid. However, I have not made any code or configuration changes to the project itself. – user472875 Jan 30 '14 at 19:22
  • I got this recently and found that it was obscuring a BadImageFormatException. Try turning on fusion logging and see if there is an assembly that won't load. I think the exception is a red herring. – Jay Jan 30 '14 at 19:46
  • It is an entirely normal exception and doesn't "crash" your program. Debug + Exception, untick the Thrown checkboxes. You can avoid it by prebuilding the serialization assemblies with sgen.exe – Hans Passant Jan 30 '14 at 21:30
  • possible duplicate of [FileNotFoundException in ApplicationSettingsBase](http://stackoverflow.com/questions/3494886/filenotfoundexception-in-applicationsettingsbase) – Hans Passant Jan 30 '14 at 21:30
  • @HansPassant: In my case it does. I have run the application without debugging and it still fails downstream of the original source of the error. – user472875 Jan 30 '14 at 22:08
  • Well, you are not looking at the right exception then. Press F5 to continue running until the real problem occurs. – Hans Passant Jan 30 '14 at 22:13
  • @HansPassant: see my answer below. – user472875 Feb 07 '14 at 15:15
  • Right, this is exactly what I told you, not a real exception. Have you found the Debug + Exceptions dialog yet? – Hans Passant Feb 07 '14 at 15:26
  • The real exception was a TypeIntializationException downstream caused directly by the absense of the XmlSerializers DLL for that assembly. It might not have been a real exception but it certainly caused real problems. – user472875 Feb 07 '14 at 15:33
  • It seems that you've accidentally disabled "Just My Code" option. It could show these exceptions. – ForNeVeR Apr 05 '16 at 07:06

2 Answers2

4

The situation was a little trickier than I had expected. The XMLSerializers DLL for that assembly seems to be essential for authentication in my solution, and just "Press[ing] F5 to continue running until the real problem occurs" did not lead to some unrelated error.

I have not been able to completely understand what the underlying issue is, but it seems that there is a problem creating the DLL dynamically after Visual Studio is upgraded.

The way I have worked around the issue is by running the sgen tool to manually create the serializers and setting the flag "Generate Serialization Assemblies" to Off. The process can be automated by adding a post-build instruction to run the tool.

That being said, I have no idea why this issue comes up in the first place, as I am certain that I did not open the project in VS2012.

user472875
  • 3,115
  • 4
  • 37
  • 68
1

With me, the problem is when you update Visual Studio, the VS developers think that you need reset your settings back to default. So they remove System.XMLSerializers from References,they disable "Enable source server support" from Tools -> Options -> Debugging -> General menu.

Add above reference and update setting makes error gone.

Leo Lucas
  • 15
  • 1
  • 9