Service start issue on Computer restart

G

Guest

I have a Windows Service developed in VB.NET that attempts to connect to a
database as soon as the service starts. I have no problem when I start the
service manually - but when I restart the computer that hosts the service, I
get a Null Reference Exception when I try to connect to the database (I have
set the service to start automatically). Any ideas why this is happening?

Thanks in advance.

Ajay Mirmira
 
C

Carlos J. Quintero [.NET MVP]

Is the NullReferenceException thrown in your code (a bug that you must fix)
or inside the .NET Framework?

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com
 
C

Chris Dunaway

It is possible that your service is being started before SQL server is.
You can set you service to depend on other services being started
first. You might look into that.
 
C

Chris Calzaretta

you need to verify that you have a network connection.
If the service is starting before the network is up and running you will get
an execption error.

if you can not figure it out... a hoky way around it is
..
when the service is started.. put a timer that pauses it for 5 mins.
Private tmr As New System.Timers.Timer(5000)

Private tmrstart As DateTime

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

tmrstart = Now

AddHandler tmr.Elapsed, AddressOf tmr_tick

tmr.Start()

End Sub

Private Sub tmr_tick(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs)

If (DateDiff(DateInterval.Minute, Now, DateAdd(DateInterval.Minute, 5,
tmrstart), FirstDayOfWeek.Sunday) <= 0) Then

MsgBox("5 mins up")

tmr.Stop()

'you need to put your function call here

End If

End Sub



Like i said it is hoky as hell but it will get you around your problem until
you can figure it out.



It seems really strange that your getting an execption error. It has to be
that your calling something that is not there yet.


If you were calling database and it has not been started yet you would be
getting an unable to connect so that is not the issues.

It seems to be happening before you get to that issue



Post you service startup code. That might help



Chris
 
C

Chris Calzaretta

Append to last post... You should error out on cn.open.


Does not matter if sql is started or not that would not cause an execption
unless he is trying to fill a an adapter.

but then you have the problem that you will error out on connection open if
you can not connect.

So i belive that it is happening when he is trying to create the ...... Umm
hard to say
could be oledb object for framework is not built yet.

What I do know.. Is that your trying to use an object that you can not use
yet
 
C

Chris Calzaretta

One more thing to... about the code i posted
Instead of waiting for 5 min. You can try to hit the database. so you can
use the below if you wish too


Private tmr As New System.Timers.Timer(5000)

Private tmrstart As DateTime

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

tmrstart = Now

AddHandler tmr.Elapsed, AddressOf tmr_tick

tmr.Start()

End Sub

Private Sub tmr_tick(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs)

''' you can put code here to see if you can create objects 'and not go on
until you have all your object creates
'so as follows
if((obj1 is not nothing) and (obj2 is not nothing))then
tmr.Stop()
call you function
end if
End Sub

that will allow you to check to see if your objects are created before you
go on.. You could also put a timer there to see if objects are not created
in 5 mins to shut down the service and send an alert email to the
administrator of the software.
 
G

Guest

Thanks Carlos. I am posting my start up code.

On the service Startup, I have this code:

Protected Overrides Sub OnStart(ByVal args() As String)
Dim oEventLog As New EventLog("Application", ".", "MyService")
Try
moCluster = New Cluster("ApplicationName", NodeTimeOut)
moCluster.Connect()
Catch ae As ApplicationException
oEventLog.WriteEntry(String.Concat("Source: ", ae.Source, " Message: ",
ae.Message), EventLogEntryType.Error)
End Try

End Sub

The Cluster object's connect method has the following code:

Public Sub Connect()

Dim sDatabase As String
Dim sServer As String
Dim sUser As String
Dim sPassword As String
Dim bTrusted As Boolean
Dim oDB As sqlDB
Dim sSource As String


Try
sSource = "Attempting to connect to registry"
'connect to registry and get the information
GetDataFromRegistry(sServer, sDatabase, sUser, sPassword,
bTrusted)

sSource = "Attempting to create database object"
'create the db class
If bTrusted Then
oDB = New sqlDB(sServer, sDatabase)
Else
oDB = New sqlDB(sServer, sDatabase, sUser, sPassword)
End If

sSource = "Attempting to connect to database"

'attempt to connect to the database
If Not oDB.Open Then
WriteEvent("Connection to Database Failed",
EventLogEntryType.Error)
Throw New ApplicationError("Connect", "Connection to
Database Failed")
Else
msConnectionString = oDB.ConnectionString
End If

'close database connection
sSource = "Attempting to close database connection"
oDB.Close()
oDB = Nothing

Catch ex As Exception
'close connection if it is open
If Not (oDB Is Nothing) Then
If oDB.State <> ConnectionState.Closed Then
oDB.Close()
End If
oDB = Nothing
End If
'error - raise back to caller
Throw New ApplicationError(String.Concat(sSource, "::",
ex.Source), "Message::", ex.Message)
End Try

End Sub

The error I see in the event viewer is as follows:
System.ApplicationException: Source: Attempting to connect to
database::BDLBSVC Message: Object reference not set to an instance of an
object. at MyService.BDLBSVC.OnStart(String[] args)
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object
state)


Since I do not see the "Attempting to close database connection" in the
error message, I beleive that the error occurs when an attempt to open the
connection is being made. The sqlDB is custom class that wraps common
database functions.

The NEW method of the sqlDB class creates the connection string and the Open
method opens a connection to the database. For the sake of completeness, I
will also post the code for the sqlDB's open method.

Public Overrides Function Open() As Boolean

Dim sqlConnection As New SqlConnection
Dim bReturnValue As Boolean = False

'set connection string
sqlConnection.ConnectionString = msConnectionString

'open connection to the database
Try
sqlConnection.Open()
bReturnValue = True
Catch ex As Exception
bReturnValue = False
End Try

'set the public member to the connection
Connection = CType(sqlConnection, Object)

'return true or false depending on the success of the connection
Return bReturnValue

End Function


Any insight into my problem is appreciated. I have also noticed that I do
not have this issue on the Windows XP box (my development environment) - the
service starts automatically on a reboot - but the problem occurs on a
Windows 2000 server and Windows 2003 (both development servers) machine.

Again, I am thankful for your help.

Regards,
Ajay Mirmira
 
G

Guest

Hi Chris,
I don't think that SQL Server service is an issue. The box that I am testing
the service on does not have SQL Server installed. It only has the SQL Server
connectivity tools.

Thanks anyways.

Ajay Mirmira
 
G

Guest

Hi Chris,
Instead of waiting for 5 minutes, can I not list the network service (and by
this I mean the RPC service) as a dependency to my service?

Thanks in advance.

Ajay Mirmira
 
G

Guest

Carlos,
If this is a bug in my code, should I not see the error all the time? How is
the code execution different when I start the service manually and when the
service is started on a machine reboot.

Ajay Mirmira
 
G

Guest

Thanks you all for the support. I finally found the issue. Chris Dunaway was
right after all. The service depends on SQL Server to be started - if the SQL
Server is not started, then the error occurs. By some freak coincidence, SQL
Server was turned off when I was performing the reboot test - and it was
turned on when I started it manually.

I caught this condition when the service failed to start manually. Anyways,
I have added better error handling in my code - now I get the error "SQL
Server does not exist or access is denied". This is a more acceptable error
and allows me to troubleshoot.

Once again, thanks to you all.

Ajay Mirmira
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top