【问题标题】:Setting Maximum Size For ObjectContent Web API Preview 5设置 ObjectContent Web API Preview 5 的最大大小
【发布时间】:2023-03-10 22:07:01
【问题描述】:

当我使用 ObjectContent 对象创建 HttpContent 以通过 HttpClient 向 Web API 服务发送请求时,我收到以下错误:

无法向缓冲区写入超过配置的最大缓冲区大小的字节数:65536

以下代码用于发送请求。 Card 对象有大约 15 个属性。

var client = new HttpClient();
var content = new ObjectContent<IEnumerable<Card>>(cards, "application/xml");
MessageBox.Show(content.ReadAsString());  //This line gives me the same error.

var response = client.Post("http://localhost:9767/api/cards", content);

如何将配置的大小更改为大于 65,536 的值?

【问题讨论】:

    标签: asp.net-mvc-3 wcf-web-api


    【解决方案1】:

    关于这个有一个thread。尝试使用 HttpCompletionOption.ResponseContentRead:

    var message = new HttpRequestMessage(HttpMethod.Post, "http://localhost:9767/api/cards");
    message.Content = content; 
    var client = new HttpClient();
    client.Send(message, HttpCompletionOption.ResponseContentRead);
    

    【讨论】:

    • 如果我添加 MessageBox.Show(content.ReadAsString());紧跟在 var content = ... 行之后,它给了我同样的错误。
    • 我更新了答案。您可能需要查看源代码以了解其这样做的原因。
    【解决方案2】:

    为客户试试这个:

    HttpClient client = new HttpClient("http://localhost:52046/");
    
    // enable support for content up to 10 MB size
    HttpClientChannel channel = new HttpClientChannel() {
        MaxRequestContentBufferSize = 1024 * 1024 * 10 
    };
    
    client.Channel = channel;
    

    在服务器上(sn-p 是基于预览版 4,但你应该得到线索):

    public class CustomServiceHostFactory : HttpConfigurableServiceHostFactory {
        public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses) {
            var host = base.CreateServiceHost(constructorString, baseAddresses);
    
            foreach (HttpEndpoint endpoint in host.Description.Endpoints) {
                endpoint.TransferMode = TransferMode.Streamed;
                endpoint.MaxReceivedMessageSize = 1024 * 1024 * 10;
            }
    
            return host;
        }
    }
    

    【讨论】:

      【解决方案3】:

      由于问题在于 ReadAsString 扩展方法,我建议您创建自己的扩展方法来解决最大缓冲区大小问题。

      这是一个可能解决问题的 ReadAsLargeString 扩展方法示例。

      public static string ReadAsLargeString(this HttpContent content)
      {
          var bufferedContent = new MemoryStream();
          content.CopyTo(bufferedContent);
      
          if (bufferedContent.Length == 0)
          {
              return string.Empty;
          }
      
          Encoding encoding = DefaultStringEncoding;
          if ((content.Headers.ContentType != null) && (content.Headers.ContentType.CharSet != null))
          {
              encoding = Encoding.GetEncoding(content.Headers.ContentType.CharSet);
          }
      
          return encoding.GetString(bufferedContent.GetBuffer(), 0, (int)bufferedContent.Length);
      }
      

      【讨论】:

      • 我不太确定这有什么帮助。我将如何“告诉” HttpClient 使用此方法?
      • 当你实现了扩展方法后,试试 content.ReadAsLargeString()
      • HttpClient 的 post 方法接受一个 HttpContent 引用指针,而不是一个字符串。
      • 我很抱歉没有仔细阅读这个问题。你有没有尝试过? HttpClient 客户端 = new HttpClient(new HttpClientHandler { MaxRequestContentBufferSize = int.MaxValue }); client.MaxResponseContentBufferSize = int.MaxValue;
      猜你喜欢
      • 2018-06-27
      • 2011-08-11
      • 2021-07-02
      • 2020-08-28
      • 2013-10-05
      • 1970-01-01
      • 2021-01-13
      • 2021-07-16
      • 1970-01-01
      相关资源
      最近更新 更多