简体   繁体   中英

WCF XML deserialization not populating array

I have developed a WCF using VS 2017 Pro (C#) targeting .Net 4.5.1 The service accepts an XML object in the SOAP request.
Problem I am having is that there is an array in the XML that is not deserializing. The WCF is using the native deserializer. Code & XML sample below.

XML tag Application->Amount deserializes ok

XML tag Application_SAList->Application_SA->SecurityKey> does not

business logic null check for ApplicationImpl.Application_SAList passes, but length of ApplicationImpl.Application_SAList.Application_SA array is 0

all classes have 'using System.Runtime.Serialization;'

Desperate to figure this out! Thanks in advance for anyone's help!

IApplicationService.cs

namespace TApplicationService
{
    [ServiceContract(Namespace = "http://xyz")]
    public interface IApplicationService
    {

        [OperationContract(Name="ProcessApplication")]
        string ProcessApplication(ApplicationRequestImpl ApplicationRequestImpl);
    }
}

ApplicationService.svc.cs

namespace ThriventApplicationService
{

    [ServiceBehavior(Namespace = "http://xyz")]
    public class ApplicationService : IApplicationService
    {
        private SDatabase gDatabase = null;

        public string ProcessApplication(ApplicationRequestImpl ApplicationRequestImpl)
        {
          // does a bunch of business logic here
        }
}

ApplicationRequestImpl.cs

namespace ThriventApplicationService
    {

        [DataContract(Namespace = "http://xyz")]
        public class ApplicationRequestImpl
        {
            [DataMember]
            public Application Application;

            [DataMember(Name="Application_SAList")]
            public ApplicationSASingle Application_SAList;
        }
    }

Application.cs

namespace ThriventApplicationService
{
    [DataContract(Namespace = "http://xyz")]
    public class Application : IExtensibleDataObject
    {

        [DataMember]
        public string Amount;
    }
}

Application_SASingle.cs

namespace ThriventApplicationService
{
    [DataContract(Namespace = "http://xyz")]
    public class ApplicationSASingle
    {
        [DataMember]
        public Application_SA[] Application_SA;
    }
}

Application_SA.cs

namespace ThriventApplicationService
{
    [DataContract(Namespace = "http://xyz")]
    public class Application_SA
    {
        [DataMember]
        public string Security_Key;
    }
}

SAMPLE XML

<ProcessApplication xmlns="http://xyz">
    <ApplicationRequestImpl>
        <Application>
            <Amount>50000.0</Amount>
        </Application>
        <Application_SAList>
            <Application_SA>
                <Security_Key>1588</Security_Key>
            </Application_SA>
        </Application_SAList>
    </ApplicationRequestImpl>
</ProcessApplication>

SOLUTION in ApplicationRequestImpl.cs

   [DataContract(Namespace = "http://prosurv")]
    public class ApplicationRequestImpl
    {

        [DataMember]
        public Application Application;

        [DataMember(Name="Application_SAList")]
        //public Application_SAList Application_SAList;
        public Application_SA[] Application_SA;
}

After another shot I have found the solution. The problem is with ApplicationRequestImpl class. XML rerader is looking for Application_SA element inside Application_SA which doesn't exist.

[DataContract(Namespace = "http://xyz")]
public class ApplicationRequestImpl
{
    [DataMember]
    public Application Application;

    //Remove these two lines
    //[DataMember(Name="Application_SAList")]
    //public ApplicationSASingle Application_SAList;

    [DataMember]
    public Application_SA[] Application_SAList;
}

Check full working solution


The problem with arrays is that they need know their size. Which during initialization is 0. Which equals to:

 public Application_SA[] Application_SA = new Application_SA[0]; 

Deserialized objects have no room to fit in.

Solution to this will be to switch to:

 public List<Application_SA> Application_SA = new List<Application_SA>(); 

Hope it helps.

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