Created
November 3, 2010 20:31
-
-
Save dgfitch/661656 to your computer and use it in GitHub Desktop.
A sample WCF host and client in F#, extremely simple but a starting point
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice work! Remember that
host.Open()
requires admin permissions or - alternatively - you have to reserve the URL for the script's user from an eleveted cmd by configuringnetsh http add urlacl url=http://+:4771/ user=domain\user