遅い→起動時

http://d.hatena.ne.jp/pmint/

WCFの名前付きパイプで双方向通信

WCFのサンプルは構成ファイル(Web.configやApp.config)を用意したり、HTTPの例だったりで名前付きパイプ(NetNamedPipeBinding)のいい例がない。
同一端末上での通信だからエンドポイントの事前公開なんて要らないはずなのに、どの例もエンドポイントの公開が入っていたり。双方向通信(全二重伝送?)の例も少ないし。あったとしてもコールバックを使うもので互いに自由なタイミングで送受信する例ではないし。


それで試行錯誤でサンプル作ってみた。

IDuplexSessionChannel サンプル(VB)
IDuplexSessionChannel1.zip


これはただ動くだけのレベル。ChannelListener - Channel - ChannelFactory を使った方式。タイムアウトありのChannel.Send()とChannel.Receive()で送受信。ServiceHostは使ってない。


複数のクライアントと一対多で送受信できるよう通信は2段階に分けた。

  1. 特定のエンドポイント(net.pipe://localhost/IDuplexSessionChannel1/ConnectionInfo)を使って動的に作ったエンドポイントを共有
  2. 前の通信で取得したエンドポイントで通信
    例えば net.pipe://localhost/IDuplexSessionChannel1/44e0b45a-d29e-4c03-ba44-d5853755f1f9 などの訳の分からないIDが付いたアドレスの。




実行するとこんな出力に。

【ホスト側コンソール出力】

connectionChannel.Send()
connectionChannel.Sent: net.pipe://localhost/IDuplexSessionChannel1/5a2fd481-b9d9-4997-887f-ebb921a27e9e
Start Transport: net.pipe://localhost/IDuplexSessionChannel1/5a2fd481-b9d9-4997-887f-ebb921a27e9e
transportChannel.Send1()
transportChannel.Sent1: >
transportChannel.Receive1()
transportChannel.Received1: MESSAGE
transportChannel.Send2()
transportChannel.Sent2: Re:MESSAGE
End Transport: net.pipe://localhost/IDuplexSessionChannel1/5a2fd481-b9d9-4997-887f-ebb921a27e9e
【クライアント側コンソール出力】

factory: Opened
connectionChannel: Opened
connectionChannel.Received: net.pipe://localhost/IDuplexSessionChannel1/5a2fd481-b9d9-4997-887f-ebb921a27e9e
transportChannel: Opened
transportChannel.Received1: >
transportChannel.Sent1: MESSAGE
transportChannel.Received2: Re:MESSAGE

Press <ENTER> to terminate the app.


ホスト側は無限ループするのでCtrl+Cで止めてね。
あとMSDNのサンプルと違って管理者権限は要らないので、一般ユーザーのままでおk。