使用流和关闭流时错误WebFaultException [英] Wrong WebFaultException when using a Stream and closing the stream

查看:836
本文介绍了使用流和关闭流时错误WebFaultException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个REST API,建立与WCF。

We have a REST API, built with WCF.

我们处理所有的异常后端的与WebFaultException是这样的:

We handle all the exceptions backend with WebFaultException like this:

throw new WebFaultException<string>(e.Message, HttpStatusCode.NotAcceptable);

这只是正常工作除了在一个场景中,我们做了后,甲流。

This works just fine except in one scenario where we do a Post, with a stream.

这样的一个例子:

[WebInvoke(Method = "POST", UriTemplate = "saveUser?sessionId={sessionId}&userId={userId}",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,            
        BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    [OperationContract]
    string SaveUser(string sessionId, int userId, Stream stream);

当在using语句,当我们再碰上个例外,我们不断收到处理此流:

When handling this stream in a using statement, whenever we then run into an exception we keep getting:

从菲德勒:

HTTP/1.1 400 Bad Request 
  <p>The server encountered an error processing the request. The exception message is 'The message object has been disposed.'. See server logs for more details. The exception stack trace is: </p>
  <p>  at System.ServiceModel.Channels.ByteStreamMessage.InternalByteStreamMessage.get_Properties()
   at System.ServiceModel.OperationContext.get_IncomingMessageProperties()
   at System.ServiceModel.Dispatcher.WebErrorHandler.ProvideFault(Exception error, MessageVersion version, Message&amp; fault)</p>

看起来它是与流和StreamReader的布置。

Looks like it has something to do with the stream and StreamReader being disposed.

我又试图删除任何东西,将处置的StreamReader,这acctualy作品。在code现在处理这看起来是这样的:

I have then tried to remove anything that will dispose the StreamReader, and this acctualy works. The code handling this now looks like this:

这解决了与发送正确的异常信息的问题,而是如何不好这会不会影响我们的应用程序,不关闭或处置我们的StreamReader? 你看到解决这个的其他方法吗?

This solves the problem with sending correct exception messages, but how bad will this affect our application, not closing or disposing our StreamReader? Do you see any other ways of solving this ?

推荐答案

这是因为StreamReader的接管流的所有权。换句话说,它使本身负责关闭源流。只要你的程序调用Dispose或关闭(保留在你的情况下,使用报表范围),那么它会处理的源数据也是如此。调用sr.Dispose()你的情况。因此,文件流死了之后。

This happens because the StreamReader takes over 'ownership' of the stream. In other words, it makes itself responsible for closing the source stream. As soon as your program calls Dispose or Close (leaving the using statement scope in your case) then it will dispose the source stream as well. Calling sr.Dispose() in your case. So the file stream is dead after.

如果你不希望这样,您可以创建一个新的类继承自的StreamReader并重写Close方法;您的关闭方法中,调用Dispose(假)不关闭流。

If you don't want this you could create a new class which inherits from StreamReader and override the Close method; inside your Close method, call Dispose(false) which does not close the stream.

您也可以使用 NonClosingStreamWrapper 类从乔恩斯基特的 MiscUtil 库,它提供完全的目的。

You could also use the NonClosingStreamWrapper class from Jon Skeet's MiscUtil library, it serves exactly that purpose.

不过,这将是最好不要离开的StreamReader不设置它,因为它无法清除所有非托管资源。

But it would be better to not leave the StreamReader without disposing it because it can't clean any unmanaged resources.

这篇关于使用流和关闭流时错误WebFaultException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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