Welcome to the CodeSmith Community!

Re: Type '' in Assembly '' is not marked as serializable

Template Frameworks

A description has not yet been added to this group.

Type '' in Assembly '' is not marked as serializable

Answered (Verified) This question is answered

In ExistsCommand.vb, in the Public Shared Function Execute (Of ) the following exists:

 

Dim cmd As New ExistsCommand()

cmd.BeforeServer(criteria)

cmd = DataPortal.Execute(cmd)

cmd.AfterServer()

 

When I execute my code, I get the failure defined in the Subject when the cmd = DataPortal.Execute(cmd) line is executed.  I saw where some others were having this on netiers; however I am not using netiers -- or at least if I am I don't know it.  I cannot put a break point in the DataPortal_Execute and the code stop.  It happens every time and it is between the DataPortal.Execute and the DataPortal_Execute

 

Any ideas? 

 

Thanks

  • Post Points: 35
All Replies
  • Hello,

    Can you clean and rebuild. Also are you using Silverlight and or .NET 4.0 support? Can you please paste the full stack trace.

    Thanks

    -Blake Niemyjski

    Blake Niemyjski
    CodeSmith Tools, LLC. Software Development Engineer
    Blog: http://windowscoding.com/blogs/blake/
    .NetTiers team | Visit http://www.nettiers.net

  • I think I know the problem; going to test and prove that in a few minutes.  I believe I have ran into the first issue with my criteria method wrapping a SQLCommand object.  One cannot serialize a command object and since my criteria is a wrapped SQLCommand object, when the DataPortal.Fetch is called on the ExistsCommand class, it tries to serialize the criteria which tries to serialize the SQLCommand and fireworks!

    CSLA is cool, but there are times it makes my life hard!

  • Nevermind, that is not the issue or I couldn't pass the criteria out of the factory to DataPortal.Fetch.  I will try to get the stack trace in here in a minute; dev is disconnected from internet and not networked to this machine; so getting stuff out of dev to share is difficult.

  • I'm trying to put the stack trace, but it doesn't seem to be posting. 

     

    Ah, can't "Post" but can "edit" the stack trace in.

     

    System.Runtime.Serialization.SerializationException {"Type 'System.Data.SqlClient.SqlCommand' in Assembly 'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable."} System.Runtime.Serialization.SerializationException 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, SerializationBinder binder)     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 Csla.Serialization.BinaryFormatterWrapper.Serialize(Stream serializationStream, Object graph)     at Csla.Core.ObjectCloner.Clone(Object obj)     at Csla.CommandBase`1.GetClone()     at Csla.CommandBase`1.System.ICloneable.Clone()     at Csla.DataPortal.Update[T](T obj)     at Csla.DataPortal.Execute[T](T obj)     at Library.Core.ExistsCommand.Execute[T](T criteria) in C:\TFS\Library - Copy\Library.Core\ExistsCommand.vb:line 49     at Library.Core.EndItemInspection.Exists(CustomCriteria`1 criteria) in C:\TFS\Library - Copy\Library.Core\Core\EndItem\Entities\EndItemInspection.Generated.vb:line 286     at Library.Core.EndItemInspectionList.Exists(CustomCriteria`1 criteria) in C:\TFS\Library - Copy\Library.Core\Core\EndItem\Collections\EndItemInspectionList.Generated.vb:line 127     at Library.Core.EndItem.get_EnditemInspections() in C:\TFS\Library - Copy\Library.Core\Core\EndItem\Entities\EndItem.Generated.vb:line 200     at WindowsApplication1.Form1.Button1_Click(Object sender, EventArgs e) in C:\TFS\Library - Copy\WindowsApplication1\Form1.vb:line 11" String

  • Criteria Object we are generating; it isn't necessarily finished, but functional

    '------------------------------------------------------------------------------

    ' <autogenerated>

    ' This code was generated using CodeSmith: v5.3.4, CSLA Templates: v3.0.0.0, CSLA Framework: v4.0.0.

    ' Changes to this file will be lost after each regeneration.

    ' To extend the functionality of this class, please modify the partial class 'Enditem.vb.

    '

    ' Template: Criteria.Generated.cst

    ' Template website: code.google.com/.../codesmith

    ' </autogenerated>

    '------------------------------------------------------------------------------

    Option Explicit On

    Option Strict On

    #Region "Using declarations"

    Imports System

    Imports System.Collections.Generic

    Imports System.Runtime.InteropServices

    Imports System.Data.SqlClient

    Imports Csla

    #End Region

    Namespace Library.Core

    <Serializable()> _

    Public Partial Class CustomCriteria(Of T)

    Inherits Csla.CriteriaBase(Of CustomCriteria(Of T))

    Implements IGeneratedCriteria

    Private _cmd As SqlCommand

    Private _type As System.Type

    Private _stateBag As New Dictionary(Of String, Object)

    Public Property Command() As SqlCommand

    Get

    Return _cmd

    End Get

    Set(ByVal value As SqlCommand)

    _cmd = value

    End Set

    End Property

    ''' <summary>

    ''' Constructor

    ''' </summary>

    ''' <remarks></remarks>

    Public Sub New()

    MyBase.New()

    _cmd =

    New SqlCommand()

    End Sub

    ''' <summary>

    ''' Constructor

    ''' </summary>

    ''' <param name="cmdText">String</param>

    ''' <remarks></remarks>

    Public Sub New(ByVal cmdText As String, ByVal commandType As CommandType)

    MyBase.New()

    _cmd =

    New SqlCommand(cmdText)

    _cmd.CommandType = commandType

    End Sub

    ''' <summary>

    ''' Constructor

    ''' </summary>

    ''' <param name="cmdText">String</param>

    ''' <param name="connection">System.Data.SQLClient.SQLConnection</param>

    ''' <remarks></remarks>

    Public Sub New(ByVal cmdText As String, ByVal connection As SqlConnection, ByVal commandType As CommandType)

    MyBase.New()

    _cmd =

    New SqlCommand(cmdText, connection)

    _cmd.CommandType = commandType

    End Sub

    ''' <summary>

    ''' Constructor

    ''' </summary>

    ''' <param name="cmdText">String</param>

    ''' <param name="connection">System.Data.SQLClient.SQLConnection</param>

    ''' <param name="transaction">System.Data.SQLClient.SqlTransaction</param>

    ''' <remarks></remarks>

    Public Sub New(ByVal cmdText As String, ByVal connection As SqlConnection, ByVal transaction As SqlTransaction, ByVal commandType As CommandType)

    MyBase.New()

    _cmd =

    New SqlCommand(cmdText, connection, transaction)

    _cmd.CommandType = commandType

    End Sub

    ''' <summary>

    ''' Constructor

    ''' </summary>

    ''' <param name="cmd">System.Data.SQLClient.SQLCommand</param>

    ''' <remarks></remarks>

    Public Sub New(ByVal cmd As SqlCommand)

    MyBase.New()

    _cmd = cmd

    End Sub

    Public ReadOnly Property StateBag As System.Collections.Generic.Dictionary(Of String, Object) Implements IGeneratedCriteria.StateBag

    Get

    Return _stateBag

    End Get

    End Property

    Public ReadOnly Property TableFullName As String Implements IGeneratedCriteria.TableFullName

    Get

    Return Nothing

    End Get

    End Property

    Public Sub SetProperty(PropertyName As String, PropertyValue As Object) Implements IGeneratedCriteria.SetProperty

    If Not _stateBag.ContainsKey(PropertyName) Then

    _stateBag.Add(PropertyName, PropertyValue)

    Else

    Throw New Exception("Property already exists in the collection")

    End If

    End Sub

    Public Function GetProperty(ByVal PropertyName As String) As Object Implements IGeneratedCriteria.GetProperty

    If _stateBag.ContainsKey(PropertyName) Then

    Return _stateBag(PropertyName)

    Else

    Throw New Exception("Property not found")

    Return Nothing

    End If

    End Function

    Public Sub RemoveAllProperties() Implements IGeneratedCriteria.RemoveAllProperties

    _stateBag.Clear()

    End Sub

    Public Sub RemoveProperty(PropertyName As String) Implements IGeneratedCriteria.RemoveProperty

    _stateBag.Remove(PropertyName)

    End Sub

    End Class

    End

    Namespace

  • Factory method example

    Public Shared Function GetByEnditemGuid(ByVal enditemGuid As System.Guid) As EnditemInfoList

    Dim criteria As CustomCriteria (Of EnditemInfoList) = New CustomCriteria (Of EnditemInfoList)(GETBYENDITEMGUID_STOREDPROCEDURE, CommandType.StoredProcedure)

    criteria.Command.Parameters.AddWithValue(

    "EnditemGuid", enditemGuid)

    Return DataPortal.Fetch(Of EnditemInfoList)(criteria)

    End Function

  • DataPortal Fetch

    Private Shadows Sub DataPortal_Fetch(ByVal criteria As CustomCriteria (Of EnditemInfoList))

    IsReadOnly =

    false

    Dim canceled As Boolean = False

    OnFetching(criteria, canceled)

    If not canceled Then

    RaiseListChangedEvents =

    False

    Using mgr As ConnectionManager(Of SqlConnection) = ConnectionManager(Of SqlConnection).GetManager(ADOHelper.ConnectionString, False)

    If Not mgr.Connection.State.Equals(ConnectionState.Open) Then mgr.Connection.Open()

    criteria.Command.Connection = mgr.Connection

    Using dr As SafeDataReader = New SafeDataReader(criteria.Command.ExecuteReader())

    While dr.Read()

    Me.Add(New Library.Core.EnditemInfo(dr))

    End While

    End Using

    End Using

    RaiseListChangedEvents =

    True

    OnFetched()

    End If

    IsReadOnly =

    true

    End Sub

    End Class

  • Update: Forgot to mention, not using Silverlight and I am using .Net 4.  <aside>I kind of wish the templates didn't have all the silverlight stuff in it; kind of seems smarter to have silverlight templates and non-silverlight templates.  I've been ripping that out as it clutters my templates and makes it very hard to read. </aside>

    Also, I have found if I comment the "cmd.BeforeServer(criteria)" call out (in the ExistsCommand), that I do not get the serialization error; of course, I cannot execute DataPortal_Fetch either as I don't have a criteria object to execute upon.

  • Hello,

    We are going to be ripping out the Silverlight blocks in the next release Wink, as we have had feedback and agree. Initially it worked great and was what everyone wanted. But as the platform progressed so do we and the feedback has changed to include them by default which before, people hadn't been using all of the Async in the their daily projects...

    I'd recommend changing your Criteria Object a bit to mark the SqlCommand with the non serializable attribute (Serializable(false) I think) or make it private. I would then create a wrapper say a string to hold the string stored procedure name. This would allow you to serialize the data across the wire and recreate it on the other side. You could then look into wiring up to the on serialized and on deserialized events to initialize the SqlCommand. This is just a suggestion Wink

    Blake Niemyjski
    CodeSmith Tools, LLC. Software Development Engineer
    Blog: http://windowscoding.com/blogs/blake/
    .NetTiers team | Visit http://www.nettiers.net

    Suggested by
  • That is awesome.  When is that "next release"?

    As for the criteria object, I cannot mark the Command property <Serializable(False)>_.  It errors saying that it can't be done.  Making it private kind of negates the purpose of it, so I can't do that either.

    As for the string to hold the proc name, there is one, but as it is, it is read-only.  I was going to make it read/write to set it in the factory or some such thing, but as of now, since I can't get through CSLA due to the Serialization Error, it is a minor issue.

    No other suggestions for the Serialize error?  It is somehow related to the BeforeServer call where the criteria is stored in the property as IGeneratedCriteria.  I am a little out of my depth.  Since my version of criteria is working for everything else, there is something going on in the ExistsCommand functionality that is not the same and I don't understand it enough to know how to solve it.

  • Addendum:

    I completely rewrote my criteria object.  There is no reference to System.Data, System.Data.SQLClient, SQLCommand objects or the like and I still get the serialization error.  It has a slightly different stack trace, but it still isn't working.

    I have a simple UI sitting on my business object library I am creating.  It has one UltraWinGrid that takes a child collection as a datasource.  If I bypass the ExistsCommand.Execute code and just "return true" then my grid populates as expected.  However, if I turn on ExistsCommand.Execute, it complains about serialization.  I am going to try to append the stack trace to the bottom of this after I post it since adding it here will not work.

     

      StackTrace "   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, SerializationBinder binder)    
    at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)    
    at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArrayMember(WriteObjectInfo objectInfo, NameInfo arrayElemTypeNameInfo, Object data)    
    at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArray(WriteObjectInfo objectInfo, NameInfo memberNameInfo, WriteObjectInfo memberObjectInfo)    
    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 Csla.Serialization.BinaryFormatterWrapper.Serialize(Stream serializationStream, Object graph)    
    at Csla.Core.ObjectCloner.Clone(Object obj)    
    at Csla.CommandBase`1.GetClone()    
    at Csla.CommandBase`1.System.ICloneable.Clone()    
    at Csla.DataPortal.Update[T](T obj)    
    at Csla.DataPortal.Execute[T](T obj)    
    at Library.Core.ExistsCommand.Execute[T](T criteria) in C:\TFS\Library - Copy\Library.Core\ExistsCommand.vb:line 49    
    at Library.Core.EndItemInspection.Exists(CustomCriteria2`1 criteria) in C:\TFS\Library - Copy\Library.Core\Core\EndItem\Entities\EndItemInspection.Generated.vb:line 286    
    at Library.Core.EndItemInspectionList.Exists(CustomCriteria2`1 criteria) in C:\TFS\Library - Copy\Library.Core\Core\EndItem\Collections\EndItemInspectionList.Generated.vb:line 127    
    at Library.Core.EndItem.get_EnditemInspections() in C:\TFS\Library - Copy\Library.Core\Core\EndItem\Entities\EndItem.Generated.vb:line 200    
    at WindowsApplication1.Form1.Button1_Click(Object sender, EventArgs e) in C:\TFS\Library - Copy\WindowsApplication1\Form1.vb:line 11" String


    I no longer think it is the SQLCommand object from my original criteria but something else I have messed up in editing the templates.  I have created a sample using the templates directly without issue; however it makes an unusable business object for us and the templates have to be modified.  I am confounded as to what is wrong and the error message isn't speaking a language I understand apparently.  :)

    J


  • Hello,

    I would take a look at this thread (pauliom.wordpress.com/.../how-to-ignore-a-fieldproperty-during-serialization). This should solve this error. Serialization errors are always tricky, it's best to always make sure the assemblies on both sides are the same, and then when you get errors like this is to make sure your not serializing those types and or create wrappers around them or create a simple value where you can store the name like you did and then recreate the object when the serialization events are fired on the client.

    Blake Niemyjski
    CodeSmith Tools, LLC. Software Development Engineer
    Blog: http://windowscoding.com/blogs/blake/
    .NetTiers team | Visit http://www.nettiers.net

  • <sigh> I can't get to the link -- dammmmn government!  I will have to look at it tonight when I get home.

    However, I don't see that I need to ignore anything anymore.  I removed the SQLCommand object all together.  I am now just capturing the command text as a string, the commandtype and the _stateBag dictionary.  Not so different than what was already being done and I still get the error; only I don't have a criteria per object -- just one should suffice.  This is happening for some other reason that makes no sense; herhaps I should hit Lohka's site as its probably on his side.

    Gotta tell ya, I am sure tired of frameworks and magic bullets; I could have coded the application by now for all the trouble I've gone through trying to implement frameworks and auto-code generation.

  • Hello,

    Here is the text of the post:

    I’ve hit the problem of serializing a property for the last time. When you create a type that contains a non-serializable type, in my case a CultureInfo class, you cannot automatically serialize the type – you get issues about "does not contain a public constructor", "problem reflecting the type", etc. Unfortunately this isn’t caught at compile time and you have to wait until an attempt is made to serialize the containing object. What to do about it? The more complicated choice is to write some custom serialization code, but really, most of the time I just want to serialize pretty simple objects. So my preference is to mark the class as serializable and let the client choose how to serialize it, i.e. via Binary or Xml "serializers". The framework provides a couple of attributes for this, and this is where the gotcha lives.

     

    The first attribute, [NonSerialized], should be applied to field members and informs the standard serialization methods to ignore the field. The second attribute, [System.Xml.Serialization.XmlIgnore], should be applied to public properties and informs the Xml Serializer to ignore the property. The combination of the two should be enough to stop the field from being serialized regardless of the serializer the client chooses to use.
    Quick pseudo example:

    [NonSerialized],
    private SomeUnFriendlyType myType:

    [System.Xml.Serialization.XmlIgnore]
    public SomeUnFriendlyType MyType()

    Blake Niemyjski
    CodeSmith Tools, LLC. Software Development Engineer
    Blog: http://windowscoding.com/blogs/blake/
    .NetTiers team | Visit http://www.nettiers.net

Page 1 of 2 (17 items) 12