简体   繁体   中英

WCF service receives all fields sent by SoapUI-Request only if they appear in a specific order

This question is very similar to this one: Not all parameters in WCF data contract make it through the web service call . However, the answer provided there did not solve my problem.

This is my service-code:

using System.Runtime.Serialization;
using System.ServiceModel;
namespace SoapUiToWcf
{
    [ServiceContract]
    public interface IWcfTest
    {
        [OperationContract]
        WcfData ReturnRequest(WcfData wcfData);
    }

    [DataContract]
    public class WcfData
    {
        [DataMember(Order = 1)]
        public WcfDataNested NestedData { get; set; }

        public class WcfDataNested
        {
            [DataMember(Order = 2)]
            public string Foo { get; set; }
            [DataMember(Order = 3)]
            public string Bar { get; set; }
        }
    }

    public class WcfTest : IWcfTest
    {
        public WcfData ReturnRequest(WcfData wcfData)
        {
            return wcfData;
        }
    }
}

The service should return the identical data which was sent to it. When i send a request through SoapUI with the data below, everything works fine and i get all the values back which have been sent:

<soapenv:Envelope><soapenv:Header/><soapenv:Body><tem:ReturnRequest><tem:wcfData>
    <soap:NestedData>
        <soap:Bar>bar</soap:Bar>
        <soap:Foo>foo</soap:Foo>
    </soap:NestedData>
</tem:wcfData></tem:ReturnRequest></soapenv:Body></soapenv:Envelope>

but if i swap the Bar - and Foo -fields in the request, the wcf-service only receives the foo-value, the other one is null:

<a:Bar i:nil="true"/>
<a:Foo>foo</a:Foo>

EDIT

Screenshot 1 (request and respone in SoapUI): 在此处输入图片说明

Screenshot 2 (Debugging in Visual Studio): 在此处输入图片说明

How do i have to implement the wcf-service in order not to be dependent of the sort-order of the fields in the request?

If you simply remove the Order property from the DataMember decorators on your WcfData object, it shouldn't require any certain order.

[DataContract]
public class WcfData
{
    [DataMember]
    public WcfDataNested NestedData { get; set; }

    public class WcfDataNested
    {
        [DataMember]
        public string Foo { get; set; }
        [DataMember]
        public string Bar { get; set; }
    }
}

EDIT: From the sound of this question, it appears that if you don't serialize alphabetically (an XML convention), the fields that are not in order may be discarded when WCF deserializes the class.

WCF wants you to send them in Order. If you don't specify an order, it defaults to some complex rules that end at alphabetical. I would think that when you remove order, it would demand Bar first. The fact that it didn't might mean something was not refreshed before it was called by SoapUI. Take a look at this other thread: https://stackoverflow.com/a/2520005/2540156

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM