代码之家  ›  专栏  ›  技术社区  ›  anelson

为什么WCF在Close()上读取EOF的输入流?

  •  5
  • anelson  · 技术社区  · 16 年前

    [ServiceContract, XmlSerializerFormat]
    public interface IReplicationWebService
    {
        [OperationContract]
        [WebInvoke(Method = "PUT", UriTemplate = "agents/{sourceName}/epoch/{guid}/{number}/{type}")]
        ReplayResult PutEpochFile(string sourceName, string guid, string number, string type, Stream stream);
    }
    

    在执行本合同时,我们从以下来源读取数据 stream

        public ReplayResult PutEpochFile(string sourceName, string guid, string number, string type, Stream inStream)
        {
            //Stuff snipped
            try
            {
                //Read from the stream and write to the file
            }
            catch (IOException ioe)
            {
                //IOException may mean no disk space
                try
                {
                    inStream.Close();
                }
                // if instream caused the IOException, close may throw
                catch
                {
                }
                _logger.Debug(ioe.ToString());
                throw new FaultException<IOException>(ioe, new FaultReason(ioe.Message), new FaultCode("IO"));
            }
        }
    

    为了测试这一点,我将向没有足够空间的服务器发送一个100GB的文件。正如预期的那样,这会抛出一个异常,但调用 inStream.Close() Close() System.ServiceModel.Channels.DrainOnCloseStream.Close() Byte[]

    Close

    在这条小溪上。如果是这样的话,我想解释一下原因。但更重要的是,如果有人能向我解释原因,我将不胜感激

    1 回复  |  直到 16 年前
        1
  •  4
  •   marc_s MisterSmith    16 年前

    .Close()

    如果你想扔下大锤,请使用 .Abort() 在您的客户端代理(或服务主机)上。这只是在没有检查的情况下关闭了所有东西,也没有很好地等待操作完成。