我有一个WCF服务,返回一个类似于以下内容:
public Stream StreamFile(string filepath)
{
try
{
// Grab the file from wherever it is
// Throw an exception if it doesn't exist
return fileStream;
}
catch (Exception ex)
{
// Log the exception nicely in a place of my user's choosing
}
return Stream.Null;
}
Run Code Online (Sandbox Code Playgroud)
我曾经尝试返回null,但是如果找不到该文件,我就开始遇到这个问题:WCF - MessageBodyMember - Stream - "Value不能为null"
通过返回Stream.Null,我已经摆脱了这个错误,但现在我有另一个问题 - 我的客户如何知道我是否发回了Stream.Null?我不能/不应该检查长度,因为这些文件可能非常大,但即使它们不是,我也会遇到这个问题:在WCF客户端中查找Stream对象的长度?
这是我的(非常简化的)客户端代码,仅供后人使用,但仅仅下载Stream.Null的问题是我最终得到一个空文件,没有人喜欢它.
public FileInfo RetrieveFile(string fileToStream, string directory)
{
Stream reportStream;
string filePath = Path.Combine(directory, "file.txt");
using (Stream incomingStream = server.StreamFile(fileToStream))
{ …Run Code Online (Sandbox Code Playgroud)