Nev*_*hai 29 .net c# stream notsupportedexception
我在尝试stream.Length发送到我的WCF方法的Stream对象时收到错误.
Unhandled Exception!
Error ID: 0
Error Code: Unknown
Is Warning: False
Type: System.NotSupportedException
Stack: at System.ServiceModel.Dispatcher.StreamFormatter.MessageBodyStream.get_Length()
Run Code Online (Sandbox Code Playgroud)
你如何得到流的长度?任何例子?
Ree*_*sey 31
Stream.Length仅适用于可以进行搜索的Stream实现.您通常可以检查Stream.CanSeek是否为真.许多流,因为它们正在流式传输,其性质是不可能提前知道长度的.
如果您必须知道长度,则可能需要实际缓冲整个流,并提前将其加载到内存中.
fro*_*rog 11
我在使用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.
}
Run Code Online (Sandbox Code Playgroud)
这就是我做的:
// Return the length of a stream that does not have a usable Length property
public static long GetStreamLength(Stream stream)
{
long originalPosition = 0;
long totalBytesRead = 0;
if (stream.CanSeek)
{
originalPosition = stream.Position;
stream.Position = 0;
}
try
{
byte[] readBuffer = new byte[4096];
int bytesRead;
while ((bytesRead = stream.Read(readBuffer, 0, 4096)) > 0)
{
totalBytesRead += bytesRead;
}
}
finally
{
if (stream.CanSeek)
{
stream.Position = originalPosition;
}
}
return totalBytesRead;
}
Run Code Online (Sandbox Code Playgroud)
您无法始终获得流的长度.例如,在网络流的情况下,找出长度的唯一方法是从中读取数据直到它被关闭.
你想做什么?您是否可以从流中读取,直到它耗尽,然后将数据复制到一个流程中MemoryStream?
| 归档时间: |
|
| 查看次数: |
42099 次 |
| 最近记录: |