locked
Serialization error in a for each loop RRS feed

  • Question

  • I have a for each loop in a 3.5 workflow. This takes a list if criteria to build a query string to call a Secure Webservive necesitating the use of a certificate. I load the certificate further up stream and pass it as a dependency property. The process returns a serialization error for the certificate. How do I avoid this error?  Thanks!
    Wednesday, August 18, 2010 9:29 PM

Answers

  • What you would need to do is keep the information needed to create the certificate instance and create an instance each time you need to make the call to the secure service. (and don't store the certificate instance in a workflow member). You would need to take this sort of approach with any types that you need to use in a workflow that are not serializable - when the workflow persists any items that are not serializable will cause an exception similar to this.

    If you need to create the certificate and then use it in several succeeding activities that all occur in the same burst of work (between persistence) you could back the property that holds the certificate by a non serializable field and then you could share the instance of the certificate between multiple activities. However, once the workflow persists and unloads the certificate instance would be lost, and you would need to create a new one if additional activities needed to use the certificate.

    I can create some sample code showing this approach (not with a certificate but with a sample class that is not serializable) if you give me some more details of the scenario (where it is created and how it is shared out to the other activities) if the ideas above are not able to solve the issue.

    Thanks,

    Steve Danielson [Microsoft]
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm

     

    Friday, August 20, 2010 5:32 PM
    Moderator

All replies

  • I'm erroring at this line.

    ChildContext = Manager.CreateExecutionContext(ChildActivity)

    and receiving this error

    System.Runtime.Serialization.SerializationException was unhandled by user code
      Message="Type 'System.Security.Cryptography.X509Certificates.X509Certificate2' in Assembly 'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable."
      Source="mscorlib"
      StackTrace:
           at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
           at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
           at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
           at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
           at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph)
           at System.Workflow.ComponentModel.Activity.Save(Stream stream, IFormatter formatter)
           at System.Workflow.ComponentModel.Activity.Clone()
           at System.Workflow.ComponentModel.ActivityExecutionContextManager.CreateExecutionContext(Activity activity)
           at HP.AirlineSOA.FP.NOTAMReceiver.Workflows.Activities.RetreiveSingleNotamActivity.Execute(ActivityExecutionContext executionContext) in C:\FP\FOT_Main\FP_Dev\FP\Source\NotamWx\NOTAMReceiver\Source\Workflows\Activities\RetreiveSingleNotamActivity.cs:line 263
           at System.Workflow.ComponentModel.ActivityExecutor`1.Execute(T activity, ActivityExecutionContext executionContext)
           at System.Workflow.ComponentModel.CompositeActivityExecutor`1.Execute(T activity, ActivityExecutionContext executionContext)
           at System.Workflow.ComponentModel.ActivityExecutor`1.Execute(Activity activity, ActivityExecutionContext executionContext)
           at System.Workflow.ComponentModel.ActivityExecutionFilter.Execute(Activity activity, ActivityExecutionContext executionContext)
           at System.Workflow.ComponentModel.FaultAndCancellationHandlingFilter.Execute(Activity activity, ActivityExecutionContext executionContext)
           at System.Workflow.ComponentModel.ActivityExecutorOperation.Run(IWorkflowCoreRuntime workflowCoreRuntime)
      InnerException:

    How do I get around this?

     

    Friday, August 20, 2010 3:09 PM
  • What you would need to do is keep the information needed to create the certificate instance and create an instance each time you need to make the call to the secure service. (and don't store the certificate instance in a workflow member). You would need to take this sort of approach with any types that you need to use in a workflow that are not serializable - when the workflow persists any items that are not serializable will cause an exception similar to this.

    If you need to create the certificate and then use it in several succeeding activities that all occur in the same burst of work (between persistence) you could back the property that holds the certificate by a non serializable field and then you could share the instance of the certificate between multiple activities. However, once the workflow persists and unloads the certificate instance would be lost, and you would need to create a new one if additional activities needed to use the certificate.

    I can create some sample code showing this approach (not with a certificate but with a sample class that is not serializable) if you give me some more details of the scenario (where it is created and how it is shared out to the other activities) if the ideas above are not able to solve the issue.

    Thanks,

    Steve Danielson [Microsoft]
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm

     

    Friday, August 20, 2010 5:32 PM
    Moderator
  • Thanks Steve,

    I'd appreciate the sample code. In this sequence I'm calling an activity that builds a https request; the URL and Network credentials are loaded in the activity. It hen calls a singleton that loads the cerificate; builds the Request object calls an external service and returns the results as a serialized string. Is that enough?  Thanks again!

     

    Mike

    Friday, August 20, 2010 8:14 PM
  • Hi Mike,

    So your scenario is that you have a Sequence (part of the custom activity or just a sequence in the workflow?) that contains a custom activity. This custom activity build an http request, and then it calls a singleton that loads the certificate, builds the request object, and then calls an external service. Does all of this take place within the execute of the custom activity? I am guessing that there is a property that holds the certificate, where does this property live. Could you show a screenshot of the workflow and show which pieces are what? I looked at your code again and I want to make sure I am replicating your scenario.

     Steve Danielson [Microsoft]
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm

     

     

    Monday, August 23, 2010 9:13 PM
    Moderator