3

我的问题如下:当我尝试安装 Windows 服务时,出现以下错误:

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

我按照这个教程

我有一个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 中运行此脚本,但我一直收到相同的错误。如果有人知道我做错了什么,我将非常感谢一些帮助。

4

2 回答 2

2

好的,那就这样吧……

我查看了 user1758475 提供的代码,只是随机开始将粘贴解决方案复制到应用程序中。Don 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
于 2015-09-23T09:12:00.457 回答
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 Gallery 上的 Win 服务模板:http: //blogs.msdn.com/b/mcsuksoldev/archive/2011/05/31/f-windows-application-template-for-windows-service.aspx

于 2015-09-23T06:48:29.463 回答