web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Creating a web service to return a list of custom data contract values

(0) ShareShare
ReportReport
Posted on by 2,699

Hi,

I've built a custom data contract with 2 parameters to be returned via my deployed service.

The end goal is to use this service in an android app but I am relatively new to developing in this environment.

My service works fine in a C# console application, however I have read that creating a web service wrapper based on my exposed AIF service is the best way to go for my end goal.

I have created a web service application but when I attempt to test this the compilation fails as I have had to declare my return type to be an IEnumerable list containing instances of the data contract being returned from AX.

I get the message Cannot serialize interface System.Collections.Generic.IEnumerable`1 when the ASMX service is tested.

I am highly experienced working with X++ and java so I am relatively new to C# as well and not an expert with XML either.

I am guessing that there is an issue with the way the XML is being generated or serialised around my custom data contract which is the return value.

Is there something I need to do with this data contract in AX to get around this issue, or do I need to focus on converting the data inside the Service wrapper and returning the values in a more native data type style list?  All I am returning in the custom data contract is 2 string based parameter values per record and the list contains each instance of this contract.

If anyone has any experience of this issue and what the best approach is I'd been keen to hear from them

Thank you.

 

I have the same question (0)
  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Perhaps you could share the code where you get the compiler error?

    Anyway if you already developed a console app that works, seems that AX part is good and next efforts and problems are outside AX. Correct? In that case you might get better help from C# forums and communities.

  • Kauto Profile Picture
    2,699 on at

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using System.Web.Services;

    namespace PackingSlipService

    {

       /// <summary>

       /// Summary description for PackingSlip data service

       /// </summary>

       [WebService(Namespace = "http://tempuri.org/")]

       [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

       [System.ComponentModel.ToolboxItem(false)]

       // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

       // [System.Web.Script.Services.ScriptService]

       public class Service1 : System.Web.Services.WebService

       {

           [WebMethod]

           public IEnumerable<PackingSlipServiceReference.BM_PODResponseContract> getDeliveryNotes(

               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();

               // set parameter values

               appRequest.parmLorryId = lorryId;

               appRequest.parmDeliveryDate = deliveryDate;

               context.Company = company;

               // send these to service and get result

               deliveryNoteList = packSlipService.getDeliveryNotes(context, appRequest);

               // return list of delivery note data contracts

               return deliveryNoteList;

           }

       }

    }

    Above is the code I have written inside the web service wrapper. This is simply for testing the return of data.

    My issue relates to the fact that I have declared the IEnumerable list type set to that of my custom data contract type.  I have correctly declared this contract inside AX with DataContractAttributeType and DataMemberAttributeType assigned to both parameters which are included in the contract:

    On the getDeliveryNotes method in AX this is what the header is set to:

    [SysEntryPointAttribute(true),

    AifCollectionTypeAttribute('PODRequest', Types::Class, classStr(BM_PODRequestContract)),

    AifCollectionTypeAttribute('return', Types::Class, classStr(BM_PODResponseContract))]

    It takes the request contract and then returns the response contract.

    This all works fine in the C# console app but when I try to test this as a web service wrapper it gives me the original error.

    I suppose my question is what to do so that my return type custom service data contract type is recognisable and can be returned with data without error through the web service?

  • Martin Dráb Profile Picture
    237,908 Most Valuable Professional on at

    Let me start by re-posting your code with Insert > Insert Code. It'll make it easier for us to read it.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    
    namespace PackingSlipService
    {
       /// 
       /// Summary description for PackingSlip data service
       /// 
       [WebService(Namespace = "http://tempuri.org/")]
       [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
       [System.ComponentModel.ToolboxItem(false)]
       // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
       // [System.Web.Script.Services.ScriptService]
       public class Service1 : System.Web.Services.WebService
       {
           [WebMethod]
           public IEnumerable getDeliveryNotes(
               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 deliveryNoteList = new List();
               PackingSlipServiceReference.CallContext context = new PackingSlipServiceReference.CallContext();
    
               // set parameter values
               appRequest.parmLorryId = lorryId;
               appRequest.parmDeliveryDate = deliveryDate;
               context.Company = company;
    
               // send these to service and get result
               deliveryNoteList = packSlipService.getDeliveryNotes(context, appRequest);
    
               // return list of delivery note data contracts
               return deliveryNoteList;
           }
       }
    }
    

    XmlSerializer isn't able to serialize interfaces such as IEnumerable<T>. Use a class instead, such as Collection<T>.

    Also, if you create a middle-tier service anyway, didn't you consider using a RESTful service instead? I don't know whether it would make things easier on Android side, but I guess it could.

  • Kauto Profile Picture
    2,699 on at

    Thanks Martin I will try this out and see how far I get, thanks for the tip on the Insert code option for the forum as well.

  • Kauto Profile Picture
    2,699 on at

    Just to clarify Martin, I would take the response contract PackingSlipServiceReference.BM_PODResponseContract and create a class in the C# application to represent the data parameters inside the AX custom contract and then pass the data into this class type, then use this newly created class as a return type and use the ICollection instead of IEnumerable?

    Apologies for my ignorance I am still getting my head around this, if you have any examples of this in use or indeed a link to the secondary option of using the RESTful service that would be great.

  • Verified answer
    Martin Dráb Profile Picture
    237,908 Most Valuable Professional on at

    Using ICollection would have the same problem. As IEnumerable, ICollection is an interface and interfaces can't be serialized by the XML serializer. You must return a class (such as Collection) instead of an interface.

    .NET world offers many ways how you can implement web services. You might be interested in something like 10 Ways to Build Web Services in .NET (sadly the site is not free, but you can use a trial), or maybe a newer resource. Or maybe you don't need custom development at all and you can use something Azure API Management. It all depends on what you want to achieve by the middle-tier service.

  • Suggested answer
    Martin Dráb Profile Picture
    237,908 Most Valuable Professional on at

    You can find a huge amount of information about RESTful services in .NET, if you decide it's what meets your requirements. Just for example, here is a minimalistic tutorial to one of the approaches: Creating A REST Webservice With .NET Core.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 544 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 450 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans