格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出错: GetLzdtArticleResult。InnerException 消息是“反序

本文介绍了解决WCF服务中因字符串长度过大导致的反序列化错误的方法。通过正确配置服务端绑定属性,成功解决了超出最大字符串内容长度配额的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当遇到这个错误的时候郁闷了好长时间报错是字符串长度过大可是修改了MaxStringContentLength”属性的值却不起作用最后才发现还是因为配置文件配置的问题在服务端

格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出错: GetLzdtArticleResult。InnerException 消息是“反序列化对象 属于类型 lzdt.DTO.Dtolzdt[] 时出现错误。读取 XML 数据时,超出最大字符串内容长度配额 (8192)。通过更改在创建 XML 读取器时所使用的 XmlDictionaryReaderQuotas 对象的 MaxStringContentLength 属性,可增加此配额

尽管我设置了“MaxStringContentLength”属性的值,但有这个错误...后来发现在服务端终结点配置上必须增加绑定配置属性 bindingConfiguration="BasicHttpBinding_NewBind"才起作用 ,恍然大悟,记录博客备忘决不允许再犯这样低级的错误!!!!!!!!

<services>
      <service name="Gps.OpenAPI.WebService.GpsWebService"  behaviorConfiguration="liuyaokai_behavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:13700/GpsWebService.svc"/>
          </baseAddresses>
        </host>
        <endpoint name="BasicHttpBinding_IGpsWebService" address="" binding="basicHttpBinding" contract="Gps.OpenAPI.WebService.IGpsWebService" bindingConfiguration="BasicHttpBinding_NewBind"   />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services> 

 

 <basicHttpBinding>
      <binding name="BasicHttpBinding_NewBind" closeTimeout="00:05:00" openTimeout="00:05:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="5242880000" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
        <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="409600" maxNameTableCharCount="2147483647" />
        <security mode="None">
          <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
          <message clientCredentialType="UserName" algorithmSuite="Default"/>
        </security>
      </binding>
      </basicHttpBinding> 

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; using System.Xml.Linq; namespace WindowsFormsApp3 { public partial class Form1: Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { WebServiceClient _webServiceClient = new WebServiceClient("http://10.100.10.106/webservice/traceability.asmx"); string methodName = "SFC_GetLastTestResultNewPlatform"; var a = _webServiceClient.CallWebServiceAsync(methodName,); } } public class WebServiceClient { private readonly HttpClient _httpClient; private readonly string _serviceUrl; public WebServiceClient(string serviceUrl) { _httpClient = new HttpClient(); _serviceUrl = serviceUrl; } public async Task<string> CallWebServiceAsync(string methodName, params (string name, string value)[] parameters) { // 构建SOAP请求体 var soapEnvelope = BuildSoapEnvelope(methodName, parameters); // 创建HTTP请求 var request = new HttpRequestMessage(HttpMethod.Post, _serviceUrl); request.Content = new StringContent(soapEnvelope, Encoding.UTF8, "text/xml"); // 添加必要的SOAP头 request.Headers.Add("SOAPAction", $"{GetNamespaceFromWsdl()}{methodName}"); // 发送请求 var response = await _httpClient.SendAsync(request); // 处理响应 if (!response.IsSuccessStatusCode) { throw new Exception($"WebService调用失败: {response.StatusCode}"); } var responseContent = await response.Content.ReadAsStringAsync(); return ParseSoapResponse(responseContent, methodName); } private string BuildSoapEnvelope(string methodName, (string name, string value)[] parameters) { var ns = GetNamespaceFromWsdl(); var soapBody = new XElement($"{ns}{methodName}"); foreach (var param in parameters) { soapBody.Add(new XElement(ns + param.name, param.value)); } var soapEnvelope = new XElement(XName.Get("Envelope", "http://schemas.xmlsoap.org/soap/envelope/"), new XElement(XName.Get("Body", "http://schemas.xmlsoap.org/soap/envelope/"), soapBody)); return soapEnvelope.ToString(); } private string ParseSoapResponse(string response, string methodName) { var doc = XDocument.Parse(response); XNamespace ns = GetNamespaceFromWsdl(); return doc.Descendants(ns + $"{methodName}Response") .Elements(ns + $"{methodName}Result") .FirstOrDefault()?.Value; } private string GetNamespaceFromWsdl() { // 需要从WSDL中获取实际的命名空间 // 示例: "http://tempuri.org/" return "http://tempuri.org/"; } } } 我该如何往var a = _webServiceClient.CallWebServiceAsync(methodName,);里面传参
06-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值