Freigeben über


Creating and Hosting an even more Minimal WCF Service in .NET 4 using F#

Visual Studio 2010 and .NET 4 will include support for a new language called F#. I must admit that you have to learn how to love F#, it’s not that often love at first sight, because you just can’t figure out what is going on. Even though I’m beginning to like F# more and more and who knows… I might even love it some time soon. Today I got the time to think a lot, during a drive to and from a customer of mine, about how it should be to implement my minimal WCF 4 Service using F#, and it turned out to be pretty straight forward. I think I’ll be trying to use more of F# later on.

Notice how I decided to use the F# function printfn to output text to the console instead of Console.WriteLine which I could have used if I’d liked to.

    1: open System
    2: open System.ServiceModel
    3: open System.ServiceModel.Description
    4:  
    5: [<ServiceContract>]
    6: type TimeService() =
    7:     [<OperationContract>]
    8:     member this.GetTime() =
    9:         DateTime.Now
   10:  
   11: let main = 
   12:     use host = new ServiceHost(typeof<TimeService>, new Uri("https://localhost:4711"))
   13:     host.Description.Behaviors.Add(new ServiceMetadataBehavior(HttpGetEnabled = true));
   14:     host.Open()
   15:  
   16:     printfn "Service is up'n running at %s" (host.Description.Endpoints.[0].Address.ToString())
   17:     printfn "Press any key to shut down service"
   18:     Console.ReadKey()