Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Curious Problem..

Reply
Thread Tools

Curious Problem..

 
 
Rob Meade
Guest
Posts: n/a
 
      04-26-2004
...here is the code from the service


Imports System.IO
Imports System.Data.SqlClient

Public Class ScanFolder
Inherits System.ServiceProcess.ServiceBase

Public folderToWatch As New FileSystemWatcher

#Region " Component Designer generated code "
Public Sub New()
MyBase.New()
' This call is required by the Component Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call
End Sub
'UserService overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
' The main entry point for the process
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
' More than one NT Service may run within the same process. To add
' another service to this process, change the following line to
' create a second service object. For example,
'
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1,
New MySecondUserService}
'
ServicesToRun = New System.ServiceProcess.ServiceBase() {New ScanFolder}
System.ServiceProcess.ServiceBase.Run(ServicesToRu n)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
' NOTE: The following procedure is required by the Component Designer
' It can be modified using the Component Designer.
' Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'Service1
'
Me.ServiceName = "ScanFolder"
End Sub
#End Region

Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
' Debugger.Launch()
' Declare variables

Dim strSysDir As String
'strSysDir = System.Environment.SystemDirectory()
strSysDir =
"C:\Inetpub\mailroot\mailbox\kingswoodwebservices. co.uk\P3_Support.mbx"
folderToWatch.Path = strSysDir
With folderToWatch
.NotifyFilter = .NotifyFilter Or NotifyFilters.FileName
.NotifyFilter = .NotifyFilter Or NotifyFilters.Attributes
' To watch only emails
.Filter = "*.eml"
'To watch sub directories....
.IncludeSubdirectories = True
End With

AddHandler folderToWatch.Created, AddressOf newLog
'AddHandler folderToWatch.Changed, AddressOf newLog
'AddHandler folderToWatch.Renamed, AddressOf newLog
'AddHandler folderToWatch.Deleted, AddressOf newLog
folderToWatch.EnableRaisingEvents = True

End Sub

Private Sub newLog(ByVal Source As Object, ByVal evt As FileSystemEventArgs)

If evt.ChangeType = WatcherChangeTypes.Created Then
writeToLog(evt.FullPath, "Created")
'ElseIf evt.ChangeType = WatcherChangeTypes.Deleted Then
' writeToLog(evt.FullPath, "Deleted")
'ElseIf evt.ChangeType = WatcherChangeTypes.Changed Then
' writeToLog(evt.FullPath, "Changed")
End If

End Sub

Private Sub writeToLog(ByVal filePath As String, ByVal fileManip As String)

Dim evtLog As New EventLog
If Not evtLog.SourceExists("ScanFolder") Then
evtLog.CreateEventSource("ScanFolder", "ScanFolder")
End If

evtLog.Source = "ScanFolder"
evtLog.Log = "ScanFolder"
evtLog.WriteEntry("ScanFolder", "File " & filePath & " has " & fileManip
& " by " & Environment.UserName.ToString, EventLogEntryType.Information)

parseEmail(filePath)

End Sub

Private Sub parseEmail(ByVal path As String)

' Declare variables
Dim objStreamReader As StreamReader
Dim strStream As String

If File.Exists(path) Then
' Open file, populate variable, close file
objStreamReader = New StreamReader(path)
strStream = objStreamReader.ReadToEnd()
objStreamReader.Close()

' Declare variables
Dim objConnection As SqlConnection
Dim objCommand As SqlCommand
Dim strConnection As String
Dim strSQL As String

' Define connection string
strConnection = "user id=*****;data source=""TITUS\KWS"";initial
catalog=testservice; password=*****"

' Create and open our database connection
objConnection = New SqlConnection(strConnection)
objConnection.Open()

' Generate SQL statement
strSQL = "exec storeEmail" & " '" & strStream & "' "

' Execute command
objCommand = New SqlCommand(strSQL, objConnection)
objCommand.ExecuteNonQuery()

' Close and clear our objects
objCommand.Dispose()
objCommand = Nothing
objConnection.Close()
objConnection = Nothing

End If

End Sub

Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
End Sub

End Class


 
Reply With Quote
 
 
 
 
Rob Meade
Guest
Posts: n/a
 
      04-26-2004
I dont think it'll matter but I have installed the service using the
InstallUtil stuff from the build directory - ie,

C:\Documents and Settings\MeadeR\My Documents\Visual Studio
Projects\ScanFolder\bin\ScanFolder.exe

It installs fine but it just occured to me that as this isn't in any
'system' or 'system32' directory or anything whether that might have an
affect...?

Rob


 
Reply With Quote
 
 
 
 
Rob Meade
Guest
Posts: n/a
 
      04-26-2004
"Chris Botha" wrote ...

> Add a test piece to your service to
> insert a test record in a test table (run this code first before anything
> else, so if something fails it does not effect the test code), to confirm
> your database access works.


Hi Chris,

I added it in the onstart section of the service, sure enough it wrote a row
to the database - I've used the same table and same connection string to
ensure that the test is a good'un...

I then stopped the service and started it again - another row was written to
the database.

I have now just changed the user account stuff from log on locally (which it
defaulted back to after I rebuilt the service) to meader (me) and the test
database code worked still...

So both logon locally and meader AND the database connection are sound (in
my opinion).

Regards

Rob


 
Reply With Quote
 
Rob Meade
Guest
Posts: n/a
 
      04-26-2004
"Chris Botha" wrote ...

> If this works, write a test piece to delete a dummy file from the

directory,
> insert the code right after the SQL server test piece, to confirm you have
> access to the directory.


I added a text file in the mailbox directory named "delete-me.txt"

I then added the code to delete this file within the onstart section but
after the database test code.

Uninstalled/rebuilt/installed the service - started it - using the logon
locally account it successfully completed both test tasks; 1 row to
database, file deleted.

(soz for delay - had to look up some code for deleting files!)

Next? )

Regards

Rob


 
Reply With Quote
 
Rob Meade
Guest
Posts: n/a
 
      04-26-2004
Hi Chris,

I have amended my code slightly - before the part (see "incase it helps") -
where I start declaring my database connection variables/objects etc...

I have changed the code to:

If File.Exists(path) Then

' Open file, populate variable, close file
objStreamReader = New StreamReader(path)
strStream = objStreamReader.ReadToEnd()
objStreamReader.Close()

ElseIf Not File.Exists(path) Then

strStream = "No file path was received"

End If

strStream is what should be written to the database - I figured if I did
this I could be guaranteed a value that 'should' get written to the
database - since doing this I still do not get a row written - I am
therefore wondering now whether this sub is actually getting called from the
sub above or not? (see my line parseEmail(filePath) )

Regards

Rob


 
Reply With Quote
 
Rob Meade
Guest
Posts: n/a
 
      04-26-2004
Erm - what the hell!

Ok - get this - with the database code BEFORE the call to the parseEmail
sub - I get a test row written to the database as we would expect - BUT - I
also get the row that should have been written to the database containing
the email - have tested this 3 times thus far - using Mail2Web.com to send
the emails through (as development box is sending and receiving)...

Have just tested from dev box - and yes, its working as it should too.....

So - why does calling the database BEFORE calling the sub fix the problem
then - bit odd aint it??

Off for food now - will check back later - thanks for the help thus far )

Regards

Rob


 
Reply With Quote
 
Chris Botha
Guest
Posts: n/a
 
      04-26-2004
Rob, sorry, I did not really look at your code, and I won't have time
either, but I can just give you some pointers.
The File Watcher runs on it's own thread, so what you do must be thread
safe. When the event occurs, open the connection as a stack variable
(declare it just before you use it), write, dispose the connection, etc.
Test one piece at a time, for example when a new email comes in, just beep,
don't do anything in the database/event log, etc, until you know that it
works, then add another piece and move the beep to the new piece, etc.

"Rob Meade" <(E-Mail Removed)> wrote in message
news:e3djc.2502$(E-Mail Removed)...
> Erm - what the hell!
>
> Ok - get this - with the database code BEFORE the call to the parseEmail
> sub - I get a test row written to the database as we would expect - BUT -

I
> also get the row that should have been written to the database containing
> the email - have tested this 3 times thus far - using Mail2Web.com to send
> the emails through (as development box is sending and receiving)...
>
> Have just tested from dev box - and yes, its working as it should too.....
>
> So - why does calling the database BEFORE calling the sub fix the problem
> then - bit odd aint it??
>
> Off for food now - will check back later - thanks for the help thus far

)
>
> Regards
>
> Rob
>
>



 
Reply With Quote
 
Rob Meade
Guest
Posts: n/a
 
      04-27-2004
"Chris Botha" wrote...

> Rob, sorry, I did not really look at your code, and I won't have time
> either, but I can just give you some pointers.


Ah ok...no worries..

> The File Watcher runs on it's own thread, so what you do must be thread
> safe. When the event occurs, open the connection as a stack variable
> (declare it just before you use it), write, dispose the connection, etc.


*whoosh* - have no idea about 'threads' and stuff... /

> Test one piece at a time, for example when a new email comes in, just

beep,
> don't do anything in the database/event log, etc, until you know that it
> works, then add another piece and move the beep to the new piece, etc.


okey dokey I'll try that - thank you very much for all of your help Chris
it has been appreciated,

Regards

Rob


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
just curious Mike Roberts Wireless Networking 0 11-28-2005 04:31 AM
Curious article for Linksys router wrt54GS Frank Wireless Networking 0 07-03-2005 05:04 PM
just curious whitzombi Firefox 2 11-09-2004 03:37 AM
Curious Ed Mullen Firefox 34 03-02-2004 11:34 PM
A curious behaviour with an URL on the W3C Griffure Firefox 0 08-11-2003 11:46 AM



Advertisments