无法从文件或字符串创建STREAM

时间:2016-07-26 19:34:48

标签: c# winforms stream amazon

我正在尝试从文件或字符串创建流,但我收到以下错误。

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

我找到了一个创建流的示例代码。看起来很好,但它给出了上面的错误

顺便说一句,我正在尝试将数据发送到亚马逊MWS Feed Api。以下是我的完整代码。

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;
 }

我做错了什么?我读了很多论坛,但我不明白。

它给出了下图中的错误

enter image description here

1 个答案:

答案 0 :(得分:0)

MemoryStream类不支持TimeOut。 你可以查看它检查属性CanTimeout。

bool canTimeout = stream.CanTimeout;

如果CanTimeout为false,则不允许调用属性ReadTimeout或WriteTimeout并抛出异常。

例如,

ReadTimeout可用于NetworkStream。