我学习WCF。我有WCF服务和WCF客户端。我的服务获取一个文件,然后对其进行编辑并将其返回。它仍然没有任何问题。我将一个文件传递给服务并通过通常的字节数组返回:
/// <summary>
/// DWG file resaving.
/// </summary>
/// <param name="source">The byte array of the source DWG file.</param>
/// <param name="dwgVersion">DWG file target version (enum).</param>
/// <returns>The result (the byte array) of DWG file resaving.</returns>
[OperationContract]
byte[] SaveAs(byte[] source, DwgVersion dwgVersion);
通过通常的字节数组将文件传递给服务(并将其恢复)是否是可接受的做法,或者在这种情况下使用其他容器来传递文件会更好吗?
答案 0 :(得分:0)
或者更像是这样:
[ServiceContract]
public interface ITransferService
{
[OperationContract]
RemoteFileInfo DownloadFile(DownloadRequest request);
[OperationContract]
void UploadFile(RemoteFileInfo request);
}
[MessageContract]
public class DownloadRequest
{
[MessageBodyMember]
public string FileName;
}
[MessageContract]
public class RemoteFileInfo : IDisposable
{
[MessageHeader(MustUnderstand = true)]
public string FileName;
[MessageHeader(MustUnderstand = true)]
public long Length;
[MessageBodyMember(Order = 1)]
public System.IO.Stream FileByteStream;
public void Dispose()
{
if (FileByteStream != null)
{
FileByteStream.Close();
FileByteStream = null;
}
}
}