Answered Returning an IEnumerable of type custom data contract from added Service reference not being serialized

Post number 7 has been selected as best answer.

kauto

Member
Joined
May 6, 2020
Messages
7
Programming Experience
Beginner
Hi,

I am relatively new to C# - I am highly experienced with X++ in Dynamics AX.

I created an AIF service inside AX which returns a list of data contract values. The data contract is structured with 2 string parameters. The DataContract attribute and DataMember attributes are assigned.

I created a console application and I am able to easily return the values however I am trying to create a web service wrapper which will consume this AX service and return the data contract values out to a third party application.

My issue is that on the web service I am not able to return the data contract values I get the error message system.NotSupportedException: Cannot serialize interface System.Collections.Generic.IEnumerable`1 *Name of my data contract value*

I need some direction on what to do, how do I go about taking the IEnumerable list of data contract values inside my web service to allow these to be output correctly as a list of data contracts comprising of 2 string values?

Thank you kindly
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,227
Location
Virginia Beach, VA
Programming Experience
10+
Have to tried returning a List<T> instead of an IEnumerable<T>?
 

kauto

Member
Joined
May 6, 2020
Messages
7
Programming Experience
Beginner
Have to tried returning a List<T> instead of an IEnumerable<T>?
Hi Skydiver,

Yes but because I am returning a non native type (as in a custom data contract being imported from a third party service) List will not work.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,227
Location
Virginia Beach, VA
Programming Experience
10+
Wrap the the data. If you have a lot of these, Automapper will be your friend.
 

kauto

Member
Joined
May 6, 2020
Messages
7
Programming Experience
Beginner
Wrap the the data. If you have a lot of these, Automapper will be your friend.
Sorry Skydiver, do you have an example of how to do this or a link to a tutorial or example?
 

kauto

Member
Joined
May 6, 2020
Messages
7
Programming Experience
Beginner
I think in short, how do I return a list of containers with 2 values in them?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,227
Location
Virginia Beach, VA
Programming Experience
10+
Code:
class MyContainmentField
{
    public string LeftReactorCore { get; set; }
    public string RightReactorCore { get; set; }
}
:
var list = new List<MyContainmentField>();
:
return list;
 

kauto

Member
Joined
May 6, 2020
Messages
7
Programming Experience
Beginner
Thanks for this, I am beginning to understand.

Here is my current code after your changes:

C#:
public class WebService1 : System.Web.Services.WebService

{   

        [WebMethod]

        public List<deliveryNoteInfo> getDeliveryNoteData(

            string      company,

            string      lorryId,

            DateTime    deliveryDate

            )

        {

            // define variables

            PackingSlipServiceReference.BM_PODRequestContract appRequest = new PackingSlipServiceReference.BM_PODRequestContract();

            PackingSlipServiceReference.BM_PackingSlipServiceClient packSlipService = new PackingSlipServiceReference.BM_PackingSlipServiceClient();

            IEnumerable<PackingSlipServiceReference.BM_PODResponseContract> deliveryNoteList = new List<PackingSlipServiceReference.BM_PODResponseContract>();

            PackingSlipServiceReference.CallContext context = new PackingSlipServiceReference.CallContext();

            PackingSlipServiceReference.BM_PODResponseContract deliveryNoteData = new PackingSlipServiceReference.BM_PODResponseContract();

            List<deliveryNoteInfo> packSlipList = new List<deliveryNoteInfo>();

            deliveryNoteInfo    delInfo;



            // set parameter values

            appRequest.parmLorryId = lorryId;

            appRequest.parmDeliveryDate = deliveryDate;

            context.Company = company;



            deliveryNoteList = packSlipService.getDeliveryNotes(context, appRequest);   



            // send these to service and get result

            var enumerator = deliveryNoteList.GetEnumerator();





            while (enumerator.MoveNext())

            {

                deliveryNoteData = enumerator.Current;



                delInfo = new deliveryNoteInfo();



                delInfo.packingSlipId = deliveryNoteData.parmPackingSlipId;

                delInfo.deliveryName = deliveryNoteData.parmDeliveryName;



                packSlipList.Add(delInfo);

            }



            // return list of delivery note data contracts

            return packSlipList;

        }



        class deliveryNoteInfo

        {

            public string packingSlipId { get; set; }

            public string deliveryName { get; set; }

        }

      

}
I am getting the following error:

Error 1 Inconsistent accessibility: return type 'System.Collections.Generic.List<PackingSlipService.WebService1.deliveryNoteInfo>' is less accessible than method 'PackingSlipService.WebService1.getDeliveryNoteData(string, string, System.DateTime)'

Am I missing something above?
 
Last edited:

kauto

Member
Joined
May 6, 2020
Messages
7
Programming Experience
Beginner
Strike that, I have made the class deliveryNoteInfo PUBLIC and this has resolved the error.

When I run the test for the service, it runs but returns me the following value:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<ArrayOfDeliveryNoteInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://TESTPOD.org/"/>

I don't get the data values returned in the Array, is this normal? Do I have to manipulate this list of data in my calling program?
 

kauto

Member
Joined
May 6, 2020
Messages
7
Programming Experience
Beginner
Hi,

I think the issue is simply down to my date parameter formatting, on the debug it looks like when I enter the date 01\05\2020 to signify the 1st May in UK date format, the service is sending the date in US format so 05\01\2020 - is there something I need to do to ensure it is in UK format for entry?

Thanks again I am basically done now!
 

Sheepings

Senior Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,141
Location
UK
Programming Experience
10+
Your date format is obviously wrong. You must have it set as : MM/dd/yyyy

Change it to :
your_datetime_variable.ToString("dd/MM/yyyy");
 
Top Bottom