我想实现一个WCF服务,允许两个节点以对称的方式在两个方向上进行通信。特别是,这里有要求:
因此,我认为我应该使用NetTcpBinding,并使用继承为服务和回调使用相同的接口。
interface ICommonInterface
{
void Request(string dataRequest);
void Reply(string dataReply);
void Disconnect(); // both nodes must be able to close the TCP connection
}
[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IMyServiceCallback))]
interface IMyService : ICommonInterface
{
[OperationContract(IsOneWay = true, IsInitiating = true, IsTerminating = false)]
void Connect();
[OperationContract(IsOneWay = true, IsInitiating = false, IsTerminating = false)]
void Request(string dataRequest);
[OperationContract(IsOneWay = true, IsInitiating = false, IsTerminating = false)]
void Reply(string dataReply);
[OperationContract(IsOneWay = true, IsInitiating = false, IsTerminating = true)]
void Disconnect();
}
interface IMyServiceCallback : ICommonInterface
{
[OperationContract(IsOneWay = true, IsInitiating = false, IsTerminating = false)]
void Request(string dataRequest);
[OperationContract(IsOneWay = true, IsInitiating = false, IsTerminating = false)]
void Reply(string dataReply);
[OperationContract(IsOneWay = true, IsInitiating = false, IsTerminating = true)]
void Disconnect();
}
这种实施能否奏效? 还有更好的选择吗?
答案 0 :(得分:0)
MSDN有一个很好的异步TCP教程: http://msdn.microsoft.com/en-us/library/5w7b7x5f(v=vs.71).aspx
如果您了解回调和基本线程,那将非常简单。