我使用WCF服務時遇到了同樣的問題。我需要獲取POST消息的內容,並在我的方法中使用Stream參數來獲取消息正文的內容。一旦我得到流,我想一次讀取它的內容,並且需要知道我需要什麼大小的字節數組。所以,在分配數組時,我會調用System.IO.Stream.Length並獲取OP提到的異常。您是否需要知道流的長度,以便可以讀取整個流的內容? 實際上,您可以使用System.IO.StreamReader將流的全部內容讀取到字符串中。如果你仍然需要知道你的流的大小,你可以得到結果字符串的長度。下面是我如何解決這個問題的代碼:
[OperationContract]
[WebInvoke(UriTemplate = "authorization")]
public Stream authorization(Stream body)
{
// Obtain the token from the body
StreamReader bodyReader = new StreamReader(body);
string bodyString= bodyReader.ReadToEnd();
int length=bodyString.Length; // (If you still need this.)
// Do whatever you want to do with the body contents here.
}
Stream類是抽象的,什麼是您所使用的派生Stream類?另外,如果可能,發佈導致問題的代碼? – 2010-07-30 16:46:34