Stream.Length throws NotSupportedException

2019-01-22 18:40发布

I am getting a error when attempting stream.Length on a Stream object sent into my WCF method.

Unhandled Exception!
 Error ID: 0
 Error Code: Unknown
 Is Warning: False
 Type: System.NotSupportedException
 Stack:    at System.ServiceModel.Dispatcher.StreamFormatter.MessageBodyStream.get_Length()

How do you get the length of the stream? any examples?

6条回答
神经病院院长
2楼-- · 2019-01-22 18:53

You can't always get the length of a stream. In the case of a network stream, the only way of finding out the length is to read data from it until it's closed, for example.

What are you trying to do? Could you read from the stream until it's exhausted, copying the data into a MemoryStream as you go?

查看更多
手持菜刀,她持情操
3楼-- · 2019-01-22 18:55

I encountered this same problem when using WCF services. I needed to get the contents of a POST message, and was using a Stream argument in my method to get the contents of the message's body. Once I got the stream, I wanted to read its contents all at once, and needed to know what size byte array I would need. So, in the allocation of the array, I would call System.IO.Stream.Length and get the exception mentioned by the OP. Is the reason you need to know the length of the stream so that you can read the contents of the entire stream? You can actually read the entire contents of the stream into a string using System.IO.StreamReader. If you still need to know the size of your stream, you can get the length of the resulting string. Here's the code for how I solved this problem:

    [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. 
    }
查看更多
别忘想泡老子
4楼-- · 2019-01-22 18:58

It is not always possible to get the length of a stream if it doesn't support seeking. See the exception table on the Stream class.

For example, a stream hooked up to another process (network stream, standard output, etc) can produce any amount of output, depending on how the other process is written, and there's no way for the framework to figure out how much data there is.

In the general case, you just have to read in all of the data until the end of the stream and then figure out how much you've read.

查看更多
太酷不给撩
5楼-- · 2019-01-22 19:01

TcpClient.EndRead() should return the number of bytes that are in the stream.

--Edit, of course you need to be using a TCP Stream

查看更多
欢心
6楼-- · 2019-01-22 19:02

Stream.Length only works on Stream implementations where seeking is available. You can usually check to see if Stream.CanSeek is true. Many streams, since they're being streamed, are of a nature where it's impossible to know the length in advance.

If you must know the length, you may need to actually buffer the entire stream, loading it into memory in advance.

查看更多
SAY GOODBYE
7楼-- · 2019-01-22 19:10

This is what I do:

    // 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;
    }
查看更多
登录 后发表回答