Type Initializer for "MyClass" threw an exception.

Type Initializer for "MyClass" threw an exception.

Old forum URL: forums.lhotka.net/forums/t/9892.aspx


AKaplan posted on Monday, December 20, 2010

So I've been working on this idea of consolidating my code a bit more and tightening the inheritance. What I've done is taken my "Factory Methods"  and moved them up in the hierarchy so all my objects can use them. This way I don't have to have so much redundant code.  The RuleBusinessListBase is working fine until after it grabs the database records and then crashes on populating the RuleBusinessBase object. It throws an error of "The type initializer for "CK.Library.Albumtrack" threw an exception". I'm just using AlbumTrack as an example of the object. This is now happening on all objects and I'm wondering what could be the problem. I've googled already possibilities and came up with the constructor is missing. But unfortunately I have a constructor in all of my objects. I also found it a bit weird that I have to comply with CLSCompiant(False) with the ID and Name encapsulating properties as well in the RuleBusinessBase class. Let me know if you come across any ideas.

<Serializable()> _
    Public MustInherit Class RuleBusinessListBase(Of T As RuleBusinessListBase(Of T, C), C As {RuleBusinessBase(Of C), IEditableBusinessObject})
    Inherits Csla.BusinessListBase(Of T, C)
    ''' <summary>
    ''' Provides a public method to tell whether any of the items in the collection has any broken validation rules.
    ''' </summary>
    Public ReadOnly Property IsBroken() As Boolean
        Get
            Dim foundBroken As Boolean = False
            Dim i As Integer = 0
            Do While i < Me.Count
                Dim item As RuleBusinessBase(Of C) = Items(i)
                If item.BrokenRules.Count > 0 Then
                    foundBroken = True
                End If
                i += 1
            Loop
            Return foundBroken
        End Get
    End Property
#Region "Factory Methods"
    Public Sub New()
    End Sub
    Public Shared Function NewList() As T
        Return DataPortal.Create(Of T)()
    End Function
    Public Shared Function NewList(ByVal ID As Nullable(Of Guid)) As T
        Return DataPortal.Create(Of T)(New SingleCriteria(Of T, Nullable(Of Guid))(ID))
    End Function
    Public Shared Function GetList() As T
        Return DataPortal.Fetch(Of T)()
    End Function
    Public Shared Function GetList(ByVal id As Nullable(Of Guid)) As T
        Return DataPortal.Fetch(Of T)(New SingleCriteria(Of T, Nullable(Of Guid))(id))
    End Function
    Public Shared Function GetList(ByVal name As String) As T
        Return DataPortal.Fetch(Of T)(New SingleCriteria(Of T, String)(name))
    End Function
    Public Shared Function GetList(ByVal _date As DateTime) As T
        Return DataPortal.Fetch(Of T)(New SingleCriteria(Of T, DateTime)(_date))
    End Function
    Public Shared Function GetList(ByVal args As Object()) As T
        Return DataPortal.Fetch(Of T)(New SingleCriteria(Of T, Object())(args))
    End Function
End Class
<Serializable()> _
Public MustInherit Class RuleBusinessBase(Of T As {RuleBusinessBase(Of T), Csla.Core.IEditableBusinessObject})
    Inherits BusinessBase(Of T)
#Region "Constructor"
    Public Sub New()
    End Sub
    Public Sub New(ByVal ID As Guid)
        SetProperty(Of Nullable(Of Guid))(_ID, ID)
    End Sub
    Public Sub New(ByVal ID As Guid, ByVal Name As String)
        SetProperty(Of Nullable(Of Guid))(_ID, ID)
        SetProperty(Of String)(_Name, Name)
    End Sub
#End Region
#Region "Properties"
    <CLSCompliant(False)> _
    Protected Friend Shared _ID As PropertyInfo(Of Nullable(Of Guid)) = RegisterProperty(Of Nullable(Of Guid))(GetType(T), New PropertyInfo(Of Nullable(Of Guid))("ID", "ID", Guid.NewGuid))
    
<CLSCompliant(False)> _
    Protected Friend Shared _Name As PropertyInfo(Of String) = RegisterProperty(Of String)(GetType(T), New PropertyInfo(Of String)("Name", "Name", String.Empty))
    <System.ComponentModel.DataObjectField(True, True)> _
   
 Public ReadOnly Property ID() As Nullable(Of Guid)
        Get
            Return GetProperty(Of Nullable(Of Guid))(_ID)
        End Get
    End Property
   
 Public Property Name() As String
        Get
            Return GetProperty(Of String)(_Name)
        End Get
        Set(ByVal value As String)
            SetProperty(Of String)(_Name, value)
        End Set
    End Property
#End Region
#Region "Factory Methods"
    Public Shared Function NewItem() As T
        Return DataPortal.Create(Of T)()
    End Function
    Public Shared Function NewItem(ByVal ID As Nullable(Of Guid)) As T
        Return DataPortal.Create(Of T)(New SingleCriteria(Of T, Nullable(Of Guid))(ID))
    End Function
    Public Shared Function NewItem(ByVal Name As String) As T
        Return DataPortal.Create(Of T)(New SingleCriteria(Of T, String)(Name))
    End Function
    Public Shared Function NewItem(ByVal ID As Integer) As T
        Return DataPortal.Create(Of T)(New SingleCriteria(Of T, Integer)(ID))
    End Function
    Public Shared Function GetItem() As T
        Return DataPortal.Fetch(Of T)()
    End Function
    Public Shared Function GetItem(ByVal ID As Nullable(Of Guid)) As T
        Return DataPortal.Fetch(Of T)(New SingleCriteria(Of T, Nullable(Of Guid))(ID))
    End Function
    Public Shared Function GetItem(ByVal ID As Integer) As T
        Return DataPortal.Fetch(Of T)(New SingleCriteria(Of T, Integer)(ID))
    End Function
    Public Shared Function GetItem(ByVal ID As String) As T
        Return DataPortal.Fetch(Of T)(New SingleCriteria(Of T, String)(ID))
    End Function
    
End Class
Example Class
<Serializable()> _
Public Class AlbumTrackList
    Inherits RuleBusinessListBase(Of AlbumTrackList, AlbumTrack)
#Region "Factory Methods"
    Friend Overloads Shared Function GetList(ByVal data As CK.Linq.AlbumTrack()) As AlbumTrackList
        Return DataPortal.FetchChild(Of AlbumTrackList)(data)
    End Function
    Public Sub New()
    End Sub
#End Region
#Region "Data Access"
#Region "Fetch"
    <Transactional(TransactionalTypes.Manual)> _
    Protected Overloads Sub DataPortal_Fetch()
        Using ctx = ContextManager(Of CKDBDataContext).GetManager(Database.CreativeKnightConnection)
            For Each value In (From p In ctx.DataContext.AlbumTracks Order By p.Title Ascending)
The next line crashes here with an error of "The type initializer for "AlbumTrack" threw an exception".
                Add(AlbumTrack.GetItem(value))
            Next
        End Using
    End Sub
#End Region ' Data Access
End Class
Example Class
<Serializable()> _
Public Class AlbumTrack
    Inherits RuleBusinessBase(Of AlbumTrack)
 Friend Overloads Shared Function GetItem(ByVal data As CK.Linq.AlbumTrack) As AlbumTrack
        Return DataPortal.FetchChild(Of AlbumTrack)(data)
    End Function
   Private Sub Child_Fetch(ByVal data As CK.Linq.AlbumTrack)
        With data
            LoadProperty(Of Nullable(Of Guid))(_ID, .ID)
            LoadProperty(Of Nullable(Of Guid))(_ProductID, .ProductID)
            LoadPropertyConvert(Of SmartInt32, Integer)(_RunningOrder, .RunningOrder)
            LoadProperty(Of String)(_Title, .Title)
            LoadProperty(Of String)(_Duration, .Duration)
            LoadPropertyConvert(Of SmartInt32, Integer)(_Rating, .Rating)
            LoadProperty(Of String)(_attachmentFileName, .FileName)
        End With
    End Sub
End Class

 

mspaourh replied on Monday, December 20, 2010

Can you post more information about the exception (stacktrace, innerException, etc?)

 

AKaplan replied on Monday, December 20, 2010

I hope this helps.

Exception Details: System.NotSupportedException: Invalid operation - create not allowed

Stack Trace: 

[NotSupportedException: Invalid operation - create not allowed]
   Csla.BusinessListBase`2.DataPortal_Create() in D:\cslavb 3.6.3\Csla\BusinessListBase.vb:1161
   dm(Object , Object[] ) +55
   Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in D:\cslavb 3.6.3\Csla\Reflection\MethodCaller.vb:272

[CallMethodException: DataPortal_Create method call failed]
   Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in D:\cslavb 3.6.3\Csla\Reflection\MethodCaller.vb:274
   Csla.Reflection.MethodCaller.CallMethod(Object obj, String method, Object[] parameters) in D:\cslavb 3.6.3\Csla\Reflection\MethodCaller.vb:201
   Csla.Reflection.LateBoundObject.CallMethod(String method) in D:\cslavb 3.6.3\Csla\Reflection\LateBoundObject.vb:75
   Csla.Server.SimpleDataPortal.Create(Type objectType, Object criteria, DataPortalContext context) in D:\cslavb 3.6.3\Csla\Server\SimpleDataPortal.vb:47

[DataPortalException: DataPortal.Create failed (System.NotSupportedException: Invalid operation - create not allowed
   at Csla.BusinessListBase`2.DataPortal_Create() in D:\cslavb 3.6.3\Csla\BusinessListBase.vb:line 1161
   at dm(Object , Object[] )
   at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in D:\cslavb 3.6.3\Csla\Reflection\MethodCaller.vb:line 272)]
   Csla.DataPortal.Create(Type objectType, Object criteria) in D:\cslavb 3.6.3\Csla\DataPortal.vb:145
   Csla.DataPortal.Create() in D:\cslavb 3.6.3\Csla\DataPortal.vb:78
   Csla.RuleBusinessListBase`2.NewList() in D:\cslavb 3.6.3\Csla\Core\RuleBusinessListBase.vb:102
   CK.Library.JobCategory..cctor() in D:\CreativeKnights\CK.Library\User\Job.vb:121

[TypeInitializationException: The type initializer for 'CK.Library.JobCategory' threw an exception.]
   CK.Library.JobCategory.GetChildItem(JobCategory data) in D:\CreativeKnights\CK.Library\User\Job.vb:270
   CK.Library.JobCategoryList.DataPortal_Fetch() in D:\CreativeKnights\CK.Library\User\Job.vb:68
   dm(Object , Object[] ) +54
   Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in D:\cslavb 3.6.3\Csla\Reflection\MethodCaller.vb:272

[CallMethodException: DataPortal_Fetch method call failed]
   Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters) in D:\cslavb 3.6.3\Csla\Reflection\MethodCaller.vb:274
   Csla.Reflection.MethodCaller.CallMethod(Object obj, String method, Object[] parameters) in D:\cslavb 3.6.3\Csla\Reflection\MethodCaller.vb:201
   Csla.Reflection.LateBoundObject.CallMethod(String method) in D:\cslavb 3.6.3\Csla\Reflection\LateBoundObject.vb:75
   Csla.Server.SimpleDataPortal.Fetch(Type objectType, Object criteria, DataPortalContext context) in D:\cslavb 3.6.3\Csla\Server\SimpleDataPortal.vb:109

[DataPortalException: DataPortal.Fetch failed (The type initializer for 'CK.Library.JobCategory' threw an exception.)]
   Csla.DataPortal.Fetch(Type objectType, Object criteria) in D:\cslavb 3.6.3\Csla\DataPortal.vb:255
   Csla.DataPortal.Fetch() in D:\cslavb 3.6.3\Csla\DataPortal.vb:179
   Csla.RuleBusinessListBase`2.GetList() in D:\cslavb 3.6.3\Csla\Core\RuleBusinessListBase.vb:108
   Page_Commerce_Types_Events.BindCategory() in D:\CreativeKnights\Web\Page\Commerce\Types\Events.aspx.vb:18
   Page_Commerce_Types_Events.Page_Load(Object sender, EventArgs e) in D:\CreativeKnights\Web\Page\Commerce\Types\Events.aspx.vb:9
   System.EventHandler.Invoke(Object sender, EventArgs e) +0
   System.Web.UI.Control.OnLoad(EventArgs e) +132
   System.Web.UI.Control.LoadRecursive() +11032399
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2428

AKaplan replied on Monday, December 20, 2010

I fixed it.. Apparently I was missing 3 methods in the RuleBusinessListBase and RuleBusinessbase. It was ...

which meant overloading all over the other methods

 

#Region "Data Access Create"

    <RunLocal()> _

    Protected Friend Overloads Sub DataPortal_Create()

    End Sub

    <RunLocal()> _

    Protected Friend Overloads Sub DataPortal_Create(ByVal criteria As SingleCriteria(Of C, Nullable(Of Guid)))

    End Sub

#End Region

#Region "Child Data Access"

    Protected Friend Sub Child_Fetch()

   End Sub

#End Region

RockfordLhotka replied on Monday, December 20, 2010

The exception seems reasonably clear - you are trying to call DataPortal.Create, and it in turn is calling DataPortal_Create, which isn't implemented.

In 3.6 the default DataPortal_Create implementation throws this exception. In CSLA 4 this no longer throws, it "just works" - which still might or might not be what you want :)

Copyright (c) Marimer LLC