Webservice返回XML数据,C#将值视为null

Webservice返回XML数据,C#将值视为null,c#,xml,wcf,soap,C#,Xml,Wcf,Soap,在C#中调用webservice,当响应反序列化时,所有值都显示为null 我使用Fiddler捕获了数据,我可以看到XML数据 请求如下: <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">

在C#中调用webservice,当响应反序列化时,所有值都显示为null

我使用Fiddler捕获了数据,我可以看到XML数据

请求如下:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <s:Header>
        <VsDebuggerCausalityData xmlns="http://schemas.microsoft.com/vstudio/diagnostics/servicemodelsink">
            uIDPoxpsAt2GhixHrH6i2gAkOR8AAAAAYKd3AIdbIUm9jK6F8GyWoHka0EgCtzpMpV5MZKq2eeUACQAA 
        </VsDebuggerCausalityData>
        <o:Security s:mustUnderstand="1"
                    xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <u:Timestamp u:Id="_0">
                <u:Created>2019-03-28T01:23:21.589Z</u:Created>
                <u:Expires>2019-03-28T01:28:21.589Z</u:Expires>
            </u:Timestamp>
            <o:UsernameToken u:Id="uuid-7e43c8af-1f07-42dd-89b5-6bc295e5d0f6-1">
                <o:Username>username</o:Username>
                <o:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</o:Password>
            </o:UsernameToken>
        </o:Security>
    </s:Header>
    <s:Body>
        <CancelService xmlns="http://tempuri.org/">
            <request xmlns:a="http://schemas.datacontract.org/2004/07/Models" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <a:Required_Date i:nil="true"/>
                <a:Service_ID>R-PMSR-19001188</a:Service_ID>
                <a:Provider_Ref>Oliver_Disconnect_BB</a:Provider_Ref>
            </request>
        </CancelService>
    </s:Body>
</s:Envelope>

UIDPOxPSAT2GhixHRH6I2GAKOR8AAAAAYKD3IDBIUM9JK6F8GYWOHKA0EGCTZPMPV5MZKQ2EEUACQAA
2019-03-28T01:23:21.589Z
2019-03-28T01:28:21.589Z
用户名
密码

我的想法快用完了

编辑1:

ServiceReference3.B2BServiceClient b2BServiceClient = new ServiceReference3.B2BServiceClient();
            var elements = b2BServiceClient.Endpoint.Binding.CreateBindingElements();
            elements.Find<SecurityBindingElement>().EnableUnsecuredResponse = true;
            b2BServiceClient.Endpoint.Binding = new CustomBinding(elements);

            ServiceReference3.CancelRequest cancelRequest = new ServiceReference3.CancelRequest
            {
                Service_ID = "R-PMSR-19001188",
                Provider_Ref = "Oliver_Disconnect_BB"
            };

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;

            b2BServiceClient.ClientCredentials.UserName.UserName = "username";
            b2BServiceClient.ClientCredentials.UserName.Password = "password";

            ServiceReference3.Response response = b2BServiceClient.CancelService(cancelRequest);
ServiceReference3.B2BServiceClient B2BServiceClient=newservicereference3.B2BServiceClient();
var elements=b2BServiceClient.Endpoint.Binding.CreateBindingElements();
elements.Find().EnableUnsecuredResponse=true;
b2BServiceClient.Endpoint.Binding=新的CustomBinding(元素);
ServiceReference3.CancelRequest CancelRequest=新的ServiceReference3.CancelRequest
{
服务\u ID=“R-PMSR-19001188”,
提供商\u Ref=“Oliver\u断开\u BB”
};
ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
b2BServiceClient.ClientCredentials.UserName.UserName=“UserName”;
b2BServiceClient.ClientCredentials.UserName.Password=“Password”;
ServiceReference3.Response-Response=b2BServiceClient.CancelService(cancelRequest);

编辑2:除此之外,我还看到ExtensionData中的值也在响应中传回。

您的消息只有三个属性Status\u Code、Status\u Description、Order\u ID,并且您的响应对象具有更多属性

WCF按顺序反序列化消息,因此,如果在您定义属性的位置未找到消息中的属性,这可能会导致空值,即使该属性在响应消息中也是如此

因此,请在您的回复消息中按顺序包含所有属性

或者,您可以尝试使用DataMember的order属性指定模型的反序列化顺序

 [DataContract]
public  class Parent
{
 // IsRequired is used to test whether has received the property

 // if not, it will show error. Order could change the order of the property in message 
    [DataMember(IsRequired = true,Order =2)]
    public int Field3 { get; set; }
   [DataMember(IsRequired = true,Order =1)]
   public int Field1 { get; set; }

}

尝试将状态代码的顺序指定为第一,将状态描述指定为第二,依此类推,以查看这是否解决了您的问题。

您的邮件只有三个属性状态代码、状态描述、顺序ID,并且您的响应对象具有更多属性

WCF按顺序反序列化消息,因此,如果在您定义属性的位置未找到消息中的属性,这可能会导致空值,即使该属性在响应消息中也是如此

因此,请在您的回复消息中按顺序包含所有属性

或者,您可以尝试使用DataMember的order属性指定模型的反序列化顺序

 [DataContract]
public  class Parent
{
 // IsRequired is used to test whether has received the property

 // if not, it will show error. Order could change the order of the property in message 
    [DataMember(IsRequired = true,Order =2)]
    public int Field3 { get; set; }
   [DataMember(IsRequired = true,Order =1)]
   public int Field1 { get; set; }

}

尝试将状态代码的顺序指定为第一,状态描述的顺序指定为第二,依此类推,看看这是否解决了您的问题。

最后,找出问题所在。从wsdl生成的Reference.cs,响应类的datacontract具有错误的命名空间

将其更改为:

[System.Runtime.Serialization.DataContractAttribute(Name="Response", Namespace="http://tempuri.org/")]


现在解决了这个问题。

最后,找出了问题所在。从wsdl生成的Reference.cs,响应类的datacontract具有错误的命名空间

将其更改为:

[System.Runtime.Serialization.DataContractAttribute(Name="Response", Namespace="http://tempuri.org/")]


目前已解决此问题。

如何反序列化响应?我已添加了发送请求和接收响应的代码。为什么取消请求?使用fiddler时单步执行代码。找出xml响应发生在哪一行。然后查看c#代码是否返回数据或null。不取消请求本身。。。我需要调用CancelService并将CancelRequest对象传递给它。如何反序列化响应?我已添加了发送请求和接收响应的代码。为什么要取消请求?使用fiddler时单步执行代码。找出xml响应发生在哪一行。然后查看c#代码是否返回数据或null。不取消请求本身。。。我需要调用CancelService并将CancelRequest对象传递给它。除此之外,我还处理了排序,值仍然返回为null(尽管fiddler显示了返回的数据)。此外,我处理了排序,值仍然返回为null(尽管fiddler显示返回的数据)