如何在服务器上安装我的Windows服务,以便它在启动时在后台运行?

时间:2015-09-28 14:01:03

标签: vb.net visual-studio-2012 timer windows-services windows-installer

我有一个我上周编写的Windows服务,它的目的是运行一个定时检查日期是否已更改的计时器,如果是这样,它启动一个方法从我们的SQL服务器获取对象,如果它们与当前匹配将它们发送给另一个处理付款的服务的日期。我不熟悉服务,并且不确定如何安装它以便a)无休止地在后台运行b)在服务器重置时运行启动。

以下是我在VB.NET中使用Windows服务的代码

Imports System.ServiceProcess

导入AFI.BusinessObjects.Billing Imports System.Data.SqlClient

Public Class FutureTransactionProcessor

'Creates a timer that can't be grabbed up by garbage collection
Private Timer As System.Timers.Timer

'Create a variable for todays date minus one to check against and see if the date has advanced.
Private lastRun As DateTime = DateTime.Now.AddDays(-1)

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.

    'Timer that finishes its run every 10 minutes
    Timer = New System.Timers.Timer(10 * 60 * 1000)

    'we trigger a method as soon as the timer has finished running its course
    'Timer.Elapsed += New System.Timers.ElapsedEventHandler(AddressOf timerElapsed)
    AddHandler Timer.Elapsed, AddressOf timerElapsed

    'start our timer
    Timer.Start()

End Sub

Protected Sub timerElapsed(ByVal sender As Object, e As System.Timers.ElapsedEventArgs)

    If lastRun.Date < DateTime.Now.Date Then

        'stop the timer to perform our check against the FUTURE_TRANSACTIONS table
        Timer.Stop()

        'BEGIN FUNCTION TO RETURN FUTURE TRANSACTIONS FROM FUTURE_TRANSACTION TABLE THAT MATCH TODAYS DATE IF ANY
        GetFutureTransactionsByDate(DateTime.Now.Date)


        'reset our lastRun variable so we'll know when this method was last ran
        lastRun = DateTime.Now

        'restart our timer
        Timer.Start()

    End If


End Sub

Public Shared Function GetFutureTransactionsByDate(ByVal dateToday As DateTime) As FuturePaymentsCollection

    Dim FuturePaymentsToBeProcessed As FuturePaymentsCollection = New FuturePaymentsCollection

    Using cnSQL As SqlConnection = New SqlConnection(AFI.Configuration.SystemSetting.Collection("ClientServer", "CS_Connection_String").SettingValue)

        Using cmdSP As New SqlCommand("PROC_FUTURE_TRANSACTIONS_SEL_BY_TODAY", cnSQL)

            cmdSP.CommandType = System.Data.CommandType.StoredProcedure
            cmdSP.Parameters.AddWithValue("DATETODAY", dateToday)

            cmdSP.Connection.Open()
            Dim sqlReader As SqlDataReader = cmdSP.ExecuteReader()

            If sqlReader.HasRows Then
                While (sqlReader.Read())
                    Dim futurePayment As FuturePayment = New FuturePayment

                    futurePayment.FutureTransactionID = sqlReader.GetInt32(sqlReader.GetOrdinal("BMW_TRANSACTION_ID"))
                    futurePayment.GroupID = sqlReader.GetInt32(sqlReader.GetOrdinal("BMW_CNTC_GROUP_ID"))
                    futurePayment.PayorAccountID = sqlReader.GetInt32(sqlReader.GetOrdinal("BMW_PAYOR_ACCOUNT_ID"))
                    futurePayment.PolicyID = sqlReader.GetInt32(sqlReader.GetOrdinal("BMW_POLICY_ID"))
                    futurePayment.AccountTypeID = sqlReader.GetInt32(sqlReader.GetOrdinal("BMW_ACCOUNT_TYPE_ID"))
                    futurePayment.TransationTypeID = sqlReader.GetInt32(sqlReader.GetOrdinal("BMW_TRANSACTION_TYPE_ID"))
                    futurePayment.TransactionDate = sqlReader.GetDateTime(sqlReader.GetOrdinal("BMW_TRANSACTION_DATE")).ToString("MM/dd/yyyy")
                    futurePayment.TransactionSubmitter = sqlReader.GetInt32(sqlReader.GetOrdinal("BMW_TRANSACTION_SUBMITTER"))
                    futurePayment.TransactionAmount = sqlReader.GetDecimal(sqlReader.GetOrdinal("BMW_TRANSACTION_AMOUNT"))
                    futurePayment.TransactionLast4 = sqlReader.GetString(sqlReader.GetOrdinal("BMW_TRANSACTION_LAST4"))
                    futurePayment.TransactionEmail = sqlReader.GetString(sqlReader.GetOrdinal("BMW_TRANSACTION_EMAIL"))
                    futurePayment.PaymentInfo1 = sqlReader.GetString(sqlReader.GetOrdinal("PaymentInfo1"))
                    futurePayment.PaymentInfo2 = sqlReader.GetString(sqlReader.GetOrdinal("PaymentInfo2"))
                    futurePayment.PaymentInfo3 = sqlReader.GetString(sqlReader.GetOrdinal("PaymentInfo3"))
                    futurePayment.PaymentInfo4 = sqlReader.GetString(sqlReader.GetOrdinal("PaymentInfo4"))
                    futurePayment.PaymentInfo5 = sqlReader.GetString(sqlReader.GetOrdinal("PaymentInfo5"))
                    futurePayment.PaymentInfo6 = sqlReader.GetString(sqlReader.GetOrdinal("PaymentInfo6"))
                    futurePayment.TransactionUpdateDate = sqlReader.GetDateTime(sqlReader.GetOrdinal("BMW_TRANSACTION_UPDATE_DATE"))


                    FuturePaymentsToBeProcessed.Add(futurePayment)

                End While
            End If

            cmdSP.Connection.Close()

        End Using

    End Using


    'Return us a collection of FuturePayment Items
    Return FuturePaymentsToBeProcessed


    'For every item returned we need to turn it into a OneTimePayment object
    For Each Payment As FuturePayment In FuturePaymentsToBeProcessed

        Dim PaymentToBeProcessed As OneTimePayment

        PaymentToBeProcessed.PayorAccountId = Payment.PayorAccountID
        PaymentToBeProcessed.PolicyID = Payment.PolicyID
        PaymentToBeProcessed.AccountTypeID = Payment.AccountTypeID

        'Future payments can only be EFT so we'll go ahead and set that to 1
        PaymentToBeProcessed.PayTypeID = 1
        PaymentToBeProcessed.BankInfoName = Payment.PaymentInfo1
        PaymentToBeProcessed.BankInfoRoutingNum = Payment.PaymentInfo2
        PaymentToBeProcessed.BankInfoAccountNum = Payment.PaymentInfo3

        If PaymentToBeProcessed.BankInfoAccountNum.Length >= 4 Then
            PaymentToBeProcessed.Last4 = PaymentToBeProcessed.BankInfoAccountNum.Substring(PaymentToBeProcessed.BankInfoAccountNum.Length - 4, 4)
        Else
            PaymentToBeProcessed.Last4 = "XXXX"
        End If

        PaymentToBeProcessed.TransactionTypeID = 1
        PaymentToBeProcessed.Email = Payment.TransactionEmail
        PaymentToBeProcessed.TransactionAmount = Payment.TransactionAmount

        PaymentToBeProcessed.Save()
        PaymentToBeProcessed.SendPaymentToGateway()


        'Run our method to remove the future payment from the Future_Transactions table and enter it into the Future_Transactions_History table as processed
        Payment.ProcessFuturePayment(Payment.FutureTransactionID)



    Next

End Function



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

结束班

我试图右键单击服务上的设计器视图并添加安装程序,我已经更改了ServiceInstaller1上的serviceName和displayName属性,然后我已将帐户属性更改为ServiceSrocessInstaller1的LocalSystem。我的教程说的下一步是构建,然后它应该创建一个MyService.exe但是当我搜索我的解决方案文件和文件夹时,我找不到这个exe所以我不知道为什么这不是在构建时创建的?我是以错误的方式来做这件事的吗?我应该使用Windows任务计划程序解决这个问题,还是应该创建一个安装程序并将其安装在我们的服务器上以便在后台运行?感谢您提供任何信息或帮助!

1 个答案:

答案 0 :(得分:1)

最佳做法是创建MSI。 Service Installer类被视为反模式,因为MSI已经支持服务。参见:

Building and Deploying a Windows Service using IsWiX