Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法在客户端的循环中调用wcf nettcp操作,该操作返回字节数组?_C#_Wcf - Fatal编程技术网

C# 无法在客户端的循环中调用wcf nettcp操作,该操作返回字节数组?

C# 无法在客户端的循环中调用wcf nettcp操作,该操作返回字节数组?,c#,wcf,C#,Wcf,我尝试了所有可能的方法来解决我的问题。我会解释我的问题 我正在尝试使用WCF NetTCP服务创建文件传输机制。客户端可以从另一台计算机上运行的服务请求文件。文件传输分三个阶段进行 BeginFileTranfer-服务器端的操作,将打开文件并准备传输,这将启动会话 [OperationContract(IsInitiating = true, IsTerminating = false)] string BeginFileTransfer(TrnsferType oType, string s

我尝试了所有可能的方法来解决我的问题。我会解释我的问题

我正在尝试使用WCF NetTCP服务创建文件传输机制。客户端可以从另一台计算机上运行的服务请求文件。文件传输分三个阶段进行

BeginFileTranfer-服务器端的操作,将打开文件并准备传输,这将启动会话

[OperationContract(IsInitiating = true, IsTerminating = false)]
string BeginFileTransfer(TrnsferType oType, string strFileName,string strFilePath);
GetFileData-服务器端的操作,将从打开的文件中每次调用发送1024(暂时)字节

[OperationContract(IsInitiating = false, IsTerminating = false)]
CFileTransferData GetFileData(string strRequestId);
EndFileTransfer-服务器端的操作将关闭该文件。将终止会话

[OperationContract(IsInitiating = false, IsTerminating = true)]
bool EndFileTranser(string strRequestId);
在客户端,我调用函数从远程服务获取文件

private void btnGet_Click(object sender, EventArgs e)
{

    string strId = _AgentService.BeginFileTransfer(iVayagerAgent.TrnsferType.GET,"contacts.csv","D:");
    iVayagerAgent.CFileTransferData oData = null;
    FileStream oFile = null;
    do
    {
         oData = _AgentService.GetFileData(strId);
         if (oData.State == iVayagerAgent.TransferState.OPEN)
         {
             oFile = File.Create("C:\\123\\contacts.csv");
             oFile.Write(oData.Data, 0, oData.Data.Length);
         }
         else if (oData.State == iVayagerAgent.TransferState.PENDING)
         {
             oFile.Write(oData.Data, 0, oData.Data.Length);
         }
         else
         {
             oFile.Close();
         }
    }while(oData.State != iVayagerAgent.TransferState.CLOSE);
}
iVayagerAgent.CFileTransferData

是用于将文件数据发送到客户端的类

[DataContract]
public enum TransferState : int
{
    [EnumMember]
    OPEN = 0,
    PENDING = 1,
    CLOSE = 2
}
[DataContract]
public class CFileTransferData
{
    [DataMember]
    public TransferState State{get; set;}
    [DataMember]
    public byte[] Data;
    [DataMember]
    public string Status;
    [DataMember]
    public string StatusDescription;
}
在循环中调用GetFileData时出现问题。第一次调用GetFileData就可以了。下一次后续呼叫出现以下错误

套接字连接已中止。这可能是由于处理消息时出错、远程主机超过接收超时或基础网络资源问题造成的。本地套接字超时为“00:00:59.9679982”

这仅在发送包含数据的字节数组时发生。如果我在那个数组中不发送任何内容,它就可以正常工作。 如果你能指出一些我可以研究的领域,那就太好了。我还将显示我的配置文件

服务器配置


请帮我找出哪里出了问题。提前感谢…

特别感谢asafrob为帮助我所做的努力,也感谢我的朋友Roshen。在做了几天的编码之后,我将能够找出哪里出了问题。最后,它发生在[EnumMembers]

[DataContract]
public enum TransferState : int
{
    [EnumMember]
    OPEN = 0,
    PENDING = 1,
    CLOSE = 2
}
如果我们在响应中使用挂起和关闭枚举成员,则此声明是错误的。因为我们没有提到挂起和关闭成员作为[EnumMembers],所以在序列化过程中,它将退出并抛出异常。如果您正在使用任何成员,您必须声明它。这将使序列化过程更加顺利

这是正确的格式。。。(这都是因为缺乏基础:D)

在我的问题中,我讲述了这个场景。现在我可以解释为什么它会这样。 在循环中调用GetFileData时出现问题。第一次调用GetFileData就可以了。下一次后续呼叫出现以下错误

它在第一次调用中工作的原因是,OPEN enum我提到了属性[EnumMember] 所以它可以正常地序列化并正常工作,下一次调用我将响应状态设置为挂起。这会导致序列化问题

所以,非常感谢你们的支持和这次会议。希望这将帮助其他许多人


-Venura

您是立即得到错误,还是等待几秒钟直到它发生?您是否调试了服务器以确保服务器端没有任何错误?谢谢您的回复。第二次调用GetFileData时出现Yes错误。服务器端没有问题。我正在接收来自服务器端的第二个呼叫,它会响应。我一直监视到操作的返回语句。一旦执行了return,它将进入客户端。请尝试增加
MaxBytesPerRead
。如果有效,请向我们展示您构建响应的代码。感谢您的帮助和响应。它不起作用了。同样的错误。在第二次调用GetFileData之后。我将在我的原始文章中显示代码,最后您可以参考它。因为无法添加注释。如果使用较小的缓冲区大小(例如256),它会改变什么吗?
EndpointAddress oEndPointAddress = new EndpointAddress("net.tcp://" + tbIP.Text + ":" + tbPort.Text + "/IiVoyagerAgentService");

NetTcpBinding oBinding = new NetTcpBinding();
oBinding.Name = "iVoyagerAgentServiceBinding";
XmlDictionaryReaderQuotas myReaderQuotas = new XmlDictionaryReaderQuotas();
myReaderQuotas.MaxStringContentLength = 5242880;
myReaderQuotas.MaxArrayLength = 2147483646;
myReaderQuotas.MaxBytesPerRead = 4096;
myReaderQuotas.MaxDepth = 32;
myReaderQuotas.MaxNameTableCharCount = 5242880;

oBinding.GetType().GetProperty("ReaderQuotas").SetValue(oBinding, myReaderQuotas, null);


oBinding.Security.Mode = SecurityMode.None;
oBinding.ReceiveTimeout = new TimeSpan(0,10,0);
_ChannelFactory = new ChannelFactory<iVayagerAgent.IiVoyagerAgentService>(oBinding, oEndPointAddress);


_ChannelFactory.Opened += new EventHandler(_ChannelFactory_Opened);
_ChannelFactory.Closed += new EventHandler(_ChannelFactory_Closed);

_ChannelFactory.Open();


_AgentService= _ChannelFactory.CreateChannel();
[DataContract]
public enum TransferState : int
{
    [EnumMember]
    OPEN = 0,
    PENDING = 1,
    CLOSE = 2
}
[DataContract]
public class CFileTransferData
{
    [DataMember]
    public TransferState State{get; set;}
    [DataMember]
    public byte[] Data;
    [DataMember]
    public string Status;
    [DataMember]
    public string StatusDescription;
}

public CFileTransferData Get()
{
    int  bNum = 0;
    byte[] bData = new byte[BufferSize];
    CFileTransferData oData = new CFileTransferData();
    oData.Status = "1";
    oData.StatusDescription = "Success"; 

    try
    {
          if(Type == TrnsferType.GET)
          {
              bNum = File.Read(bData, 0, (Int32)BufferSize);
              if (BytesRead == 0)
              {
                  oData.State = TransferState.OPEN;
              }
              else
              {
                  if (bNum != 0)
                  {
                      oData.State = TransferState.PENDING;
                  }
                  else
                  {
                      oData.State = TransferState.CLOSE;
                  }
              }
              oData.Data = bData;
              BytesRead += bNum;
              BytesToRead -= bNum;
          }
          else
          {
              oData.Status = "0";
              oData.StatusDescription = "Invalid Transfer Type";
          }
    }
    catch
    {
        oData.Status = "0";
        oData.StatusDescription = "Critical Error";
    }
    return oData;
}
[DataContract]
public enum TransferState : int
{
    [EnumMember]
    OPEN = 0,
    PENDING = 1,
    CLOSE = 2
}
[DataContract]
public enum TransferState : int
{
    [EnumMember]
    OPEN = 0,
    [EnumMember]
    PENDING = 1,
    [EnumMember]
    CLOSE = 2
}