c# - WCF接口服务契约实例直接调用函数
问题描述
我有一个类(名为 Sender),它有一个定义 WCF 服务合同的 Icommunicator 接口成员。
public class Sender
{
private ICommunicator channel;
private ChannelFactory<ICommunicator> factory = null;
在同一个 .cs 文件中,有一个class Receiver
继承自 Icommunicator 的文件。
定义服务契约的接口如下:
[ServiceContract(Namespace = "P2PComm")]
public interface ICommunicator
{
[OperationContract(IsOneWay = true)]
void PostMessage(CommMessage msg);
// used only locally so not exposed as a service method
CommMessage GetMessage();
[OperationContract]
bool openFileForWrite(string fileName);
[OperationContract]
bool writeFileBlock(byte[] block);
[OperationContract]
void closeFile();
}
在Sender
类内部有一个连接函数,它Icommunicator channel
直接使用该成员来调用该PostMessage
函数。
// attempts to connect to Receiver instance
// attempts a finite number of times to connect to a Receiver
// first attempt to send will throw exception of no listener
// at the specified endpoint
// to test that we attempts to send a connect message
public bool connect(string toAddress)
{
int timeToSleep = 500;
CreateSendChannel(toAddress);
CommMessage connectMsg = new CommMessage(CommMessage.MessageType.connect);
connectMsg.to = toAddress;
connectMsg.from = fromAddress;
connectMsg.command = Msg.Command.connect;
while (true)
{
try
{
channel.PostMessage(connectMsg);
tryCount = 0;
lastUrl = toAddress;
return true;
}
catch (Exception ex)
{
if (++tryCount < maxCount)
{
Thread.Sleep(timeToSleep);
}
else
{
lastError = ex.Message;
lastUrl = "";
tryCount = 0;
return false;
}
}
}
}
我不明白这里发生了什么我不是从Icommunicator
接口继承的,而是我用它来调用它的一个函数,那么在这种情况下调用哪个函数?
当我用调试器运行它时,它没有进入函数,但它仍然能够发送消息。
解决方案
推荐阅读
- c# - C# 事件订阅者排队
- pandas - 如何将 Keras 输入的形状纠正为 3D 数组
- c - 为什么 FREAD 在 Linux 上给出错误,但在 Windows 上却没有?
- android - 可扩展的 Recyclerview Android
- python-3.x - 电报错误 401:通过电报 cli 注册后的 USER_DEACTIVATED
- c - “make clean”的任务配置在 Visual Studio 2017 中不起作用
- angularjs - I have issue in creating a module in Angular
- spring-boot - 停止 spring 应用程序上下文时 management.server.port 不关闭
- javascript - Javascript添加运费
- php - insert multiple checkbox value using jquery and ajax but i only insert one record