我想使用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#在进程之间进行通信
基础教程推荐
- 快速了解如何在.NETCORE中使用Generic-Host建立主机 2023-02-17
- C#中Winform 实现Ajax效果自定义按钮 2022-12-01
- C#语言主要语言区域 2023-05-11
- Unity3D实现扭动挤压浏览效果 2023-01-16
- Unity Shader实现径向模糊效果 2023-04-27
- Unity实现Flappy Bird游戏开发实战 2023-01-06
- Unity3D撤回命令开发详解 2023-01-16
- c# – 当使用MVC应用程序和针对MySQL的MS Access时,记录被覆盖 2023-11-26
- Unity实现切割图集工具 2023-04-21
- C# 使用AspriseOCR.dll实现验证码识别 2023-02-09