Search code examples
wcff#f#-interactive

F# interactive vs. F# solution and WCF


Try to run this in F# interactive:

#r "System.ServiceModel"
#r "System.Runtime.Serialization"

open System.ServiceModel

[<ServiceContract>]
type IWCF =
  [<OperationContract>]
  abstract Ping: float -> unit

type WCF () =
  interface IWCF with
    member o.Ping a = printfn "Hello, %g" a

let svh = new ServiceHost (typeof<WCF>)

You will probably succeed. Try to make a new solution.

Reference:

  • System.Runtime.Serialization
  • System.ServiceModel

Paste the following code into Program.fs:

open System.ServiceModel

[<ServiceContract>]
type IWCF =
  [<OperationContract>]
  abstract Ping: float -> unit

type WCF () =
  interface IWCF with
    member o.Ping a = printfn "Hello, %g" a

let svh = new ServiceHost (typeof<WCF>)

And run it. I get the following error:

All parameter names used in operations that make up a service contract must not be null. Parameter name: name

What is wrong?

PS: I use Visual Studio 2010 Ultimate SP1

EDIT: just to make sure, the C# equivalent works fine


Solution

  • The problem is indeed that you need to have names for the parameters in WCF-Operations.

    Here is a solution to get named parameters in there (named it a just like you did) - as to why it is working in F#-Interactive? No clue, maybe it puts some standardnames for parameters in there. The syntax is slightly strange but you can define names for the parameters in F#, try:

    [<ServiceContract>]
    type IWCF =
      [<OperationContract>]
      abstract member Ping: a:float -> unit
    

    NOTE: I don't know if you need the member in there but I just checked some of my files and did put it in there. I have no compiler around ATM so I will let it sit there in case you really need it (but I don't think so)