我有一个返回流对象的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();
答案 0 :(得分:1)
我认为问题可能是来自响应的Content-Length
标头。您将其设置为8192,而实际上您还不知道长度。
byteRead
)。
答案 1 :(得分:0)
这可能与您在服务器端关闭流的方式有关。我有一个类似于此的项目,其中客户端在异步调用中将流传递到服务器,并且.zip文件有时被破坏。事实证明,我没有正确关闭客户端的流,导致流不完整。
我没有看到显示服务关闭它在服务端发送的流的代码。
我认为除非关闭,否则流将无法正常完成。当没有任何东西要发送时,你仍然可以读取0个字节...