c#-使用WCF和F#在进程之间进行通信

我想使用F#在两个长时间运行的F#进程之间进行简单的一对一通信.他们将在每个ca定期交换信息. 5秒.到目前为止,我已经达到了这一点:#r System.ServiceModel#r System.Runtime.Serialization//#r @d:\DLL\Proto...

我想使用F#在两个长时间运行的F#进程之间进行简单的一对一通信.他们将在每个ca定期交换信息. 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是否实际使用它?

解决方法:

您需要在客户端和服务器上将绑定的MaxReceivedMessageSize属性从默认值65536增加到MaxReceivedMessageSize,以容纳要传输的数据量.

您可以使用Message Inspector检查WCF是否实际上在使用ProtoBuf序列化器(不是). ProtoBehavior似乎仅适用于指定了DataContract / ProtoContract属性的值.因此,在下面的修改示例中,我创建了一个Vector记录类型(也用F#3 CLIMutable属性标记)来包装数组:

#r "System.ServiceModel"
#r "System.Runtime.Serialization"
#r "protobuf-net.dll"
#time "on"

open System.ServiceModel
open System.Runtime.Serialization

[<DataContract; ProtoBuf.ProtoContract; CLIMutable>]
type Vector<'T> = { [<DataMember; ProtoBuf.ProtoMember(1)>] Values : 'T[] }

[<ServiceContract>]
type IService =
  [<OperationContract>]
  [<ProtoBuf.ServiceModel.ProtoBehavior>]
  abstract Test: Vector<Vector<Vector<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>)
  let binding = NetNamedPipeBinding()
  binding.MaxReceivedMessageSize <- binding.MaxReceivedMessageSize * 4L
  svh.AddServiceEndpoint (typeof<IService>, binding, "net.pipe://localhost/123") |> ignore
  svh.Open () )

server.IsBackground <- true
server.Start()

let scf: IService = 
   let binding = NetNamedPipeBinding()
   binding.MaxReceivedMessageSize <- binding.MaxReceivedMessageSize * 4L
   ChannelFactory.CreateChannel (binding, EndpointAddress "net.pipe://localhost/123")
let rnd = System.Random ()
let arr =
  { Values = Array.init 100 (fun i ->
   { Values =
      Array.init 10 (fun j ->
         { Values =Array.init 10 (fun k -> rnd.NextDouble()) }
      )}
   )}

printfn "%s" (scf.Test arr)

本文标题为:c#-使用WCF和F#在进程之间进行通信

基础教程推荐