'How to solve BinaryFormatter Deserialize 3th party object Exception in C#?

In my project, I have a function which is used for loading objects from files:

public static ObjectBase LoadObject(string _filePath, string _name)
{
    ObjectBase module = null;

    try
    {
        IFormatter formatter = new BinaryFormatter();
        Stream streamRead = new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.None);

        object ob = formatter.Deserialize(streamRead);
        module = (ob as ObjectBase);
        streamRead.Close();
    }
    catch (Exception ex)
    {
        return null;
    }
    finally
    {
    }
    return module;
}

Everytime when I run this function, I hit exception:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Operation is not valid due to the current state of the object.
 at Cognex.VisionPro.Implementation.Internal.ccComFactory.CreateInstance()
 at Cognex.VisionPro.FGGigE.Implementation.Internal.CogAcqFifoGigE._com()
 at Cognex.VisionPro.FGGigE.Implementation.Internal.CogAcqFifoGigE..ctor(SerializationInfo info, StreamingContext context)

 --- End of inner exception stack trace ---

 at System.RuntimeMethodHandle.SerializationInvoke(IRuntimeMethodInfo method, Object target, SerializationInfo info, StreamingContext& context)
 at System.Runtime.Serialization.ObjectManager.CompleteISerializableObject(Object obj, SerializationInfo info, StreamingContext context)
 at System.Runtime.Serialization.ObjectManager.FixupSpecialObject(ObjectHolder holder) at System.Runtime.Serialization.ObjectManager.DoFixups()
 at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
 at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
 at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
 at My code file.

Message: "Operation is not valid due to the current state of the object." Source: "Cognex.VisionPro.FGGigE" StackTrace:
 at Cognex.VisionPro.Implementation.Internal.ccComFactory.CreateInstance()
 at Cognex.VisionPro.FGGigE.Implementation.Internal.CogAcqFifoGigE._com()
 at Cognex.VisionPro.FGGigE.Implementation.Internal.CogAcqFifoGigE..ctor(SerializationInfo info, StreamingContext context)

Anyone have ideas? I really appreciate your helps!



Solution 1:[1]

I had the same problem when trying to load vpp file (cognex) using this code line..

cogJobManager = CogSerializer.LoadObjectFromFile(jobFilesPath) as CogJobManager;

the solution for me was to uncheck the "Prefer 32-bit" from the project properties under Build tab, and compile it as Any CPU

after that the problem was solved

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Steffen Moritz