gpt4 book ai didi

f# - 安装 Windows 服务时出错(在 F# 中)

转载 作者:行者123 更新时间:2023-12-02 01:33:27 25 4
gpt4 key购买 nike

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

片段:
...
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 中运行此脚本,但我不断收到相同的错误。如果有人知道我做错了什么,我将不胜感激。

最佳答案

好吧,现在开始...

我查看了 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

关于f# - 安装 Windows 服务时出错(在 F# 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32732426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com