2015-09-23 16 views
3

我的問題是: 當我嘗試安裝我的Windows服務我得到以下錯誤:錯誤安裝一個Windows服務(在F#)

片段: ... No public installers with the RunInstallerAttribute.Yes attribute could be found in the <path to exe> assembly. ...

我遵循這個tutorial

我有一個包含一個Program.fs文件:

[<RunInstaller(true)>] 
type public FSharpServiceInstaller() = 
    inherit Installer() 
    do 
     < some logic, doesn't really matter > 

釷應該是足夠的,事實上,我甚至不認爲我需要將public關鍵字添加到類型定義中。與InstallUtil.exe安裝此可執行文件給了我同樣的錯誤使用下面的代碼安裝它:

[<EntryPoint>] 
let main args = 

    if Environment.UserInteractive then 
     let parameter = String.Concat(args); 
     match parameter with 
     | "-i" -> ManagedInstallerClass.InstallHelper [| Assembly.GetExecutingAssembly().Location |] 
     | "-u" -> ManagedInstallerClass.InstallHelper [| "/u"; Assembly.GetExecutingAssembly().Location |] 
     | _ -> printf "Not allowed!\n" 
    else 
     ServiceBase.Run [| new CreditToolsService() :> ServiceBase |]; 
    0 

我試圖運行在PowerShell中,CMD和Visual Studio CLI這個腳本管理員和我正常的帳戶,但我不斷收到同樣的錯誤。如果有人知道我在做什麼錯,我會非常感謝一些幫助。

+0

相關:http://stackoverflow.com/q/31081879/126014 –

+0

感謝您發表評論我真的很感激。我已經檢查了相關的問題,儘管操作員詢問「相同」的問題,但沒有答案。只是一個實現示例。 – Baudin999

回答

2

OK,所以這裏去...

我看了由user1758475提供的代碼,只是隨機副本開始粘貼解決方案整合到一個應用程序。唐Symes的解決方案「剛剛工作」,我終於弄清楚爲什麼:我沒有(和他)有一個名稱空間聲明,在我的來源。似乎這是罪魁禍首!在我添加命名空間後,安裝程序像魅力一樣工作。

正如Curt Nichols指出的那樣,安裝程序不應該位於模塊中,因爲模塊會有效地隱藏調用代碼中的類型。

謝謝你幫忙解決這個問題。

對於那些誰希望看到一個工作示例:

namespace FileWatcher 
open System 
open System.Reflection 
open System.ComponentModel 
open System.Configuration.Install 
open System.ServiceProcess 
open System.IO 
open System.Configuration 

type FileWatcherService() = 
    inherit ServiceBase(ServiceName = "FileWatcher") 

    let createEvent = fun (args: FileSystemEventArgs) -> 
        printf "%s has been %s\n" args.FullPath (args.ChangeType.ToString().ToLower()) 
        |> ignore 

    override x.OnStart(args) = 
     let fsw = new FileSystemWatcher() 
     fsw.Path     <- "C:\TEMP" 
     fsw.NotifyFilter   <- NotifyFilters.LastAccess ||| NotifyFilters.LastWrite ||| NotifyFilters.FileName ||| NotifyFilters.DirectoryName ||| NotifyFilters.CreationTime 
     fsw.Filter     <- "*.txt" 
     fsw.EnableRaisingEvents  <- true 
     fsw.IncludeSubdirectories <- true 
     fsw.Created.Add(createEvent) 

    override x.OnStop() = 
     printf "Stopping the FileWatcher service" 

[<RunInstaller(true)>] 
type public FSharpServiceInstaller() = 
    inherit Installer() 
    do 

     // Specify properties of the hosting process 
     new ServiceProcessInstaller 
      (Account = ServiceAccount.LocalSystem) 
     |> base.Installers.Add |> ignore 

     // Specify properties of the service running inside the process 
     new ServiceInstaller 
      (DisplayName = "AAA FileWatcher Service", 
      ServiceName = "AAAFileWatcherService", 
      StartType = ServiceStartMode.Automatic) 
     |> base.Installers.Add |> ignore 


module Program = 
    [<EntryPoint>] 
    let main args = 

     printf "starting the application...\n" 


     if Environment.UserInteractive then 
      let parameter = String.Concat(args); 
      match parameter with 
      | "-i" -> ManagedInstallerClass.InstallHelper [| Assembly.GetExecutingAssembly().Location |] 
      | "-u" -> ManagedInstallerClass.InstallHelper [| "/u"; Assembly.GetExecutingAssembly().Location |] 
      | _ -> printf "Not allowed!\n" 
     else 
      ServiceBase.Run [| new FileWatcherService() :> ServiceBase |]; 
     0 
+0

另外 - 不要把服務安裝程序類型放在模塊中,這有效地將其隱藏在尋找它的代碼中。 –

0

工作在https://github.com/zbilbo/TB4TG/blob/master/TourneyBot.Service/Installer.fs

現場製作例如認爲它需要與InstallUtil.exe雖然安裝。

可能不是編碼中最好的時刻,但是特定的服務代碼來自Don Syme或多或少:http://blogs.msdn.com/b/dsyme/archive/2011/05/31/a-simple-windows-service-template-for-f.aspx,所以它可能沒問題,但該庫上其餘的「周圍」代碼可能不是慣用的; - )

Don Symes的博客也解釋了很多,所以它應該很容易地適應您的需求。網站還鏈接了一個雙贏服務模板上VS畫廊:http://blogs.msdn.com/b/mcsuksoldev/archive/2011/05/31/f-windows-application-template-for-windows-service.aspx

+0

感謝您發佈,但我似乎無法在提供的代碼鏈接中找到解決我的問題的解決方案。我試圖用'InstallUtil.exe'和我的自定義安裝程序安裝服務,都給我同樣的錯誤。似乎安裝程序無法使用屬性找到安裝程序類,儘管我在代碼中明確指定了此屬性。 – Baudin999

+0

如果其他示例正常工作,而您的示例沒有正常工作,則Occam會告訴我**您**有問題。可能假設你正在做什麼樣的例子顯示你是錯誤的。退後一步,從頭開始,按照原樣做,如果它不起作用,請發送電子郵件給Thomas Petricek或在此處調用他,並告訴他他的代碼是錯誤的。他不時在這裏;-) –

+0

謝謝你的幫助。我已經想出瞭如何解決這個問題。好像我錯過了一個命名空間聲明。我已將我的代碼和解決方案發布在答案中。 – Baudin999