views:

1690

answers:

5

Hi,

This webservice expects this xml file:

request.FeedContent = File.Open("test.xml", FileMode.Open, FileAccess.Read);

I already have the file in a stream, but this statement hangs:

stream.Position = 0;
request.FeedContent = stream;

the stream is a standard .net MemoryStream

what operation do I do on the stream to make it the same as File.Open?

Thanks!!

check this out (api definition):

    /// <summary>
    /// Gets and sets the FeedContent property.
    /// </summary>
    //[XmlElementAttribute(ElementName = "FeedContent")]
    public Stream FeedContent
    {
        get { return this.feedContentField ; }
        set { this.feedContentField= value; }
    }
A: 

File.Open returns a stream. Something else is the problem.

spender
+2  A: 

I doubt that the web service really expects a stream. How on earth would it represent that? Are you sure it doesn't just expect the contents as a byte array?

File.Open just returns a FileStream - if stream is already a FileStream then there's no difference between the two. It's just possible that it wants a FileStream whereas you've just got a Stream. If that's the case and it's really not from a file, you'll potentially have to write it to a file and open a FileStream to that. Then complain to the webservice developers that their API is bizarre.

EDIT: If it's only expecting a Stream, you should be fine. You say that it hangs - have you tried debugging and seeing where exactly it's hanging? Is it trying to read more data for some reason?

Jon Skeet
the stream is a memory stream-- I need the eqiv filestream: the web service is amazon merchant ws, and the 1st statement works--
Scott Kramer
also amazon mws is still in beta, but you should see their previous nightmare of an api
Scott Kramer
A: 

File.Open returns a FileStream that is open and positioned at the beginning of the stream.

The code you posted:

request.FeedContent = stream;

doesn't indicate the state of the stream you are assigning to FeedContent. E.g. maybe you aren't positioned at the start of the file? (use Stream.Seek if the stream supports seeking, which it will if it's a FileStream or MemoryStream).

Post more code if this doesn't help.

Joe
+1  A: 

Are you sure that the current position in your stream is the correct one?

The most common error i do when moving from a file stream to a memory stream is to miss that after writing to the memory stream i am at the end of it :

stream.Seek(0, SeekOrigin.Begin);
VirtualBlackFox
is this the same as : stream.Position = 0;
Scott Kramer
Yes, it should be.
VirtualBlackFox
+1  A: 

Have you tried putting the file content into a memory stream before assigning it? Maybe the service is using some stream features that your file stream doesn't support.

Something like this:

var stream = new MemoryStream(File.ReadAllBytes(fileName));
request.FeedContent = stream;

EDIT:

Wait... I totally misunderstood your question. So the version where you directly pass a FileStream works and if you provide a MemoryStream with the same content it doesn't?

Now I would suggest you to exactly compare the content of the memory stream with that of the file stream. Perhaps a different encoding?

VVS
I didnt downvote you :) -- this actually helped me debug
Scott Kramer
the ws accepts the stream this way, so something is wrong with the content
Scott Kramer