0

我有一個上週編寫的windows服務,它的意圖是運行一個定時器,定期檢查日期是否發生變化,如果是的話,它會啓動一個方法從我們的SQL服務器獲取對象,並且它們與當前日期匹配它會將它們發送給另一個處理付款的服務。我對服務是陌生的,並且不確定如何安裝它以使其能夠:a)無限地在後臺運行; b)在服務器重置時啓動時運行。如何在服務器上安裝我的Windows服務,以便它在啓動時在後臺運行?

這裏是VB.NET我的Windows服務代碼

Imports System.ServiceProcess 

進口AFI.BusinessObjects.Billing 進口System.Data.SqlClient的

公共類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 

End Class

我有tr ied右鍵單擊服務上的設計器視圖並添加安裝程序,然後我已更改ServiceInstaller1上的serviceName和displayName屬性,然後將ServiceProcessInstaller1的帳戶屬性更改爲LocalSystem。下一步我的教程說的是構建,然後它應該創建一個MyService.exe然而,當我通過我的解決方案文件和文件夾搜索時,我找不到這個exe,所以我不知道爲什麼這不是在構建時創建?我是否以這種錯誤的方式去做?我是否應該用Windows任務計劃程序來解決這個問題,還是應該創建一個安裝程序並將其安裝在我們的服務器上以便始終在後臺運行?感謝您的任何信息或幫助!

回答

1

最好的做法是創建一個MSI。 Service Installer類被認爲是反模式,因爲MSI已經支持服務。請參閱:

Building and Deploying a Windows Service using IsWiX

+0

您好,感謝您的意見,我在視頻下面一起,但我沒有看到一個Windows Insaller XML我可以添加到我的解決方案。我需要以某種方式將IsWix添加到Visual Studio嗎?謝謝! – mcloud313

+1

http://wix.codeplex.com和http://iswix.codeplex.com –

+1

FWIW,我建議單獨的解決方案。由於這個原因,ISWIX不支持項目引用。如果你做同樣的解決方案,你需要添加一個項目依賴關係,以確保正確的構建順序。 –

相關問題