【问题标题】:C# WCF Client Argument Exception XmlNode[] cannot be converted to type "..."C# WCF 客户端参数异常 XmlNode[] 无法转换为类型“...”
【发布时间】:2019-11-19 08:56:31
【问题描述】:

我正在尝试使用 C# 和 WCF 创建一个 SOAP 客户端。

我的环境:

  • Visual Studio 2019
  • .net Core 3.0 MVC 项目
  • 带有基本身份验证的 WSDL 网址

代码:

AccountServicePortClient.EndpointConfiguration configuration = new AccountServicePortClient.EndpointConfiguration();
AccountServicePortClient client = new AccountServicePortClient(configuration);
BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
client.Endpoint.Binding = binding;
client.ClientCredentials.UserName.UserName = "XXX";
client.ClientCredentials.UserName.Password = "XXX";
Task<CheckAddressSuccess> success = client.checkAddressAsync(*Some Arguments*);
success.Wait();
CheckAddressSuccess successResult= success.Result;

收到结果时,我得到一个 ArgumentException:

“System.Xml.XmlNode[]”类型的对象无法转换为“ServiceReference2.CheckAddressSuccess”类型

有人知道我该如何解决这个问题吗?

更新:

客户端是通过将 WSDL URL 添加为 WCF 提供程序来创建的。我没有构建肥皂服务。我已经发现 SOAP 响应 CheckAddressSuccess 在命名空间中有错字(“http ...”而不是“https ...”)。我想使用 WCF DataContractSerializer。我怎样才能做到这一点?

[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.1")]
[System.ServiceModel.ServiceContractAttribute(Namespace="https://XXX/account", ConfigurationName="ServiceReference2.AccountServicePort")]
public interface AccountServicePort
{
    [System.ServiceModel.OperationContractAttribute(Action="https://XXX/account#checkAddress", ReplyAction= "*")]
    [System.ServiceModel.XmlSerializerFormatAttribute(Style = System.ServiceModel.OperationFormatStyle.Rpc, SupportFaults = true, Use = System.ServiceModel.OperationFormatUse.Encoded)]
    [return: System.ServiceModel.MessageParameterAttribute(Name="return")]
    System.Threading.Tasks.Task<ServiceReference2.CheckAddressSuccess> checkAddressAsync(string transactionId, ServiceReference2.Address address, bool checkHousenumberAdditive);
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.1")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.Xml.Serialization.SoapTypeAttribute(Namespace="https://XXX/account")]
public partial class CheckAddressSuccess
{
    ...
}  

【问题讨论】:

    标签: c# wcf asp.net-core soap client


    【解决方案1】:

    反序列化过程中出现问题。你如何定义数据合约?您是否使用了 XML 序列化程序?在某些情况下,直接使用 XML 反序列化自定义对象可能会导致错误。这是一个例子。
    https://forums.asp.net/t/1704221.aspx?Unable+to+cast+object+of+type+System+Xml+XmlNode+to+type
    我想知道你的服务器的数据合约信息和创建服务器的配置。
    最后,我建议您使用 DataContractSerializer 作为 WCF 中的默认序列化程序。
    请参考下面的序列化建议。
    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/types-supported-by-the-data-contract-serializer
    如果问题仍然存在,请随时告诉我。

    【讨论】:

    • 您的建议很有帮助。我在我的原始帖子中添加了一个更新。我想使用 DataContractSerializer。如何在我的代码中实现它?
    • 在 WCF 中,发布服务元数据取决于服务器端的 WCF 配置。因此,修改默认序列化程序应该在服务器端完成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 2014-10-19
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多