SerializationException while Persisting inside UnloadInstance Method

Hi all,

while persisting,Im getting a SerializationException of the SqlConnection !So my workflow is gettin terminated and hence the instanceid is not gettin saved in the instancestate table! Its happening inside the UnloadInstance method when i try this

staticvoid UnloadInstance(object workflowInstance)

{

try

{

//Here is where gettin exception.

((WorkflowInstance)workflowInstance).TryUnload();

}

catch (PersistenceException ex)

{

MessageBox.Show("Exception in Unloadinstance " + ex.Message);

}

}

I made the Class which declaring the SqlConnection(Inside my Data Access Layer) as Serializable and even made the field SqlConnection object as NonSerialzed.But still its not working.

Inside the class where im persisting,i assigned the object of the Data Layer Class to NULL and even tried to make that class as Serialzed.But i cudnt fix the same!Kindly help me to sort it out...

Pls check this Exception im getting and advice me what i have to do to solve this out.I searched for the same type of exceptions in this site but stil couldnt fix it out!

Thanks in advance,

Sunath

--

{System.Workflow.Runtime.Hosting.PersistenceException: Type 'System.Data.SqlClient.SqlConnection' in Assembly 'System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable. > System.Runtime.Serialization.SerializationException: Type 'System.Data.SqlClient.SqlConnection' in Assembly 'System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.
at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
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.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)
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.Save(Stream stream)
at System.Workflow.Runtime.Hosting.WorkflowPersistenceService.GetDefaultSerializedForm(Activity activity)
at System.Workflow.Runtime.Hosting.SqlWorkflowPersistenceService.SaveWorkflowInstanceState(Activity rootActivity, Boolean unlock)
at System.Workflow.Runtime.WorkflowExecutor.Persist(Activity dynamicActivity, Boolean unlock, Boolean needsCompensation)
End of inner exception stack trace
at System.Workflow.Runtime.WorkflowExecutor.Persist(Activity dynamicActivity, Boolean unlock, Boolean needsCompensation)
at System.Workflow.Runtime.WorkflowExecutor.PerformUnloading(Boolean handleExceptions)
at System.Workflow.Runtime.WorkflowExecutor.TryUnload()
at System.Workflow.Runtime.WorkflowInstance.TryUnload()
at WorkflowManager.WFManager.UnloadInstance(Object workflowInstance) in D:\Sunath\Saastha Works\LOS\LoanOrginationSystem\WorkflowServer\WorkflowManager\WFManager.cs:line 527}

[4864 byte] By [Sunath] at [2007-12-24]
# 1

All data in your workflow must be serializable or you must only include the data that is serializable during serialization by implementing either ISerializable or a serialization surrogate.

Thanks,
Joel West
MSFTE - SDE in WF runtime and hosting

This posting is provided "AS IS" with no warranties, and confers no rights

JoelWest at 2007-8-31 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 2

Thanku soo much 4 ur quick Reply Joel..

So..as you said, since SqlConnection is not serializable,im getting this exception rite?So can i directly implement ISerializable interface to that Data Layer Class where i use the SqlConnection?Or is there any other way and wot abt serialization Surrogate? I wud b soo grateful to you if you can provide some code samples too..so that i can avoid asking more and more doubts to u abt the same!;)

Waiting 4 ur reply

Thanks in advance...

Sunath

Sunath at 2007-8-31 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 3

You'll need to implement ISerializable on a class that you control. So whatever class in your workflow is holding on to the Data Layer Class (if you didn't write the Data Layer Class) should implement ISerializable so that you can control what objects are serialized. Unfortunately I don't have any sample code handy but MSDN has good docs on ISerializable.

Thanks,
Joel West
MSFTE - SDE in WF runtime and hosting

This posting is provided "AS IS" with no warranties, and confers no rights

JoelWest at 2007-8-31 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 4

Thanku once again Joel...

I tried all the ways you asked me to...But im still gettin the same Exception!I wil tell u my scenario in detail this time,so that you can explain me much easily about it.

I have a StateMachine WF as the Base and in its StateInitialization activity im launching a Sequential WF which havin a CallExternalActivity and HandleExternalActivity.In the StateFinalisationActivity i hav put another HandleExternalActivity for the StateMachine WF to wait.So StateMachine WF is getting Idled and Persisted and unloaded and its instanceid is in instancestate table too.That part is fine but in the Sequential WF after calling the CallExternalActivity(Assiging a Task to a user),its getting idled for the next handleexternal but that time its gettin terminated while unloading sayin SqlConnection is not serializable!

Now i wil describe how the sqlconnection is...As i said,there is a DAL(Data Access Layer) which handling all the Database actions and in my workflowManager Class im just calling its object thats all! Since im getting this exception now i changed that code to another class so that there wont b any DAL accessing codes in the WorkflowManager class and implemented ISERIALIZABLE interface in the class where i use object of DAL.Am i right till here?Or i have to implement ISERIALiZABLE in DAL Class too?

From here im gettin confused...bcoz while implementing the method GetObjectData() inside the class i implement ISerializable,im confused how to add the values of SqlConnection into the Serializationinfo! Is it like this?

[Serializable]

public class GetXomlPaths : ISerializable

{

SqlConnection sqlcon;

public GetXomlPaths()

{

}

// I cant define this protected constructor which is a must for deserializing since im using the object of this class in the WorkflowManager!Im getting a compiletime error of the Security problem.Thats y i commented this constructor

//protected GetXomlPaths(SerializationInfo info, StreamingContext context)

//{

//}

[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]

public virtual void GetObjectData(SerializationInfo info, StreamingContext context)

{

info.AddValue("sqlcon",true);

info.SetType(typeof(SqlConnection));

}

}

Im really confused how to use the SerilzationInfo.addvalue to add the SqlConnection and how to fix that SerializationException of SqlConnection!& pls tell me wot i hav to do to use that protected constructor otherwise it wont work while deserializing!

Pls Help...

Waiting 4 ur valuable response...

Sunath.

Sunath at 2007-8-31 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 5

Thanku once again Joel...

I tried all the ways you asked me to...But im still gettin the same Exception!I wil tell u my scenario in detail this time,so that you can explain me much easily about it.

I have a StateMachine WF as the Base and in its StateInitialization activity im launching a Sequential WF which havin a CallExternalActivity and HandleExternalActivity.In the StateFinalisationActivity i hav put another HandleExternalActivity for the StateMachine WF to wait.So StateMachine WF is getting Idled and Persisted and unloaded and its instanceid is in instancestate table too.That part is fine but in the Sequential WF after calling the CallExternalActivity(Assiging a Task to a user),its getting idled for the next handleexternal but that time its gettin terminated while unloading sayin SqlConnection is not serializable!

Now i wil describe how the sqlconnection is...As i said,there is a DAL(Data Access Layer) which handling all the Database actions and in my workflowManager Class im just calling its object thats all! Since im getting this exception now i changed that code to another class so that there wont b any DAL accessing codes in the WorkflowManager class and implemented ISERIALIZABLE interface in the class where i use object of DAL.Am i right till here?Or i have to implement ISERIALiZABLE in DAL Class too?

From here im gettin confused...bcoz while implementing the method GetObjectData() inside the class i implement ISerializable,im confused how to add the values of SqlConnection into the Serializationinfo! Is it like this?

[Serializable]

public class GetXomlPaths : ISerializable

{

SqlConnection sqlcon;

public GetXomlPaths()

{

}

// I cant define this protected constructor which is a must for deserializing since im using the object of this class in the WorkflowManager!Im getting a compiletime error of the Security problem.Thats y i commented this constructor

//protected GetXomlPaths(SerializationInfo info, StreamingContext context)

//{

//}

[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]

public virtual void GetObjectData(SerializationInfo info, StreamingContext context)

{

info.AddValue("sqlcon",true);

info.SetType(typeof(SqlConnection));

}

}

Im really confused how to use the SerilzationInfo.addvalue to add the SqlConnection and how to fix that SerializationException of SqlConnection!& pls tell me wot i hav to do to use that protected constructor otherwise it wont work while deserializing!

Pls Help...

Waiting 4 ur valuable response...

Sunath.

Sunath at 2007-8-31 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 6

You can't add the SqlConnection to the data to be serialized because the SqlConnection class cannot be serialized. You need to exclude the SqlConnection and only serialize the state of your object that can be serialized. ISerializeable is a hook to allow you to control what data in your object gets serialized. In ISerializable.GetObjectData you add the data from your object that can be serialized. Please see http://msdn2.microsoft.com/en-us/library/system.runtime.serialization.iserializable.aspx for more information.

Thanks,
Joel West
MSFTE - SDE in WF runtime and hosting

This posting is provided "AS IS" with no warranties, and confers no rights

JoelWest at 2007-8-31 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...

Software Development for Windows Vista

Site Classified