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 :
Microsoft Dynamics AX (Archived)

How to consume C# data contract class list in Ax 2012

(0) ShareShare
ReportReport
Posted on by 60

Hello,

I had to make integration between 2 different Ax environment. I wrote an AIF service for one environment to read data and I wrote a C# class, I am getting data from AIF to C# by using data contract class and I am returning as data contract class list. When I added C# project to second Ax environment in Visual Studio C# Projects I can not consume the data which returning from C# project as data contract class list in x++. How can i consume C# list in X++?

I need to get data from this C# method in x++;

public List<ShipmentTableContract> getShipmentTable(string companyId)
{

    CallContext axCallContext = new CallContext();
    ShipmentIntegServiceClient axService = this.logonToService();
    axCallContext.Company = companyId;


    List<ShipmentTableContract> shipmentList = new List<ShipmentTableContract>();
    shipmentList = axService.getShipmentTable(axCallContext);

    return shipmentList;

}


Can anyone help?

*This post is locked for comments

I have the same question (0)
  • metin emre Profile Picture
    502 on at

    Hi,

    Try something like this:

    uyumAX.uyumServis.InvoiceInfo[] invoiceTypeList;
    uyumAX.uyumServis.InvoiceInfo invoiceType;

    ...

    invoiceTypeList = new uyumAX.uyumServis.InvoiceInfo[invoiceCount]();

    ...

    invoiceType = invoiceTypeList.GetValue(i);

  • Ertugrul Balveren Profile Picture
    60 on at

    I tried something like this but still can not consume the list which is coming from C#.

    public void processShipment()
    {
        ShipmentIntegrationWebService.ShipmentIntegration        shipmentIntegrationService = new ShipmentIntegrationWebService.ShipmentIntegration();
        
        
        ShipmentIntegrationWebService.ShipmentIntegrationServiceReference.ShipmentTableContract[]       shipmentTable;    
        
        shipmentTable = new ShipmentIntegrationWebService.ShipmentIntegrationServiceReference.ShipmentTableContract[10]();
        
        
        shipmentTable = shipmentIntegrationService.getShipmentTable("T01");
    
    }


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

    You can use the non-generic interface IEnumerable, as discussed in Get List < T> of VS2010 in X ++.

    By the way, notice that your code doesn't dispose the client (which should be done both if the code succeeds and if it throws an exception).

  • Ertugrul Balveren Profile Picture
    60 on at

    How can I declare ShipmentTableContract class to non generic types in C#?

    public IEnumerable getShipmentTable(string companyId)
    {
        CallContext axCallContext = new CallContext();
        ShipmentIntegServiceClient axService = this.logonToService();
    
        try 
        {	        
    		        
            axCallContext.Company = companyId;
               
            IEnumerable<ShipmentTableContract> shipmentList = new ArrayLis<ShipmentTableContract>();
            shipmentList = axService.getShipmentTable(axCallContext);
    }
    catch (Exception) { axService.Abort(); throw; } finally { axService.Close(); } }


    I am getting the following error while declaring shipmentList to new ArrayList;

    "The non-generic type 'System.Collections.IEnumerable' cannot be used with type arguments"

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

    I didn't mean changing the declaration, I meant declaring the variable in X++ as IEnumerable and using polymorphism to access the generic collection through the non-generic interface. Note that declaring a different return type doesn't change the actual returned object, so it doesn't make any difference.

    Anyway, regarding your code, I guess you get the error at IEnumerable<ShipmentTableContract>. Maybe you don't have a reference to System.Collections.Generic, therefore the compiler doesn't know the generic interface IEnumerable<T>. It see only the non-generic interface IEnumerable, which (as the error says) can't be used with type parameters (such as IEnumerable<ShipmentTableContract>).

  • Ertugrul Balveren Profile Picture
    60 on at

    Hello again,

    I wrote the C# code like following and in X++ I am getting  System.NullReferenceException: Object reference not set to an instance of an object.

    My C# Code,

    public List<ShipmentTableContract> getShipmentTable(string companyId)
    {
    	CallContext axCallContext = new CallContext();
    	ShipmentIntegServiceClient axService = this.logonToService();
    
    	try 
    	{	        
    		
    		axCallContext.Company = companyId;
    
    		List<ShipmentTableContract> shipmentList = new List<ShipmentTableContract>();
    		shipmentList = axService.getShipmentTable(axCallContext);
    
    		return shipmentList;
    	}
    	catch (Exception)
    	{
    		axService.Abort();
    		throw;
    	}
    	finally
    	{
    		axService.Close();
    	}
    }


    and my X++ code where i get  System.NullReferenceException: Object reference not set to an instance of an object error.

    public void processShipment()
    {
    
        ShipmentIntegrationWebService.ShipmentIntegration                   shipmentInteg = new ShipmentIntegrationWebService.ShipmentIntegration();
        System.Collections.IEnumerable                                      shipmentLines;
    
    
        shipmentLines = shipmentInteg.getShipmentTable("T01");
    
        info("Done");
    }


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

    Which line of code is throwing the error?

    By the way, notice that the assignment of "new List<ShipmentTableContract>()" has no effect, because the value is immediately overwritten on the next line.

  • Ertugrul Balveren Profile Picture
    60 on at

    Hello,

    I am getting the error in this line;

    shipmentLines = shipmentInteg.getShipmentTable("T01");
    


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

    This could mean two things:

    • either shipmentInteg is null
    • or the exception is thrown from inside of getShipmentTable()

    I assume that it's the latter case and therefore the answer isn't correct. The exception isn't thrown at this line, it's thrown inside you C# code and you must do more work to find the place. For instance, use Visual Studio to debug your library.

  • Ertugrul Balveren Profile Picture
    60 on at

    Yes, You are right i debugged the code and I am getting error in C# but when i run this code as console application it works fine i can get the list from x++ to C# when i convert to class library i am getting following error.

    "Could not find default endpoint element that references contract 'ShipmentServiceReference.ShipmentIntegrationService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element."

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 > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans