Skip to content

Instantly share code, notes, and snippets.

@giuliohome
Forked from dgfitch/simple_wcf.fs
Last active November 6, 2016 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giuliohome/1b87d0b4006c373c72c331c474423002 to your computer and use it in GitHub Desktop.
Save giuliohome/1b87d0b4006c373c72c331c474423002 to your computer and use it in GitHub Desktop.
A sample WCF host and client in F#, extremely simple but a starting point
open System
open System.ServiceModel
open System.ServiceModel.Description
[<ServiceContract(ConfigurationName = "PublishService", Namespace = "http://xyz.gov/PublishService")>]
[<ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)>]
type IPublishService =
[<OperationContract(Name="TestMethod")>]
abstract member TestMethod : name:string -> string
type PublishService() =
interface IPublishService with
member x.TestMethod (name:string) = "Hello " + name
end
let address = "http://localhost:4771"
let StartServer() =
use host = new ServiceHost(typeof<PublishService>, new Uri(address))
host.Description.Behaviors.Add(new ServiceMetadataBehavior(HttpGetEnabled = true));
// from admin cmd (see user from > net user
// > netsh http add urlacl url=http://+:4771/ user=MyHome
host.Open()
printfn "Service is running at %s" (host.Description.Endpoints.[0].Address.ToString())
printfn "Press Enter to shut down service"
Console.ReadLine() |> ignore
let RunClient() =
let endpoint = new ServiceEndpoint(
ContractDescription.GetContract(typeof<IPublishService>),
new BasicHttpBinding(),
new EndpointAddress(address))
let factory = new ChannelFactory<IPublishService>(endpoint)
let channel = factory.CreateChannel()
let result = channel.TestMethod("there")
printfn "Got result: %s" result
[<EntryPoint>]
let main argv =
match argv with
| argv when argv.Length > 0 ->
printfn "%A" argv
match argv.[0].ToLower() with
| "server" ->
StartServer()
| "client" ->
RunClient()
| _ ->
printfn "wrong parameter: only server/client"
0 // return an integer exit code
| _ ->
printf "no parameter: choose server/client"
-1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment