2

I'm getting the most absurd error when calling Marshal.ThrowExceptionForHR(). My application performs certain FS operations and uses Marshal.ThrowExceptionForHR() to get the proper exceptions with the proper error messages.

However, in some tests we sometimes get the following error instead of the correct one.

'UnrelatedAssembly' is a real assembly in the application, but it isn't related in any way to my code, and isn't even referenced. (My code is on a lower level). It does use XmlSerializer. I've seen the detailed explanation about serializers Here, but it doesn't explain this behavior.

My only theory is that the XmlSerializer cache somehow operates in a multi-threaded environment and that the compilation that's meant for 'UnrelatedAssemby' is performed in my thread. That would require Marshal to also use XmlSerializer...

Can anyone confirm, correct, or otherwise explain this?

Thanks,

System.IO.FileNotFoundException: Could not load file or assembly 'UnrelatedAssembly.XmlSerializers' or one of its dependencies. The system cannot find the file specified. File name: 'UnrelatedAssembly.XmlSerializers'
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)
at System.Reflection.RuntimeAssembly.LoadWithPartialNameInternal(AssemblyName an, Evidence securityEvidence, StackCrawlMark& stackMark)
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at MyAssembly.Win32.ThrowLastError()

My code:

    public static void ThrowLastError()
    {
        int err = Marshal.GetHRForLastWin32Error();

        Marshal.ThrowExceptionForHR(err);
    }
Community
  • 1
  • 1
Jimbidf
  • 391
  • 1
  • 15
  • Does the class where the method exists is have a static constructor or a static field that uses `UnrelatedAssembly`? – theB Jun 20 '16 at 14:35
  • @theB No, like I said, it's not referenced in any way, and is above `MyAssembly` in the references hierarchy – Jimbidf Jun 20 '16 at 14:53
  • 1
    It is an inevitable risk when you go for the "last error", this particular exception is [pretty notorious](http://stackoverflow.com/a/3798614/17034). It is easy to debug, you can see the first chance exception notification in the Output window and you can get the debugger to stop when it is thrown. – Hans Passant Jun 20 '16 at 15:17
  • That's closer to the truth, but it's not a first chance exception. It happens in the QA labs, with entire system tests. I haven't managed to reproduce it while debugging, actually. – Jimbidf Jun 20 '16 at 15:35

0 Answers0