Search code examples
c#winformsstreamamazon-mws

Cannot create STREAM from file or string


I am trying to create a stream from a file or a string but I am getting the below error.

ReadTimeout = 'stream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'
WriteTimeout = 'stream.WriteTimeout' threw an exception of type 'System.InvalidOperationException'

I found a sample code to create a stream. It seems fine but it gives the error above

By the way, I am trying to send data to Amazon MWS Feed Api. Here is my full code below.

using (Stream s = GenerateStreamFromString("D:\\feed.xml"))
{
      SubmitFeedRequest request = new SubmitFeedRequest();
      request.Merchant = merchantId;
      request.MarketplaceIdList = new IdList();
      request.MarketplaceIdList.Id = new List<string>(new string[] { marketplaceId });
      request.FeedContent = s;
      request.ContentMD5 = MarketplaceWebServiceClient.CalculateContentMD5(request.FeedContent);
      //request.FeedContent.Position = 0;

      request.FeedType = "_POST_INVENTORY_AVAILABILITY_DATA_";

      SubmitFeedSample.InvokeSubmitFeed(service, request);
  }


 public static Stream GenerateStreamFromString(string s)
 {
     MemoryStream stream = new MemoryStream();
     StreamWriter writer = new StreamWriter(stream);
     writer.Write(s);
     writer.Flush();
     stream.Position = 0;
     return stream;
 }

What am I doing wrong?

It gives the error seen in the image below:

enter image description here


Solution

  • MemoryStream class does not support TimeOut. You can check it checking the property CanTimeout.

    bool canTimeout = stream.CanTimeout;
    

    If CanTimeout is false calling to properties ReadTimeout or WriteTimeout is not allowed and throws an exception.

    ReadTimeout can be used for NetworkStream for example.