简体   繁体   English

从核心 API 调用 WCF 服务导致错误:缓冲 XML 内容所需的大小超出了缓冲区配额

[英]Calling WCF service from Core API results in error: The size necessary to buffer the XML content exceeded the buffer quota

We have an existing WCF Webservice used in a web application and for a third party to supply data I have created an API in dotnet core to pass on that information.我们有一个现有的 WCF Web 服务用于 web 应用程序,并且为了让第三方提供数据,我在 dotnet 核心中创建了一个 API 以传递该信息。

When I run the API I see that I get to the desired function in the webservice.当我运行 API 时,我看到我在 web 服务中到达了所需的 function。 The return value of the function is an XML. function 的返回值为 XML。

When the webservice returns the XML to the API I get an error in the API.当 Web 服务将 XML 返回到 API 时,我在 API 中收到错误消息。 I never get to see what the response is, it goes directly to an exception.我永远看不到响应是什么,它直接进入异常。

The error:错误:

The size necessary to buffer the XML content exceeded the buffer quota缓冲 XML 内容所需的大小超过了缓冲配额

This is the API method that calls the a private method to call the webservice:这是调用私有方法调用webservice的API方法:

 private async Task<string?> GetSessionIdAsync()
{
  try
  {
    var serviceClient = GetWsClient();

    string sessionUser = _config["Wmws:sessionUserName"];
    string sessionPassword = _config["Wmws:sessionPassWord"];

    var result = await serviceClient.GetSessionIdAsync(sessionUser, sessionPassword);

    return "";
  }
  catch (Exception ex)
  {
    _logger.Log(LogLevel.Information, "Webservice error: {@fout}", $"{ex.Message}");
    return null;
  }
}

And the method that actually makes the connection:以及实际建立连接的方法:

 private W2DWebservice_WM.W2DWebserviceClient GetWsClient()
{
  string username = _config["Wmws:UNT_UserName"];
  string password = _config["Wmws:UNT_PassWord"];
  string serviceClientEndpoint = _config["Wmws:UNT_clientEndpoint"];
  int quotaSize = 2147483647;

  // binding instellen
  WSHttpBinding binding = new WSHttpBinding
  {
    Security = new WSHttpSecurity()
    {
      Mode = SecurityMode.TransportWithMessageCredential,
      Transport = new HttpTransportSecurity()
      {
        ClientCredentialType = HttpClientCredentialType.Certificate
      },
      Message = new NonDualMessageSecurityOverHttp()
      {
        ClientCredentialType = MessageCredentialType.UserName,
        EstablishSecurityContext = false,
        NegotiateServiceCredential = false
      }
    },
    TextEncoding = System.Text.Encoding.UTF8,
    ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
    {
      MaxDepth = quotaSize,
      MaxStringContentLength = quotaSize,
      MaxArrayLength = quotaSize,
      MaxBytesPerRead = quotaSize,
      MaxNameTableCharCount = quotaSize
    }, 
    MaxBufferPoolSize = quotaSize, 
    MaxReceivedMessageSize = quotaSize
  };

  // Nieuwe binding maken en MTOM toe te kunnen voegen
  var messageEncodingBindingElementType = typeof(MessageEncodingBindingElement);
  var elements = binding.CreateBindingElements();

  IEnumerable<BindingElement> elementsWithoutEncodingElement = elements.Where(item => !messageEncodingBindingElementType.IsAssignableFrom(item.GetType()));
  var existingEncodingElement = (MessageEncodingBindingElement)elements.Where(item => messageEncodingBindingElementType.IsAssignableFrom(item.GetType())).First();

  var newEncodingElement = new MtomMessageEncoderBindingElement(existingEncodingElement);

  var customBinding = new CustomBinding(elementsWithoutEncodingElement.Prepend(newEncodingElement));

  EndpointAddress address = new EndpointAddress(serviceClientEndpoint);

  // serviceClient maken
  var serviceClient = new W2DWebservice_WM.W2DWebserviceClient(customBinding, address);
  serviceClient.ClientCredentials.UserName.UserName = username;
  serviceClient.ClientCredentials.UserName.Password = password;
  
  if (IsDevelopment)
  {
    // dit zou niet nodig moeten zijn als het op de server staat
    serviceClient.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByIssuerName, "TempCA");

    serviceClient.ClientCredentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication
    {
      CertificateValidationMode = X509CertificateValidationMode.None,
      TrustedStoreLocation = StoreLocation.LocalMachine,
      RevocationMode = X509RevocationMode.NoCheck
    };
  }

  return serviceClient;
}

This is the method in the webservice (in vb.net):这是 web 服务中的方法(在 vb.net 中):

 Public Function GetSessionId(username As String, password As String) As XmlDocument Implements IW2DWebservice.GetSessionId


   Dim xmldto As XmlDTO = Authorization("", "", "", "", username, password, "WEBSERVICE")


   Dim retXml As New XmlDocument
   retXml.LoadXml($"<sessionId>{xmldto.SessionID}</sessionId>")

   Return retXml
 End Function

The webservice is linked via connected services.网络服务通过连接的服务链接。

So like said when the webservice method returns to the API it goes straight to the catch.所以就像所说的,当 web 服务方法返回到 API 时,它会直接进入问题。

Why is this happening?为什么会这样?

What is returned from the webservice?从 Web 服务返回什么? Is it just the little XML or is it an entire soap message?它只是小 XML 还是整个 soap 消息? How can I read that?我怎么能读到呢?

Any help is appreciated.任何帮助表示赞赏。

We figured out that the problem has to do with using Mtom for the messageEconding.我们发现问题与使用 Mtom 进行 messageEconding 有关。 We couldn't switch the wcf to Text in stead of Mtom because it is an existing service so we ended up with adding a second endpoint to the webservice which uses Text for textencoding.我们无法将 wcf 切换为 Text 而不是 Mtom,因为它是现有服务,因此我们最终向使用 Text 进行文本编码的 Web 服务添加了第二个端点。

I used the WcfCoreMtomEncoderV2 package but it didn't work.我使用了 WcfCoreMtomEncoderV2 package 但它没有用。

So I didn't need to create the customBinding anymore.所以我不再需要创建 customBinding 了。 I just used the already created binding and changed the endpointaddress to the newly added endpoint on the webservice.我只是使用了已经创建的绑定并将端点地址更改为 web 服务上新添加的端点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 抛出FaultException时,WCF错误“缓冲XML内容所需的大小超过了缓冲区配额” - WCF error “The size necessary to buffer the XML content exceeded the buffer quota” when throwing FaultException 缓冲XML内容所需的大小超出了缓冲配额 - The size necessary to buffer the XML content exceeded the buffer quota WCF服务中的缓冲区大小 - Buffer size in WCF service 将Wcf服务发布到远程服务器后,缓冲区大小错误 - Buffer Size Error after publishing Wcf Service to a Remote Server “在读取XML数据时已超出最大字符串内容长度配额(8192)”将XML字符串发送到WCF时出错 - “The maximum string content length quota (8192) has been exceeded while reading XML data” error while sending XML string to WCF 将 XML 字符串发送到 WCF 时出现“读取 XML 数据时已超出最大字符串内容长度配额 (8192)”错误 - "The maximum string content length quota (8192) has been exceeded while reading XML data" error while sending XML string to WCF 文件上载错误WCF WEB API(预览6):无法向缓冲区写入比配置的最大缓冲区大小更多的字节:65536 - Error with a file upload WCF WEB API (Preview 6) : Cannot write more bytes to the buffer than the configured maximum buffer size: 65536 WCF-读取XML数据时超出了最大字符串内容长度配额(8192) - WCF - The maximum string content length quota (8192) has been exceeded while reading XML data WCF错误:System.ServiceModel.CommunicationException已超过传入消息的最大消息大小配额 - WCF error: System.ServiceModel.CommunicationException the maximum message size quota for incoming messages has been exceeded WCF REST抛出HTTP 400错误maxstringcontentlength配额(8192) - WCF REST throws HTTP 400 error maxstringcontentlength quota (8192) exceeded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM