-
Windows Service Error
This code is in a timer in a windows service. The service installs successfuly and starts. I tested the code on a web form and it worked. Now when it runs in the service it errors out. From what I can tell it errors out on "Cnn.Open()". Other than that I don't know. The app settings are correct. the connection string is the used on a web app and works, so I don't see why it wouldn't work here.
These are the imported namespaces:
1. Imports System.ServiceProcess
2. Imports System.Data.SqlClient
3. Imports System.Configuration
4. Imports System.Web.Mail.SmtpMail
5. Imports System.DirectoryServices
Thanks for your assistance
Code:
Private Sub Timer_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer.Elapsed
Dim Cnn As New SqlConnection(ConfigurationSettings.AppSettings("Cnn"))
Dim cmdCheck As New SqlCommand()
Dim rdrCheck As SqlDataReader
Dim DirEntry As New DirectoryEntry(ConfigurationSettings.AppSettings("ad"), ConfigurationSettings.AppSettings("admin"), ConfigurationSettings.AppSettings("pw"))
Dim GroupSearch As New DirectorySearcher(DirEntry)
Dim EmailSearch As New DirectorySearcher(DirEntry)
Dim GroupResult, EmailResult As SearchResult
Dim EmailList As New ArrayList()
Dim X As Integer
Dim BodyText As String
Try
SmtpServer = "W2KExchange" 'specify email server
EmailSearch.PropertiesToLoad.Add("Mail") 'select email address for user search
GroupSearch.PropertiesToLoad.Add("member") 'select users for the group search
GroupSearch.Filter = ("(CN=Satellite Email Notification)")
GroupResult = GroupSearch.FindOne
For X = 0 To GroupResult.Properties("member").Count - 1 'select each users email address and enter it into an array
EmailSearch.Filter = ("(" & Left(GroupResult.Properties("member").Item(X), InStr(GroupResult.Properties("member").Item(X), ",") - 1) & ")")
EmailResult = EmailSearch.FindOne
EmailList.Add(EmailResult.Properties("Mail").Item(0))
Next
Catch
'email dba that the active directory search errored
NotifyAdmin("Error searching Acitve Directory at " & Date.Now, EventLogEntryType.Error)
Finally
DirEntry.Dispose()
GroupSearch.Dispose()
EmailSearch.Dispose()
DirEntry = Nothing
GroupSearch = Nothing
EmailSearch = Nothing
End Try
Try 'attempt to run a stored procedure and email users
With cmdCheck 'check to see if there are any feeds in the next 45 mintues
.CommandType = CommandType.StoredProcedure
.CommandText = "sp_CheckForFeeds_s"
.Parameters.Add("@DateTime", SqlDbType.DateTime, 20).Value = Date.Now
.CommandTimeout = 30
.Connection = Cnn
End With
Cnn.Open()
rdrCheck = cmdCheck.ExecuteReader
While rdrCheck.Read 'send an email for each feed in the reader
BodyText = "Feed Time: " & rdrCheck.Item("FeedTime") & ", Signal: " & rdrCheck.Item("Signal") & ", Satellite: " & rdrCheck.Item("Satellite") & ", Sent: " & Date.Now
For X = 0 To EmailList.Count - 1
Send(ConfigurationSettings.AppSettings("EmailFrom"), EmailList.Item(X), "Feed Notification", BodyText)
Next
End While
rdrCheck.Close()
Cnn.Close()
Catch 'attempt failed
'email dba that the stored procedure and email errored
NotifyAdmin("Error quering database and emailing staff at " & Date.Now, EventLogEntryType.Error)
Finally
Cnn.Dispose()
cmdCheck.Dispose()
Cnn = Nothing
cmdCheck = Nothing
End Try
End Sub
-
If you are using Windows security in your connection string remember that there is no user for a service so the login may fail, also it could be some sort of permissions error. What error are you getting?
-
Thanks for the insight, it would make sense that the account running the service needs access to the database.