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)

Error Invoking Method from .NET Referenced Assembly

(0) ShareShare
ReportReport
Posted on by 100

Hi.  Please would someone confirm whether it is possible within an AX 4 class, to invoke a static method from a referenced .NET assembly, which returns a System.String?

I have created a .NET assembly, added a reference to it within AX 4, and am able to invoke a method from the referenced assembly, where the method returns null.  However, when I attempt to do the same, but return System.String, the following error is raised:

Error executing code: Wrong argument types for addition.

The error puzzles me, since the method does not have any arguments, and I am struggling to understand why the error states 'for addition'.

Would someone please enlighten me?

Thanks

*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    237,805 Most Valuable Professional on at

    How do you use the return value? If you use it in an expression, put it to an AX string variable first and use that native-typed variable in the X++ expression.

    Unfortunately I have no AX4 on hand to try to replicate your issue.

  • Community Member Profile Picture
    on at

    I agree with Martin on this. Working with referenced .NET assemblies can be very painful and one thing that I have noticed is that the best thing is always to put the return value in a x++ variable before you use it.

    E.g. in this case, you have probably written something like:

    str s;

    s = 'first part';

    s += assembly.getMoreStringData(); //Conversion (probably) fails

    You should do:

    s2 = assembly.getMoreStringData(); //Conversion works

    s += s2;

    I have had similar problems with integers.

    E.g.

    if (assembly.get_count() > 0)

    does not work properly.

    it is better to do it like this:

    int i;

    i = assembly.get_count();

    if (i > 0)....

    You also have some useful static methods in the ClrInterop class (at least in Ax 2009)

    One good method here is

    ClrInterop::isNull

    to check if an assembly has returned null. Ordinary null check will not work in Ax.

    /Jonas

  • BDRichardson Profile Picture
    100 on at

    Unfortunately, it will be some time before our company will upgrade from version 4.  They have only recently completed a massive rollout.

    I have been experimenting with all of the X++ where I think it might overcome the issue.

    All of the following approaches raise the wrong arguments for addition error when I attempt to assign the return value to the variable.

    I have tried declaring an X++ str variable, and then assigning it to the return value from the reference method, which fails:

    str result;

    ;

    result = NetAssembly.NetMethod();

    I have tried using System.String, which fails:

    System.String result;

    ;

    result = NetAssembly.NetMethod();

    I have tried using CLRInterop::getAnyTypeForObject:

    str result;

    ;

    result = CLRInterop::getAnyTypeForObject( NetAssembly.NetMethod());

    I would be pleased if I could just get the return value to assign to something.  From there I would be able try using it.

    I have even made the assembly method as simple as it can be, with no logic, i.e. non-static, no arguments, a purely returning an empty string.

    Thanks.

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

    Maybe it's caused only by your simplifying of the example, but what exactly "NetAssembly.NetMethod()" means? If NetMethod() in an instance method, you would have to create an instance at first. If it is a static method, you have to call it as NetAssembly::NetMethod().

  • BDRichardson Profile Picture
    100 on at

    The method is not static.  Apologies, I stated incorrectly, in my opening passage, that the method was static.  So I declare an instance of the class in X++:

    str result;

    NetAssembly.NetClass test;

    ;

    test = new NetAssembly.NetClass;

    Then attempt to invoke the non-static method:

    result = test.NetMethod();

    There really isn't much more to my test code beside asserting the appropriate permissions, and adding a reference to the assembly.  All of which appears to work perfectly.

    I could try changing the method to a static one, but that wouldn't really work very well as a solution within the assembly.

    I have successfully invoked a different non-static method, which doesn't have a return value, i.e. void.

    It really puzzles me what the error message is trying to tell me by means of the 'addition'.  It seems to me that when I am trying to assign the result to the variable, it is trying to add the result to the string, rather than assign it.  Bizarre

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

    Unfortunately I don't know where the problem can be. The last idea - are you sure that it is caused by converting to X++ and not by an exception in managed code? (Because I would expect some marshalling exception in the first case.)

  • Community Member Profile Picture
    on at

    Perhaps you did stop the .NET assembly from changing its build number/class id, so that your old assembly is still installed. Perhaps the method is still returning null and not the string that you think it is?

    Also... at least in Ax 2009, if you add a method or change its declaration you will have to restart the AOS. Ax keeps an in-memory copy of how the assembly was looking like when it was started.

  • Verified answer
    BDRichardson Profile Picture
    100 on at

    I think I may have identified the problem...

    I created some new methods within the assembly, to try identify what works, and what doesn't.  I found that I can create both static, and non-static methods, and return either a System.String or a System.Int32, and they all work.

    So then, I attempted to invoke the original method, and that too worked!

    Aha, I suspect the assembly had not been updated in every location, i.e. GAC, client\bin and server\bin.

    I have not yet determined whether I need to install the assembly in all of these locations, but I was intending to address that issue once I got over this initial problem.

    Clearly, I have not been thorough enough in my deployment.  Apologies for wasting your time, and thank you so much for giving me the kick that I was in much need of!

  • Community Member Profile Picture
    on at

    No problem.

    My approach to .NET assemblies that I have created on my own is to always call them from server-side classes. After that I will only have to install my assembly in the GAC of the server (and on the development machines)

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