C# WCF 客户端参数异常 XmlNode[] 无法转换为类型 "..."



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

我的环境:

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

代码:

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:

类型为"系统"的对象。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
{
...
}  

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

最新更新