用F#编写服务 [英] Writing a service in F#

查看:63
本文介绍了用F#编写服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am back again, this time with a question on writing service in F#. I cannot seem to install the service using installutil. It gives me the following error.

$ 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.

代码如下.感谢您的任何帮助,并在此先感谢.

The code is given below. Any help is appreciated and thanks in advance.

Ramesh

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 |]

推荐答案

我想出了如何使用网络上的其他示例编写自助安装服务的方法,尤其是这篇关于堆栈的文章非常有用: http://pingfu .net/programming/2011/08/11/creating-a-self-installing-windows-service-with-csharp.html

I figured out how to write a self installing service using other examples on the web, especially this post on stack was useful: http://pingfu.net/programming/2011/08/11/creating-a-self-installing-windows-service-with-csharp.html

open System
open System.ServiceProcess
open System.Windows
open System.Threading
open System.Windows.Forms
open System.ComponentModel
open System.Configuration.Install
open System.Reflection
open Microsoft.Win32

type ATFServiceInstaller() =
    inherit Installer()


 let spi_ = new ServiceProcessInstaller(Account = ServiceAccount.LocalSystem)
    let si_ = new ServiceInstaller( DisplayName = "ATF Service", Description="ATF service", ServiceName = "atf",StartType = ServiceStartMode.Automatic ) 
    let dic_ = new System.Collections.Hashtable()
    let SVC_SERVICE_KET = @"SYSTEM\CurrentControlSet\Services"

    member this.install () = 
        base.Installers.Add(spi_) |> ignore
        let ret = base.Installers.Add(si_)
        let apath = sprintf "/assemblypath=%s" (Assembly.GetExecutingAssembly().Location)
        let ctx = [|apath; "/logToConsole=false"; "/showCallStack"|]
        this.Context <- new InstallContext("atfserviceinstall.log", ctx)

        base.Install(dic_)
        base.Commit(dic_)

    member this.uninstall() = 

        base.Installers.Add(spi_) |> ignore


        let ret = base.Installers.Add(si_)
        let apath = sprintf "/assemblypath=%s" (Assembly.GetExecutingAssembly().Location)
        let ctx = [|apath; "/logToConsole=false"; "/showCallStack"|]
        this.Context <- new InstallContext("atfserviceinstall.log", ctx)
        base.Uninstall(null)

module Main =
try
    let args = Environment.GetCommandLineArgs()

    match (args |> Seq.length) with
    | 2 -> match (args.[1]) with
           | "-install" -> let installer = new ATFServiceInstaller()
                           installer.install()
                           installer.Dispose()
           | "-uninstall" -> let installer = new ATFServiceInstaller()
                             installer.uninstall()
                             installer.Dispose()
           | _ -> failwith "Unrecognized param %s" args.[0]
    | _ -> ServiceBase.Run [| new atfservice.ATFService() :> ServiceBase |]
with
    | _ as ex -> MessageBox.Show(ex.ToString()) |> ignore

这篇关于用F#编写服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆