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

yacmzcpb  于 2023-02-19  发布在  其他
关注(0)|答案(1)|浏览(168)

我试图从文件或字符串创建流,但我得到下面的错误。

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

我找到了一个创建流的示例代码。它看起来不错,但它给出了上面的错误
顺便说一下,我正在尝试发送数据到亚马逊MWS饲料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;
 }

我哪里做错了?
它给出了下图所示的错误:

bis0qfac

bis0qfac1#

MemoryStream类不支持TimeOut。您可以通过检查属性CanTimeout来检查该类。

bool canTimeout = stream.CanTimeout;

如果CanTimeout为false,则不允许调用ReadTimeout或WriteTimeout属性,并将引发异常。
例如,ReadTimeout可用于网络流。

相关问题