0

I have a VB.NET website project. In one of the pages, during a button click, it is supposed to load a telerik RadGrid with data. It works fine on my local machine. However when I deploy it to pre-production on a server, it throws the following error.

The type initializer for 'Utility' threw an exception

Utility is a Static class and while calling any members of the static class (either public static functions or public static variables) I was receiving this error.

Here is the code snippet:

Partial Class Session
  Inherits System.Web.UI.Page

  Protected Sub RadGrid1_ItemDataBound(sender As Object, e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
   If TypeOf e.Item Is GridDataItem Then
     Dim testString As String = String.Empty
     Dim encryptString As String = String.Empty

     Try
        encryptString = Utility.EncryptString(cellValue.ToString())
        testString = Utility.test

     Catch ex As Exception
        Logger.getInstance().log("Value1" & cellValue.ToString() & "Value2" & testString)
     Finally
     End Try
   End If
End Sub
End Class

In my Utility.vb, this is what I have:

Public Class Utility
   Public Shared test As String = "hello"

   Public Shared Function EncryptString(ByVal strEncrypted As String) As String

     Try
        Dim b As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(strEncrypted)
        Dim encryptedConnectionString As String = Convert.ToBase64String(b)
        Return encryptedConnectionString
    Catch ex As Exception
        Logger.getInstance().log("Error occurred in Utility.vb while executing EncryptString method. \nSOURCE: " & ex.Source.ToString() & "\nMESSAGE: " & ex.Message.ToString() & "\nTARGETSITE: " & ex.TargetSite.ToString() & "\nSTACKTRACE: " & ex.StackTrace.ToString())
        Logger.getInstance().log("\nstrEncrypted: " & strEncrypted)

    Finally
    End Try
 End Function
End Class

From my logging statements, I realized that the error is happening right when I call the Utility class because none of the logging statements I added into the EncryptString() function are being shown in my logs.

I tried commenting out the code for EncryptString() function and added just a public static string variable called test in my Utility class, and tried accessing it from my code from Session.aspx.vb. That string value was not being returned as well.

By the way, all of my code is in the same website project. The error is driving me crazy for past 2 days. Like I mentioned, it works fine on my local machine, but fails only on the pre-prod server. The code was written using an older framework (3.5 I believe) and then we upgraded to 4.6.2 and migrating to new servers. We are facing this issue during the migration process on the new server.

Ron Beyer
  • 10,378
  • 1
  • 15
  • 33
Test_User
  • 161
  • 1
  • 5
  • 14
  • Possible duplicate of [The type initializer for 'MyClass' threw an exception](https://stackoverflow.com/q/4398334/11683) – GSerg Apr 12 '18 at 19:24
  • Sounds like you need to clean solution and rebuild. NOW - `Utility is a Static class` - there is no such thing as static class in vb – T.S. Apr 12 '18 at 19:26
  • Possible duplicate of [How to debug "The type initializer for 'my class' threw an exception"](https://stackoverflow.com/questions/12305648/how-to-debug-the-type-initializer-for-my-class-threw-an-exception) – GSerg Apr 12 '18 at 21:47

3 Answers3

1

OK, so I figured out the issue. At the beginning of the Utility.vb class there were some unsupported cryptographic algorithm variable declarations.

Private Shared DES As New TripleDESCryptoServiceProvider
Private Shared MD5 As New MD5CryptoServiceProvider

FIPS compliance is turned on, on the server and we already knew these algorithms were non-compliant with the latest .net frameworks. But I didn't realize even just these declarations would throw a misleading error, when they were not really being used in my function calls.

When I commented out those parts of the code, it started working fine.

Test_User
  • 161
  • 1
  • 5
  • 14
0

Check the InnerException property of the exception. Any time a class initializer fails the original exception should be set as the InnerException property of the unhandled exception.

jColeson
  • 901
  • 1
  • 14
  • 23
-1

If you want a truly static class, you need to make it a module, in VB.Net.