Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm in the process of converting a very old VB6 Windows Form application to a Windows Service(VB.NET) but since it was kind of a challenging task most of the online community developers suggested me to develop a VB.NET console application as the first step and then convert to a Windows Service easily. So I took the advice and successfully made the console application and made it work successfully. Now when I try to convert the Console App to a Windows Service it does not work after certain point(Note : Console application works perfectly fine). I made sure everything is identical between 2 apps, I added all the references, config files and everything that requires for the Windows Service to work as same as Console App.

PS : I have added some file logs in the code just to check until which part the code runs and seems like no matter what it does not run anything after "Reached point 7! " log. Feel free to check the results below
Click here to see the output.

What I have tried:

VB.NET
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Windows.Forms
Imports System.Diagnostics
Imports System.IO
Imports System.Timers
Imports System.ServiceProcess
Imports Wisys.AllSystem.TrxEnums
Imports Wisys.AllSystem.ReturnCodes
Imports ADODB
Imports System.Text

Public Class Service1

Dim Timer1 As Timers.Timer

' Global Variables
Public g_bESVersion As Boolean
Public sWiSysUser As String
Public g_sRtnServer As String = "STLEDGSQL01"
Public cnADOMacola As New ADODB.Connection
Public cnADOEDGE_DSN As New ADODB.Connection
Public cnADODEV As New ADODB.Connection

Dim lngST = 0
Dim strStatus As String
Dim strMsg As String
Dim strConnectionString As String
Dim strSelectStatement As String
Dim strEDGE_DSN As String = "TestDEV"
Dim strALL_LABOR_TABLE As String = "[MES_DEV].[dbo].[ALL_LABOR_DETAILS]"

Dim strOrderNumber As String
Dim prevStrOrderNumber As String
Dim strItemNumber As String
Dim strItemNumber2 As String
Dim strOrdLocation As String
Dim strPathNumber As String
Dim strOperationNumber As String
Dim strOperatorSeqNumber As String
Dim strCntPnt As String
Dim strWorkCenter As String
Dim strLaborGrade As String
Dim lngClosedPeriodCount As Long = 0
Dim lngPostedCount As Long = 0
Dim lngSkippedCount As Long = 0
Dim varResponse As String
Dim TargetCount As Integer
Dim postCountVal As Integer

Dim rs1 As New ADODB.Recordset
Dim rs2 As New ADODB.Recordset
Dim rs3 As New ADODB.Recordset
Public g_sRtnDatabase As String = "XXXXXXXXX"
Public dev_Database As String = "XXXXXXXXX"
Public g_sSQLUser As String = "XXXXXXXXX"
Public g_sSQLPass As String = "XXXXXXXXX"
Public WiSysConn As New Wisys.AllSystem.ConnectionInfo
Public WiSysTrans As New Wisys.AllSystem.ConnectionInfo
Public Const AppTitle As String = "XXXXXXXXX"
Public Const g_RtnApplicationName As String = "XXXXXXXXX"

Protected Overrides Sub OnStart(ByVal args() As String)

Try
Timer1 = New Timers.Timer()
Timer1.Interval = 300000
AddHandler Timer1.Elapsed, AddressOf MyTickHandler
Timer1.Enabled = True

Catch ex As Exception

FileIO.WriteLog("Exception Occured : " + ex.Message)

End Try

End Sub


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

On Error Resume Next

If Trim(strStatus) <> "" Then
Console.WriteLine(strStatus)
End If

Console.WriteLine(CStr(Now) & " Application End")
FileIO.WriteLog(CStr(Now) & " Application End")


If rs1.State = 1 Then rs1.Close()
If rs2.State = 1 Then rs2.Close()

Console.WriteLine(CStr(Now) & cnADOMacola.State)

If cnADOMacola.State = 1 Then cnADOMacola.Close()
If cnADOEDGE_DSN.State = 1 Then cnADOEDGE_DSN.Close()

On Error GoTo 0

End Sub

Private Sub MyTickHandler()

'Initializing the Wisys connection and Open the connection
ApplicationStartup()

'Validating all the data and posting transactions
SFCActivityTransactions()

End Sub

Sub ApplicationStartup()

Dim sRtnErr As String = ""

'Assign the connection parameters to 2 connections. One will be a transaction-based connection

WiSysConn.Parameters(g_sRtnServer, g_sRtnDatabase, g_sSQLUser, g_sSQLPass, g_RtnApplicationName)
WiSysTrans.Parameters(g_sRtnServer, g_sRtnDatabase, g_sSQLUser, g_sSQLPass, g_RtnApplicationName)

Dim iRtn As Integer
'Go ahead and open the non-transactional based connection
iRtn = WiSysConn.OpenWisysConnection(False, sRtnErr)

If iRtn <> Wisys.AllSystem.ReturnCodes.SUCCESSFUL Then
FileIO.WriteLog(" Return Codes : " & sRtnErr)
Else
FileIO.WriteLog(" Wisys Objects Connected to Database!")
End If

End Sub

Sub SFCActivityTransactions()

Timer1.Stop()

Do
lngST = lngST + 1

Dim p_sRtnErrMsg = ""
If WiSysConn.TestConnectivity(p_sRtnErrMsg) = False Then
If lngST >= 3 Then
strStatus = "Wisys objects failed to connect to database. " & p_sRtnErrMsg
FileIO.WriteLog(strStatus)
Exit Do
End If
Else
strStatus = ""
Exit Do
End If
Loop

strConnectionString = "DRIVER={SQL Server};Server=" & g_sRtnServer & ";UID=" & g_sSQLUser & ";PWD=" & g_sSQLPass & ";DATABASE=" & g_sRtnDatabase

FileIO.WriteLog(strConnectionString)

FileIO.WriteLog("Macola Connection State : " & cnADOMacola.State)

If cnADOMacola.State = 1 Then cnADOMacola.Close()
cnADOMacola.ConnectionString = strConnectionString

FileIO.WriteLog("Reached point 1! ")

On Error Resume Next
cnADOMacola.Open(strConnectionString)
lngST = Err.Number
strMsg = Err.Description
FileIO.WriteLog("Reached point 2! ")
On Error GoTo 0

FileIO.WriteLog("Reached point 3! ")

If lngST <> 0 Then
strStatus = "There was an error trying to connect to the " & g_sRtnDatabase & " database. Error " & lngST & " - " & strMsg & ". Program ends now!"
FileIO.WriteLog(strStatus)
OnStop()
End If

FileIO.WriteLog("Reached point 4! ")

strSelectStatement = "SELECT bedrnm FROM [100].[dbo].[bedryf] WHERE ID = 1"

FileIO.WriteLog("Reached point 5! ")

If rs1.State = 1 Then rs1.Close()
rs1.Open(strSelectStatement, cnADOMacola, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly)

FileIO.WriteLog("Reached point 6! ")

If rs1.EOF Then
strStatus = "There was an error finding the company name. Program exits now!"
FileIO.WriteLog(strStatus)
OnStop()
Else
FileIO.WriteLog("Reached point 7! ")
FileIO.WriteLog("Company Name : " & rs1.Fields("bedrnm").Value)
End If

FileIO.WriteLog("Reached point 8! ")

If rs1.State = 1 Then rs1.Close()

FileIO.WriteLog("Reached point 9! ")

FileIO.WriteLog("Connecting to MES_DEV database using " & strEDGE_DSN & " DSN")

FileIO.WriteLog("Reached point 10! ")

Do
If cnADOEDGE_DSN.State = 1 Then cnADOEDGE_DSN.Close()
cnADOEDGE_DSN.ConnectionString = "DSN=" & strEDGE_DSN
On Error Resume Next
cnADOEDGE_DSN.Open("DSN=" & strEDGE_DSN)
lngST = Err.Number
strMsg = Err.Description
On Error GoTo 0
If lngST = 0 Then Exit Do
strStatus = "The following error occurred trying to connect to the ACCESS database. Please make sure you have set up an ODBC connection named " & strEDGE_DSN & " to this database. " & strMsg
FileIO.WriteLog(strStatus)
Loop

Code continues................


I appreciate all your help! Thank you
Posted
Comments
Richard Deeming 2-Aug-21 9:11am    
Your code is probably crashing, but it will be very hard to debug because you're still using the VB6-era "On Error ..." error handling. Switch to using structured error handling instead:
Try...Catch...Finally statement - Visual Basic | Microsoft Docs[^]

You should also ditch ADODB and use ADO.NET instead:
Overview - ADO.NET | Microsoft Docs[^]
GlennAshan 2-Aug-21 9:42am    
Thank you Richard for your ideas, I think it will help me to debug much easier. :)
SeeSharp2 2-Aug-21 9:17am    
1. If your console app works, you can use the Windows Task Scheduler to have it run even without someone logged in. That may be the easiest answer.
2. The correct answer is to rewrite this as a .Net app using all .Net code.
3. However, to focus on what you have here, if it only gets to point 7 that means that this line of code errors:
FileIO.WriteLog("Company Name : " & rs1.Fields("bedrnm").Value).
GlennAshan 2-Aug-21 9:48am    
Yes I know that's the easiest way but my client's requirement(end goal) is to have a windows-service. So I'm working on it, and yes I think I would have to convert the parts which are still VB6 code. Finally I was thinking the same about the line you mentioned has some issues, let's see I'll try to catch the errors using 'Try Catch'. Thank you! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900