F# Windows应用程序模板

前段时间,我写了一篇描述了如何为WinForms应用程序创建项目模板的博客。这篇博客描述了一个新的项目模板:提供了用F# 创建Windows Service程序需要的元素。你可以在Visual Studio Gallery里找到这个模板:

http://visualstudiogallery.msdn.microsoft.com/1f2ee1da-e87f-4863-b98e-d8b7b63d2253

安装完此模板后,你会在创建F#项目时看到如下图所示的模板已经被加入到F# 文件夹中了:

image

这个模板提供了多个项目元素:

  • 一个MyService.fs和Program.fs源文件提供服务元素
  • 一个Installer.fs源文件实现了利用安装工具安装此服务的功能
  • 一个安转与卸载此服务的命令文件

这个MyService.fs源文件提供了此服务的实现。这个模板代码为OnStart和OnStop方法提供了框架已经展示了如何创建一个EventLog和如何引用此EventLog。

namespace FSharp.Service
open System;
open System.Diagnostics;
open System.Linq;
open System.ServiceProcess;
open System.Text;
type public MyService() as service =
    inherit ServiceBase()
  
    // TODO define your service variables
    let eventLog = new EventLog();
   
    // TODO initialize your service
    let initService =
        service.ServiceName <- "FSharp.Service"
        // Define the Event Log
        let eventSource = "FSharp.Service"
        if not (EventLog.SourceExists(eventSource)) then
            EventLog.CreateEventSource(eventSource, "Application");
        eventLog.Source <- eventSource;
        eventLog.Log <- "Application";
    do
        initService
    // TODO define your service operations
    override service.OnStart(args:string[]) =
        base.OnStart(args)
        eventLog.WriteEntry( "Service Started")
    override service.OnStop() =
        base.OnStop()
        eventLog.WriteEntry( "Service Ended")

此程序的如可被定义在Program.fs源文件中:

namespace FSharp.Service

open System
open System.Collections.Generic
open System.Linq
open System.ServiceProcess
open System.Text
module Program =
    [<EntryPoint>]
    let Main(args) =
        // Define your services
        let myService = new MyService()
        // Start the services
        let servicesToRun = [| myService :> ServiceBase |]
        ServiceBase.Run(servicesToRun)
        // main entry point return
        0

如上所示,一个服务的入口是用[<EntryPoing>]属性来定义的。

最后,为了让安装工具能正确安装此服务,一个名为Installer的类被定义如下所示:
namespace FSharp.Service
open System
open System.Configuration
open System.Configuration.Install
open System.ComponentModel
open System.Linq
open System.ServiceProcess
[<RunInstaller( true)>]
type public ProjectInstaller() as installer =
    inherit Installer()
    let processInstaller = new ServiceProcessInstaller();
    let serviceInstaller = new ServiceInstaller();
    // TODO initialize your service
    let initInstaller =
        // Define the process settings
        processInstaller.Account <- ServiceAccount.LocalSystem
        processInstaller.Password <- null
        processInstaller.Username <- null
       
        // Define the service settings
        serviceInstaller.Description <- "FSharp.Service MyService"
        serviceInstaller.ServiceName <- "FSharp.Service.MyService"
        serviceInstaller.StartType <- ServiceStartMode.Manual;
       
        // Define the installers
        let installers = [| processInstaller :> Installer; serviceInstaller :> Installer|]
        installer.Installers.AddRange(installers)
    do
        initInstaller

这个版本的模板使用本地帐户来创建此服务。但是,修改帐户是很简单的。

最后,这个模板创建一个用来安装和卸载此服务的命令文件。当运行此命令文件时,你需要一个较高的权限来运行此命令。

另外,但愿此模板对你有所帮助。

CarlNolan

原文链接:http://blogs.msdn.com/b/mcsuksoldev/archive/2011/05/31/f-windows-application-template-for-windows-service.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值