【问题标题】:WCF service stream byte array size issueWCF 服务流字节数组大小问题
【发布时间】:2010-08-31 15:49:56
【问题描述】:

我有一个返回流对象的 WCF 服务。但由于某种原因,我得到了一个损坏的 zip 文件,我正在流式传输。所有代码都在下面请指教

合同代码

[ServiceContract(Namespace = "http://schemas.acme.it/2009/04/01")]
public interface IFileTransferService
{
    [OperationContract(IsOneWay = false)]
    FileDownloadReturnMessage DownloadFile(FileDownloadMessage request);

    [OperationContract()]
    string HellowWorld(string name);

}

[MessageContract]
public class FileDownloadMessage
{
    [MessageHeader(MustUnderstand = true)]
    public FileMetaData FileMetaData;
}

[MessageContract]
public class FileDownloadReturnMessage
{
    public FileDownloadReturnMessage(FileMetaData metaData, Stream stream)
    {
        this.DownloadedFileMetadata = metaData;
        this.FileByteStream = stream;
    }

    [MessageHeader(MustUnderstand = true)]
    public FileMetaData DownloadedFileMetadata;
    [MessageBodyMember(Order = 1)]
    public Stream FileByteStream;
}


[DataContract(Namespace = "http://schemas.acme.it/2009/04/01")]
public class FileMetaData
{
    public FileMetaData(string [] productIDs, string authenticationKey)
    {
        this.ids = productIDs;
     this.authenticationKey= authenticationKey;
    }

    [DataMember(Name = "ProductIDsArray", Order = 1, IsRequired = true)]
    public string[] ids;
    [DataMember(Name = "AuthenticationKey", Order = 2, IsRequired = true)]
    public string authenticationKey;
}

SVC 文件代码

 public class DownloadCoverScan : IFileTransferService
    {
        public FileDownloadReturnMessage DownloadFile(FileDownloadMessage request)
        {
            FileStream stream = new FileStream(@"C:\Pictures.zip", FileMode.Open, FileAccess.Read);
            FileMetaData metaData= new FileMetaData(new string[] { "1", "2" },"asd");
            FileDownloadReturnMessage returnMessage = new FileDownloadReturnMessage(metaData,stream);
            return returnMessage;
        }
        public string HellowWorld(string name)
        {
            return "Hello " + name;
        }

    }

配置代码

 <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="DownloadCoverScanBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true" httpHelpPageEnabled="true" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="DownloadCoverScanBehavior" name="DownloadService.DownloadCoverScan">
        <endpoint address="" name="basicHttpStream" binding="basicHttpBinding" bindingConfiguration="httpLargeMessageStream"
                  contract="DownloadService.IFileTransferService" />
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="httpLargeMessageStream" maxReceivedMessageSize="2147483647" transferMode="Streamed"  messageEncoding="Mtom" />
      </basicHttpBinding>
    </bindings>
  </system.serviceModel>

客户代码

        FileMetaData metaData = new FileMetaData();
        metaData.ProductIDsArray = new string[] { "1", "2" };
        metaData.AuthenticationKey = "test";
        FileDownloadMessage inputParam = new FileDownloadMessage(metaData);
        FileTransferServiceClient obj = new FileTransferServiceClient();
        FileDownloadReturnMessage outputMessage = obj.DownloadFile(inputParam);
        Byte[] buffer = new Byte[8192];
        int byteRead = outputMessage.FileByteStream.Read(buffer, 0, buffer.Length);
        Response.Buffer = false;
        Response.ContentType = "application/zip";
        Response.AppendHeader("content-length", buffer.Length.ToString());
        Response.AddHeader("Content-disposition", "attachment; filename=testFile.zip");
        Stream outStream = Response.OutputStream;
        while (byteRead > 0)
        {
            outStream.Write(buffer, 0, byteRead);
            byteRead = outputMessage.FileByteStream.Read(buffer, 0, buffer.Length);
        }
        outputMessage.FileByteStream.Close();
        outStream.Close();

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    缓冲区大小无关紧要,只要选择一个合理的大小,例如:

    Byte[] buffer = new Byte[8192];
    

    你的代码也有错误,你写的是缓冲区的内容:

    outStream.Write(buffer, 0, buffer.Length);
    

    应该是:

    outStream.Write(buffer, 0, byteRead);
    

    否则您将始终写入整个缓冲区,即使它只是部分填充了数据。

    【讨论】:

    • 感谢您的回复,我已按照建议进行了更改,并且我可以看到我可以下载我的 zip 文件,但是当我解压缩它时出现错误。所以不确定错误出在WCF服务还是客户端。我已经用所有代码更新了我的问题。请指教
    • @Amit:问题在于您将内容长度设置为缓冲区大小。如果你能找到实际的流大小,你可以使用它,否则你必须跳过 content-length 属性。
    猜你喜欢
    • 2011-07-30
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    相关资源
    最近更新 更多