创建SoapClient对象几个常见错误提示及解决方案
创建SoapClient对象几个常见错误提示及解决方案
//初始化实例
SalesModuleWebServiceSoapClient o = new SalesModuleWebServiceSoapClient();
错误提示:
Error Message:
在 ServiceModel 客户端配置部分中,找不到引用协定“SalesModuleWebService.SalesModuleWebServiceSoap”的默认终结点元素。这可能是因为未找到应用程序的配置文件,或者是因为客户端元素中找不到与此协定匹配的终结点元素。
在 ServiceModel 客户端配置部分中,找不到引用协定“SalesModuleWebService.SalesModuleWebServiceSoap”的默认终结点元素。这可能是因为未找到应用程序的配置文件,或者是因为客户端元素中找不到与此协定匹配的终结点元素。
于是我尝试使用第2,3,4个构造器,错误依旧,试验第5个方法时出现另一个错误!
Error Message:
BasicHttpBinding BINDING = new BasicHttpBinding("SalesModuleWebServiceSoap");
在配置元素集合中找不到任何元素与密钥“SalesModuleWebServiceSoap”匹配。
以上两个错误提示的根本原因是找不到Config文件或取不到配置文件内的信息,查看工程文件有app.config, 查看debug目录有CSFramework3.WebServiceReference.dll.config 文件。google,baidu搜索文章无果,百思不得其解,无耐之下只能自己取配置文件,源码如下:
/// <summary>
/// 创建销售模块的SOAP Client对象
/// </summary>
/// <returns></returns>
public static SalesModuleWebServiceSoapClient CreateSalesModuleClient()
{
//取ASMX文件URI地址
string endpoint = SoapClientConfig.GetSoapRemoteAddress("SalesModuleWebServiceSoap");
BasicHttpBinding BINDING = new BasicHttpBinding();
SoapClientConfig.ReadBindingConfig(BINDING, "SalesModuleWebServiceSoap");
return new SalesModuleWebServiceSoapClient(BINDING, new EndpointAddress(endpoint));
}
// 来源:www.CSFramework.com, C/S框架网
/// 创建销售模块的SOAP Client对象
/// </summary>
/// <returns></returns>
public static SalesModuleWebServiceSoapClient CreateSalesModuleClient()
{
//取ASMX文件URI地址
string endpoint = SoapClientConfig.GetSoapRemoteAddress("SalesModuleWebServiceSoap");
BasicHttpBinding BINDING = new BasicHttpBinding();
SoapClientConfig.ReadBindingConfig(BINDING, "SalesModuleWebServiceSoap");
return new SalesModuleWebServiceSoapClient(BINDING, new EndpointAddress(endpoint));
}
// 来源:www.CSFramework.com, C/S框架网
通过GetSoapRemoteAddress自定义方法获到URI地址,创建实例成功。
在测试程序中偶然又出现另一个错误,原因是SOAP不支持大数据包,在下载大图片文件时报错的:
Error Message:
格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出错: SO_GetBusinessByKeyResponse。InnerException 消息是“反序列化对象 属于类型 CSFramework3.WebServiceReference.SalesModuleWebService.SO_GetBusinessByKeyResponseBody 时出现错误。读取 XML 数据时,超出最大数组长度配额(16384)。通过更改在创建 XML 读取器时所使用的 XmlDictionaryReaderQuotas 对象的 MaxArrayLength 属性,可增加此配额。 第 1 行,位置为 36611。”。有关详细信息,请参阅 InnerException。
格式化程序尝试对消息反序列化时引发异常: 尝试对参数 http://tempuri.org/ 进行反序列化时出错: SO_GetBusinessByKeyResponse。InnerException 消息是“反序列化对象 属于类型 CSFramework3.WebServiceReference.SalesModuleWebService.SO_GetBusinessByKeyResponseBody 时出现错误。读取 XML 数据时,超出最大数组长度配额(16384)。通过更改在创建 XML 读取器时所使用的 XmlDictionaryReaderQuotas 对象的 MaxArrayLength 属性,可增加此配额。 第 1 行,位置为 36611。”。有关详细信息,请参阅 InnerException。
错误提示告诉我需要改XmlDictionaryReaderQuotas.MaxArrayLength 属性,在app.config文件内改好了,运行后错误依旧!根本原因仍是找不到config文件,于是补充代码:
/// <summary>
/// 设置BasicHttpBinding对象的配置信息
/// </summary>
/// <param name="BINDING">BasicHttpBinding对象</param>
/// <param name="bindingName">bindingName</param>
public static void ReadBindingConfig(System.ServiceModel.BasicHttpBinding BINDING, string bindingName)
{
XmlDocument xml = new XmlDocument();
xml.Load(SoapClientConfig.ConfigFile);
string xpath;
XmlNode node;
xpath = "configuration/system.serviceModel/bindings/basicHttpBinding/binding[@name='" bindingName "']";
node = xml.SelectSingleNode(xpath);
BINDING.MaxBufferSize = Int32.Parse(node.Attributes["maxBufferSize"].Value);
BINDING.MaxBufferPoolSize = Int32.Parse(node.Attributes["maxBufferPoolSize"].Value);
BINDING.MaxReceivedMessageSize = Int32.Parse(node.Attributes["maxReceivedMessageSize"].Value);
xpath = "configuration/system.serviceModel/bindings/basicHttpBinding/binding[@name='" bindingName "']/readerQuotas";
node = xml.SelectSingleNode(xpath);
BINDING.ReaderQuotas.MaxDepth = Int32.Parse(node.Attributes["maxDepth"].Value);
BINDING.ReaderQuotas.MaxStringContentLength = Int32.Parse(node.Attributes["maxStringContentLength"].Value);
BINDING.ReaderQuotas.MaxArrayLength = Int32.Parse(node.Attributes["maxArrayLength"].Value);
BINDING.ReaderQuotas.MaxBytesPerRead = Int32.Parse(node.Attributes["maxBytesPerRead"].Value);
BINDING.ReaderQuotas.MaxNameTableCharCount = Int32.Parse(node.Attributes["maxNameTableCharCount"].Value);
}
// 来源:www.CSFramework.com, C/S框架网
/// 设置BasicHttpBinding对象的配置信息
/// </summary>
/// <param name="BINDING">BasicHttpBinding对象</param>
/// <param name="bindingName">bindingName</param>
public static void ReadBindingConfig(System.ServiceModel.BasicHttpBinding BINDING, string bindingName)
{
XmlDocument xml = new XmlDocument();
xml.Load(SoapClientConfig.ConfigFile);
string xpath;
XmlNode node;
xpath = "configuration/system.serviceModel/bindings/basicHttpBinding/binding[@name='" bindingName "']";
node = xml.SelectSingleNode(xpath);
BINDING.MaxBufferSize = Int32.Parse(node.Attributes["maxBufferSize"].Value);
BINDING.MaxBufferPoolSize = Int32.Parse(node.Attributes["maxBufferPoolSize"].Value);
BINDING.MaxReceivedMessageSize = Int32.Parse(node.Attributes["maxReceivedMessageSize"].Value);
xpath = "configuration/system.serviceModel/bindings/basicHttpBinding/binding[@name='" bindingName "']/readerQuotas";
node = xml.SelectSingleNode(xpath);
BINDING.ReaderQuotas.MaxDepth = Int32.Parse(node.Attributes["maxDepth"].Value);
BINDING.ReaderQuotas.MaxStringContentLength = Int32.Parse(node.Attributes["maxStringContentLength"].Value);
BINDING.ReaderQuotas.MaxArrayLength = Int32.Parse(node.Attributes["maxArrayLength"].Value);
BINDING.ReaderQuotas.MaxBytesPerRead = Int32.Parse(node.Attributes["maxBytesPerRead"].Value);
BINDING.ReaderQuotas.MaxNameTableCharCount = Int32.Parse(node.Attributes["maxNameTableCharCount"].Value);
}
// 来源:www.CSFramework.com, C/S框架网
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网