Type initializer threw an exception.

Type initializer threw an exception.

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


nhwilly posted on Monday, March 24, 2008

New to CSLA, trying to digest it all. 

Bought the first book, the second book and the online book; been reading them for years and finally got around to doing a project with CSLA.  To bad they don't come with brain cells.  So, sorry for asking this question.  I just *know* this is a cockpit error, but I am stumped.

Any help would be *greatly* appreciated.  TIA

I've created a BO called a NewsItem (that looks a lot like a CSLA Project).

I've followed the example code to the letter (as far as I can tell).  The first time I try to create an instance of NewsItem, it chokes with this error:

System.TypeInitializationException: {"The type initializer for 'OORG.Library.NewsItem' threw an exception."}

{"String value can not be converted to a date"}

Here's the code that calls it:

Dim n As OORG.Library.NewsItem = OORG.Library.NewsItem.NewNewsItem

n.Subject = "subect text"

n.PostedBy = "posted by willy"

n.StartDate = CStr(Today)

n.EndDate = "12/31/2009"

n.Text = "This is text for the message"

Here's the property with the date:

Private Shared StartDateProperty As PropertyInfo(Of SmartDate) = RegisterProperty(Of SmartDate)(GetType(NewsItem), New PropertyInfo(Of SmartDate)("StartDate", "Start date", SmartDate.EmptyValue.MinDate))

Public Property StartDate() As String

Get

Return GetProperty(Of SmartDate)(StartDateProperty)

End Get

Set(ByVal value As String)

SetProperty(Of SmartDate)(StartDateProperty, value)

End Set

End Property

Here's the stack trace:

at Csla.SmartDate.StringToDate(String value, EmptyValue emptyValue) in C:\Documents and Settings\billn\My Documents\Downloaded Products\CSLA\CSLA 3.5 beta 2\cslavb\Csla\SmartDate.vb:line 622 

at Csla.SmartDate.set_Text(String value) in C:\Documents and Settings\billn\My Documents\Downloaded Products\CSLA\CSLA 3.5 beta 2\cslavb\Csla\SmartDate.vb:line 301 

at Csla.SmartDate..ctor(String value) in C:\Documents and Settings\billn\My Documents\Downloaded Products\CSLA\CSLA 3.5 beta 2\cslavb\Csla\SmartDate.vb:line 195 

at Csla.SmartDate.op_Explicit(String dateValue) in C:\Documents and Settings\billn\My Documents\Downloaded Products\CSLA\CSLA 3.5 beta 2\cslavb\Csla\SmartDate.vb:line 927   

at OORG.Library.NewsItem..cctor() in C:\Documents and Settings\billn\My Documents\Visual Studio 2008\Projects\OORG2\OORG.Library\NewsItem.vb:line 55

 

nhwilly replied on Monday, March 24, 2008

OK, I pulled the Project class directly into my project, commented out the code that calls the database and other libraries and it seems to work fine.

Curiouser and curiouser.

 

nhwilly replied on Monday, March 24, 2008

Please ignore this post. 

I am a total idiot.

Things are working now...

JoeFallon1 replied on Tuesday, March 25, 2008

LOL.

Ok - so what was the *real* issue?

 

nhwilly replied on Tuesday, March 25, 2008

Besides the room temperature IQ?

Well, I've never really used multiple projects in the same solution, but I thought the way that Rocky structured it was pretty organized and I wanted to adopt it.

That being the case, in the past, *all* modifications were always taken into account when I ran a debug session. 

Unfortunately, I didn't realize that modifications in other projects in the same solution don't automatically get rebuilt.  By that I mean that I don't know squat about what I'm doing and should get back into management. Embarrassed [:$]

So, the DLL's from the BO Library fell out of sync with the corresponding changes in the application.  It just kept complaining about my dates.

Just like me in college.

nermin replied on Tuesday, March 25, 2008

Well the first thing to check is if your application is referencing the BO library project and not the DLL itself.

 

Right click on References portion of your application project and see what you get.

 

Once you reference the BO library project, compiling the Application project will force compiling of the BO library.

 

Hope that helps.

 

Nermin

 

From: nhwilly [mailto:cslanet@lhotka.net]
Sent: Tuesday, March 25, 2008 4:22 PM
To: Nermin Dibek
Subject: Re: [CSLA .NET] Type initializer threw an exception.

 

Besides the room temperature IQ?

Well, I've never really used multiple projects in the same solution, but I thought the way that Rocky structured it was pretty organized and I wanted to adopt it.

That being the case, in the past, *all* modifications were always taken into account when I ran a debug session. 

Unfortunately, I didn't realize that modifications in other projects in the same solution don't automatically get rebuilt.  By that I mean that I don't know squat about what I'm doing and should get back into management. Embarrassed <img src=">

So, the DLL's from the BO Library fell out of sync with the corresponding changes in the application.  It just kept complaining about my dates.

Just like me in college.



nhwilly replied on Tuesday, March 25, 2008

Well...

I selected Show All Files, then expanded the tree for the References branch, then right clicked over the library name and I can't find any references to a DLL.

I went to the project property page and selected the references tab on the left, then went to Add reference..., then made sure to select the "Project" tab, then selected the Library Project and it said there was already a reference to it and it couldn't be added again.

I probably did add a direct reference to the DLL when I did this, thinking I had to.  I'll bet I have to remove the reference to the Library DLL and re-add it, but do it as a Project and not a DLL.

BTW, thanks a lot, that will really help me avoid this dumb*ss error in the future.

p.s. Just tested my first BO to SQL storage and it worked great.

 

nhwilly replied on Tuesday, March 25, 2008

You mean a Reference Path to the project?

AaronH replied on Monday, June 21, 2010

I'm also getting this error.  When I run the debugger in studio, no issues.  However, when I deploy the app (regardless of Debug or Release mode), the callback handler event args ( DataPortalResult<T>.Error ) contains the Type Initializer exception.

I'm using CSLA 4.0 for SL, and this only happens with ReadOnly objects.  Here is one of the classes that is failing:

    [Serializable]
    public class CompanyInfo : ReadOnlyBase<CompanyInfo>
    {
        #region Properties

        private static PropertyInfo<int> CompanyIDProperty = RegisterProperty<int>(c => c.CompanyID);
        public int CompanyID
        {
            get { return GetProperty(CompanyIDProperty); }
            internal set { LoadProperty(CompanyIDProperty, value); }
        }

        private static PropertyInfo<string> NameProperty = RegisterProperty<string>(c => c.Name);
        public string Name
        {
            get { return GetProperty(NameProperty); }
            internal set { LoadProperty(NameProperty, value); }
        }

        #endregion

    }
 
Any thoughts?

RockfordLhotka replied on Monday, June 21, 2010

Make your PropertyInfo<T> fields public and see if that helps.

AaronH replied on Tuesday, June 22, 2010

That worked.  Strange, I only remember the scope declarations being an issue in an inheritance scenario, when creating an abstract class based off of BusinessBase<T>, and creating a concrete class based off the abstract.

In any case, I still find it strange that when debugging in VS10, the issue does not exist.  I'm assuming that this is a bug in CSLA 4.0?

If you have any thoughts about this, I'd love to hear them.  I'm not a huge fan of exposing private implementation.

Thanks!

RockfordLhotka replied on Tuesday, June 22, 2010

It appears to be something Microsoft changed with the way static fields are initialized in Silverlight 4.

You might be able to use the _forceInit trick (which is discussed in the book and on this forum) to address the problem - but not in release mode, because the compiler is now smart enough to optimize away the, essentially unused, _forceInit code...

My recommendation starting with CSLA 4 is to make the PropertyInfo<T> fields public. This matches Microsoft's recommendations around dependency properties, and so provides some consistency. CSLA 4 has been changed so the important fields of PropertyInfo<T> aren't easily changed (or seen) by a UI developer or other consumer, specifically to make this a good practice.

CSLA includes code to force initialization of the static fields. But due to SL reflection limitations the fields must be public in SL for this to work.

AaronH replied on Tuesday, June 22, 2010

Interesting.  Ok, good to know, thanks!

Copyright (c) Marimer LLC