wcfworkflowworkflow-foundation-4workflowservice

How to fix 'System.ServiceModel.Channels.ReceivedFault' cannot be serialized


I have a workflow service. I also use workflow persistence in that service. But after I deployed workflow in IIS, from client I make a request to workflow service, in log file on server. I see a message

The execution of the InstancePersistenceCommand named {urn:schemas-microsoft-com:System.Activities.Persistence/command}SaveWorkflow was interrupted by an error.InnerException Message: Type 'System.ServiceModel.Channels.ReceivedFault' cannot be serialized.
Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.
If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for other supported types.

I tried research about this exception, but I didn't find anything.

How to fix this problem ? or let me know what is the reason about above exception ?


Solution

  • System.ServiceModel.Channels.ReceivedFault is a non-serializable internal .NET framework class, so unfortunately there is nothing you can do to correct the actual root cause (i.e. making said class serializable).

    You are probably calling an external service via WCF which faults, i.e. a System.ServiceModel.FaultException is thrown. IIRC, somewhere deep down in that FaultException object is a reference to the culprit, a ReceivedFault instance.

    Solution: Catch all FaultExceptions, transfer the information you need to know from the FaultException into a serializable exception, and throw the latter:

    try
    {
        CallService();
    }
    catch (FaultException ex) 
    {
        // Gather all info you need from the FaultException
        // and transport it in a serializable exception type.
        // I'm just using Exception and the message as an example.
        throw new Exception(ex.Message);
    }