0

I have a .Net 4.6 web application that has a chronic yet intermittent problem where an error occurs, always at the same spot, which crashes IIS.

The web app is used everyday, but this error doesn't happen everyday. But when it does error out and crash the website, it's always on the same line.

Here's the error:

[NullReferenceException: Object reference not set to an instance of an object.] SecureUserCheck..ctor(Dataset secureData) in e:\gameServer\SecureUserCheck.vb:160

Here is how "ds" is initialized:

Dim ds As Data.DataSet = Session("secureData")

    If IsNothing(ds) Then
        ds = getSecureDS(gameID)
        Session("secureData") = ds
    End If

Here is SecureUserCheck.vb with line 160 below:

inhSec = New SecureUserCheck(ds)

    Public Sub New(ByVal secureData As DataSet)

        If secureData Is Nothing Then
            Throw New System.ArgumentException("secureData in empty.")
        Else
            startTime = DateTime.Now
            Try
                myGameInfo = secureData.Tables("GameInfo")   // LINE 160
            Catch ex As Exception
                Throw New System.ArgumentException("The secureData must contain [GameInfo] table.")
            End Try
            With myGameInfoRows(0)
            ...fill variables
           End With

I tried wrapping it in a Try Catch block, but that never seems to be thrown. The error will still pop up and crash IIS.

Is there anything I can do to check and make sure the datasets and datatables exist, or not null so that IIS doesn't crash?

Thanks!

SkyeBoniwell
  • 6,345
  • 12
  • 81
  • 185
  • @AndrewMorton thanks for catching that. I wrote the code in by hand instead of copying and pasting from the project and mistyped. I fixed it, thanks! – SkyeBoniwell Nov 14 '18 at 13:41
  • 1
    Where is `secureData` initialised? Could it be that when the [application pool is recycled](https://stackoverflow.com/a/34545072/1115360), it gets lost? Can you reinitialise it if it is Nothing? – Andrew Morton Nov 14 '18 at 15:50
  • @AndrewMorton I have updated the code to show how it's initialized. Thanks! – SkyeBoniwell Nov 14 '18 at 18:54

1 Answers1

1

First, you should use Option Strict On. It will help you get all the types matching up correctly, which leads to fewer errors and faster code.

Now, onto the main issue. If you can do something useful about a problem encountered while the program is running, you should. In this case, you can re-initialise the data if it has been lost:

Public Sub New()
    Dim ds As Data.DataSet

    If Session("secureData") Is Nothing Then
        ds = getSecureDS(gameID)
        Session("secureData") = ds
    Else
        ds = DirectCast(Session("secureData"), Data.DataSet)
    End If

    If Not ds.Tables.Contains("GameInfo") Then
        Throw New System.ArgumentException("The secureData must contain [GameInfo] table.")
    End If

    ' more code...

End Sub

Please also notice the cleaner way of checking if a particular table exists, although I suggest it properly belongs in the getSecureDS method.

If the dataset applies to all users of the site, you should consider using System.Runtime.Caching instead of session state.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • Thank you. I do have one dataset that applies to all users. When I populate that, I do use Page.Application.Lock(), populate the dataset, then Page.Application.Unlock(). Maybe that's a problem too? – SkyeBoniwell Nov 15 '18 at 17:03
  • Andrew, do you think adding the check for the table would help with the app causing IIS to crash? Thanks! – SkyeBoniwell Nov 15 '18 at 17:16
  • 1
    @SkyeBoniwell If you had the dataset cached once for all users, then you could (call a method to) load it in the `Application_Start` event handler. I don't know if using `Application.Lock()` could cause a problem, I would not *expect* it to. Do have a careful read of the System.Runtime.Caching documentation if you use it. – Andrew Morton Nov 15 '18 at 18:26
  • 1
    @SkyeBoniwell I only put in the check for the table because it was in the code sample in the question. If there is no way the table could disappear from the dataset, then it doesn't really need the check. – Andrew Morton Nov 15 '18 at 18:28