简体   繁体   English

我如何检查Stream.Null?

[英]How do I check for Stream.Null?

I have a WCF service which returns a Stream much like the following: 我有一个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;
}

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" 我曾经尝试返回null,但是如果找不到该文件,我就开始遇到这个问题: WCF - MessageBodyMember - Stream - “Value不能为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? 通过返回Stream.Null,我已经摆脱了这个错误,但现在我有另一个问题 - 我的客户如何知道我是否发回了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? 我不能/不应该检查长度,因为这些文件可能非常大,但即使它们不是,我也会遇到这个问题: 在WCF客户端中查找Stream对象的长度?

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. 这是我的(非常简化的)客户端代码,仅供后人使用,但仅仅下载Stream.Null的问题是我最终得到一个空文件,没有人喜欢它。

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. 当WCF服务(或任何设计良好的服务)发生意外情况时,客户端不能/不应该知道细节或关心。 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. 请注意,如果您想让客户了解故障并做出反应,您应该将这些故障设计为合同的一部分,并抛出FaultException<>包装您在合同中明确声明的合同FaultException<>异常。 This would allow the proxy to continue to be usable. 这将允许代理继续可用。 You can read more here 你可以在这里阅读更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM