|
JOB REFERRALS
|
|
|
|
ON THIS PAGE
|
|
|
|
|
ARCHIVES
|
| May, 2013 (1) |
| April, 2013 (4) |
| March, 2013 (4) |
| February, 2013 (5) |
| January, 2013 (8) |
| December, 2012 (3) |
| November, 2012 (5) |
| October, 2012 (4) |
| May, 2012 (1) |
| March, 2012 (6) |
| January, 2012 (4) |
| December, 2011 (2) |
| October, 2011 (2) |
| August, 2011 (1) |
| May, 2011 (1) |
| April, 2011 (1) |
| February, 2011 (1) |
| January, 2011 (1) |
| December, 2010 (1) |
| November, 2010 (1) |
| October, 2010 (3) |
| September, 2010 (4) |
| August, 2010 (2) |
| July, 2010 (1) |
| June, 2010 (1) |
| May, 2010 (3) |
| March, 2010 (5) |
| February, 2010 (1) |
| January, 2010 (4) |
| December, 2009 (1) |
| November, 2009 (3) |
| October, 2009 (3) |
| August, 2009 (2) |
| July, 2009 (4) |
| June, 2009 (3) |
| May, 2009 (6) |
| April, 2009 (4) |
| March, 2009 (4) |
| February, 2009 (5) |
| January, 2009 (11) |
| December, 2008 (3) |
| November, 2008 (9) |
| October, 2008 (1) |
| September, 2008 (2) |
| August, 2008 (4) |
| July, 2008 (10) |
| June, 2008 (5) |
| May, 2008 (10) |
| April, 2008 (13) |
| March, 2008 (11) |
| February, 2008 (18) |
| January, 2008 (17) |
| December, 2007 (12) |
| November, 2007 (2) |
| October, 2007 (6) |
| September, 2007 (1) |
| August, 2007 (2) |
| July, 2007 (7) |
| June, 2007 (1) |
| May, 2007 (1) |
| April, 2007 (2) |
| March, 2007 (2) |
| February, 2007 (1) |
| January, 2007 (16) |
| December, 2006 (3) |
| November, 2006 (7) |
| October, 2006 (5) |
| September, 2006 (1) |
| June, 2006 (4) |
| May, 2006 (3) |
| April, 2006 (3) |
| March, 2006 (17) |
| February, 2006 (5) |
| January, 2006 (13) |
| December, 2005 (2) |
| November, 2005 (6) |
| October, 2005 (15) |
| September, 2005 (16) |
| August, 2005 (17) |
|
|
|
CATEGORIES
|
|
|
|
|
BLOGROLL
|
|
|
|
|
LINKS
|
|
|
|
|
SEARCH
|
|
|
|
|
MY BOOKS
|
|
|
|
|
DISCLAIMER
|
Powered by:
newtelligence dasBlog 1.9.7067.0
The opinions expressed herein are my own personal opinions and do not represent
my employer's view in any way.
© Copyright
2013
,
Ted Neward
E-mail
|
|
|
|
|
 Saturday, November 20, 2010
|
Windows Service in F#
|
|
Recently I received an email forwarded to me from a fan of the F# language, asking about the steps required to build a Windows service (the Windows equivalent to a background daemon from Unix) in F#. It’s not hard, but getting the F# bits in the right place can be tricky—the key being, the Installer (that will be invoked when installutil.exe is asked to install your service) has to have the right custom attribute in place, and the service has to have all the bits lined up perfectly. So, without further ado…. - Create an F# Application project
- Add references to “System.ServiceProcess” and “System.Configuration.Install”
- Create the service itself:
namespace FSService open System.ComponentModel open System.Configuration.Install open System.ServiceProcess type WindowsService() as this = inherit ServiceBase() do this.ServiceName <- "My Windows Service" this.EventLog.Log <- "Application" override this.OnStart(args:string[]) = base.OnStart(args) override this.OnStop() = base.OnStop() static member Main() = ServiceBase.Run(new WindowsService())
- Create the service installer class (inside the same namespace, for easy reference):
[<RunInstaller(true)>] type MyInstaller() as this = inherit Installer() do let spi = new ServiceProcessInstaller() let si = new ServiceInstaller() spi.Account <- ServiceAccount.LocalSystem spi.Username <- null spi.Password <- null si.DisplayName <- "My New F# Windows Service" si.StartType <- ServiceStartMode.Automatic si.ServiceName <- "My Windows Service" this.Installers.Add(spi) |> ignore this.Installers.Add(si) |> ignore - Build.
- Take the resulting executable, install the service by using the “installutil.exe” utility from the .NET SDK: “installutil /I FSService.exe”
- Verify that the service has been installed in the Services Control Panel.
MSDN documentation describes Windows services and the various overridable methods from ServiceBase, as well as how the ServiceInstaller configuration options describe the service itself (account to use, start mode, etc). Update: Whoops! I forgot something else—the service above will install, but it won’t start! The symptom is that you’ll get a “timeout” error immediately after trying to start the service, and the reason for that is because F# (unlike C#) doesn’t recognize the Main() member as an assembly entry point. (I knew that, I swear.) The fix is to move Main() outside of the WindowsService type and into a module (which EntryPoint requires), like so: module Entry = [<EntryPoint>] let Main(args) = ServiceBase.Run(new WindowsService()) 0
Sorry about that. Bear in mind, too, that the service may have additional properties (CanShutdown, CanPauseAndContinue, etc) that you may also want to set, in order to receive those events (OnShutdown, OnPause, etc) in the service. From here, though, things should be smooth sailing.
|
|