如何检查 Stream.Null? [英] How do I check for Stream.Null?

查看:37
本文介绍了如何检查 Stream.Null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WCF 服务,它返回一个类似于以下内容的 Stream:

public Stream StreamFile(string filepath){尝试{//从任何地方抓取文件//如果不存在则抛出异常返回文件流;}捕获(异常前){//在我的用户选择的地方很好地记录异常}返回 Stream.Null;}

我曾经尝试返回 null,但如果找不到文件,我开始遇到这个问题:WCF - MessageBodyMember - Stream - 值不能为空"

通过返回 Stream.Null,我摆脱了那个错误,但现在我遇到了另一个问题 - 我的客户如何知道我是否发回了 Stream.Null?我不能/不应该检查长度,因为这些文件可能很大,但即使不是,我也会面临这个问题:在 WCF 客户端中查找 Stream 对象的长度?

这是我的(大大简化的)客户端代码,仅供后人使用,但对我而言,仅下载 Stream.Null 的问题是我最终得到了一个空文件,而没有人喜欢这样.

public FileInfo RetrieveFile(string fileToStream, string directory){流报告流;string filePath = Path.Combine(directory, "file.txt");使用(流incomingStream = server.StreamFile(fileToStream)){if (incomingStream == null) throw new FileExistsException();//这完全不起作用使用 (FileStream OutgoingStream = File.Open(filePath, FileMode.Create, FileAccess.Write)){IncomingStream.CopyTo(outgoingStream);}}返回新的文件信息(文件路径);}

似乎我只是以某种方式设计了这个方法错误,但我想不出更好的方法来做到这一点,而不涉及抛出未捕获的异常.有什么建议吗?

解决方案

当在 WCF 服务(或任何设计良好的服务)中发生意外时,客户端无法/不应该知道细节或关心.它只需要知道出了什么问题.

您应该允许异常发生而不捕获它,并且客户端会知道发生了错误并且操作失败,而不管文件丢失、没有读取它的权限或文件在磁盘上损坏的事实.这些对客户来说都不重要,因为除了与服务过于耦合之外,我无法对信息做任何事情.

当然,在那种情况下,返回值是没有意义的,实际上代理会(通常)处于故障状态并且不再可用.(这取决于您的会话模式和实例生活方式).

请注意,如果您想让客户端知道故障并做出反应,您应该将这些故障设计为合同的一部分,并抛出一个 FaultException<> 包装您明确声明的合同异常在合同中.这将允许代理继续可用.您可以在此处

I have a WCF service which returns a Stream much like the following:

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;
}

I used to attempt to return null, but I began running into this issue if the file could not be found: WCF - MessageBodyMember - Stream - "Value cannot be null"

By returning Stream.Null, I've gotten rid of that error, but now I have another problem - how does my client know if I sent back Stream.Null? I can't/shouldn't check the length, because these files can be quite big, but even if they weren't, I would be faced with this problem: Find Length of Stream object in WCF Client?

Here's my (much-simplified) client code, just for posterity, but the issue for me with just downloading the Stream.Null is that I end up with an empty file, and nobody likes that.

public FileInfo RetrieveFile(string fileToStream, string directory)
{
    Stream reportStream;
    string filePath = Path.Combine(directory, "file.txt");
    using (Stream incomingStream = server.StreamFile(fileToStream))
    {
        if (incomingStream == null) throw new FileExistsException(); // Which totally doesn't work      
        using (FileStream outgoingStream = File.Open(filePath, FileMode.Create, FileAccess.Write))
        {
            incomingStream.CopyTo(outgoingStream);
        }
    }
    return new FileInfo(filePath);
}

It seems like I'm just designing this method wrong somehow, but I can't think of a better way to do it that doesn't involve throwing an uncaught exception. Any suggestions?

解决方案

When something unexpected happens in a WCF service (or any well designed service for that matter) the client cannot / should not know the details or care. All it needs to know it that something went wrong.

You should allow the exception to rise without catching it and the client would know that a Fault occurred and the operation failed, regardless to the fact that the file was missing, no permissions to read it or maybe the file is corrupt on disk. None of that matters to the client since i cannot do anything with the information anyway except from being too coupled to the service.

In that case of course, the return value is meaningless and in fact the proxy would (generally) be in a faulted state and would not be usable any more. (This depends on your session mode and instance lifestyle).

Note that if you wanted to let the client know about the faults and react, you should design those faults as part of the contract and throw a FaultException<> wrapping the contracted exception you explicitly declared in the contract. This would allow the proxy to continue to be usable. You can read more here

这篇关于如何检查 Stream.Null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆