我希望使用F#实现两个长时间运行的进程之间的简单一对一通讯,并且它们将定期每5秒交换信息。目前我已经做到了这一点:
#r "System.ServiceModel"
#r "System.Runtime.Serialization"
//#r @"d:\DLL\Protobuf\protobuf-net.dll"
#time "on"
open System.ServiceModel
[<ServiceContract>]
type IService =
[<OperationContract>]
// [<ProtoBuf.ServiceModel.ProtoBehavior>]
abstract Test: float [] [] [] -> string
type Service () =
interface IService with
member o.Test data = sprintf "Hello, %A" data
let server = System.Threading.Thread (fun () ->
let svh = new ServiceHost (typeof<Service>)
svh.AddServiceEndpoint (typeof<IService>, NetNamedPipeBinding(), "net.pipe://localhost/123") |> ignore
svh.Open () )
server.IsBackground <- true
server.Start()
let scf: IService = ChannelFactory.CreateChannel (NetNamedPipeBinding(), EndpointAddress "net.pipe://localhost/123")
let rnd = System.Random ()
let arr =
Array.init 100 (fun i ->
Array.init 10 (fun j ->
Array.init 10 (fun k ->
rnd.NextDouble()
)))
printfn "%s" (scf.Test arr)
我遇到了许多不同的异常,主要是由于不同的WCF安全限制。
我的问题如下:
- 为使它正常工作需要最少需要做什么?
- 我编写的代码是否正确,以尽可能快地进行通信?
- 我已经尝试包含ProtoBuf序列化程序(请参见代码中的ProtoBehavior),以使序列化更快。我是否实现正确? 我怎样知道WCF是否真正使用它?