gpt4 book ai didi

f# - 用 F# 编写服务

转载 作者:行者123 更新时间:2023-12-04 11:36:29 25 4
gpt4 key购买 nike

我又回来了,这次是关于在 F# 中编写服务的问题。我似乎无法使用 installutil 安装该服务。它给了我以下错误。

$ installutil atfwindowsservice.exe
Microsoft (R) .NET Framework Installation utility Version 4.0.30319.18408
Copyright (C) Microsoft Corporation. All rights reserved.

Running a transacted installation.

Beginning the Install phase of the installation.
See the contents of the log file for the C:\Dev\ATF\output\bin\Debug\atfwindowsservice.exe assembly's progress.
The file is located at C:\Dev\ATF\output\bin\Debug\atfwindowsservice.InstallLog.
Installing assembly 'C:\Dev\ATF\output\bin\Debug\atfwindowsservice.exe'.
Affected parameters are:
logtoconsole =
logfile = C:\Dev\ATF\output\bin\Debug\atfwindowsservice.InstallLog
assemblypath = C:\Dev\ATF\output\bin\Debug\atfwindowsservice.exe
No public installers with the RunInstallerAttribute.Yes attribute could be found in the C:\Dev\ATF\output\bin\Debug\atfwindowsservice.exe assembly.

代码如下。任何帮助表示赞赏并提前致谢。

拉梅什
namespace service

open System.ServiceProcess
open System.Runtime.Remoting
open System.Runtime.Remoting.Channels

type atf() =
inherit ServiceBase(ServiceName = "atf win service")

override x.OnStart(args) = ()
override x.OnStop() = ()

注册服务代码:
// Learn more about F# at http://fsharp.net
// See the 'F# Tutorial' project for more help.=
open System
open System.ComponentModel
open System.Configuration.Install
open System.ServiceProcess

[<RunInstaller(true)>]
type 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 = "F# ATF Service", ServiceName = "atf",StartType = ServiceStartMode.Automatic ) |> base.Installers.Add |> ignore

// Run the chat service when the process starts
module Main =
ServiceBase.Run [| new service.atf() :> ServiceBase |]

最佳答案

我有同样的问题。我最终添加了以下代码,它运行良好,并且具有不需要 installutil.exe 的额外好处。该服务能够通过传入正确的命令行参数来安装/卸载自身。保留所有代码并添加以下内容:

module Program =   

let getInstaller() =
let installer = new AssemblyInstaller(typedefof<atf>.Assembly, null);
installer.UseNewContext <- true
installer

let installService() =
let installer = getInstaller()
let dic = new System.Collections.Hashtable()
installer.Install(dic)
installer.Commit(dic)

let uninstallService() =
let installer = getInstaller()
let dic = new System.Collections.Hashtable()
installer.Uninstall(dic)

[<EntryPoint>]
let main (args:string[]) =
match (args |> Seq.length) with
|1 -> match (args.[0]) with
|"-install" -> installService()
|"-uninstall" -> uninstallService()
|_-> failwith "Unrecognized param %s" args.[0]
|_ -> ServiceBase.Run [| new atf() :> ServiceBase |]
0

要安装,您可以从命令行执行以下操作:
atfwindowsservice.exe -install

关于f# - 用 F# 编写服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31081879/

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