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)

Using System.Net.Sockets.Socket in AX 4.0

(0) ShareShare
ReportReport
Posted on by

Hello,


I have to develop a functionality to send data to an IP-address & port. In AX 2012 it works however in 4.0 it doesn't. My example job:

static void ALP_SocketTest(Args _args)
{
    System.Net.IPAddress        address;
    System.Net.IPEndPoint       endPoint;
    System.Net.Sockets.Socket   socket;
    str                         contents;

    void sendCommand(str _command)
    {
        System.Text.Encoding ascii;
        CLRObject   bytes;

        int             result;
        ;
        ascii = System.Text.Encoding::get_ASCII();
        bytes = ascii.GetBytes(_command + '\n');

        socket.Send(bytes);
    }

    socket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily::InterNetwork,|
             System.Net.Sockets.SocketType::Stream, System.Net.Sockets.ProtocolType::Tcp);


    address = new System.Net.IPAddress(588384778);

    endPoint = new System.Net.IPEndPoint(address, 9100);

    socket.Connect(endPoint);

    sendCommand(contents);

    socket.Close();
}

In this form I get the compile error  "*** Error: 73, The class %1 does not contain this function" for the first line marked red. If I comment out this line (socket.Send(bytes);) I get a compile error in the second line marked red: "*** Error: -1, Syntax error.", the cursor then stands at the place marked with the black vertical line above.

I'm completely open to either use this approach to connect and send the data or another if that would be better/easier. Has anyone an idea how to solve this...?

Regards
Patrick

*This post is locked for comments

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

    The question is complicated by the fact that AX 4.0 is really old and most people don't have any AX 4.0 environment to try your code.

    It's the same with me, but I'll try an idea anyway.

    I suspect that that the problem is that your parameter of socket.Send() doesn't have the right type. AX tries to find Send() accepting a single Object and there is no such method, therefore it throws "... does not contain this function". You would have to declare the type as System.Byte[], but I suspect it's not supported by AX 4.0 (you can try it).

    If you can't make it work from X++, create a wrapper class that will hide all problematic parts and call this façade from X++ (I use it quite often because of generic types and such things).

  • Community Member Profile Picture
    on at

    Hello Martin,

    I've declared the bytes variable as CLRObject because the ...[]-declaration is not supported in AX 4.0. But strange enough it also doesn't work if I put the call ascii.GetBytes(_command + '\n') directly as parameter in the socket.Send()-call. Your tip to create a wrapper class sounds good, could you please give me a short explanation how/where to do this?

    Thanks!

    Regards
    Patrick

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

    It's actually pretty simple. You'll create a class library in Visual Studio and implement whatever you want there, using all your favorite features. What happens inside is invisible to AX, the important thing is to expose an interface that you can easily use from AX. You don't want generic types, explicitly implemented interfaces, events and all such stuff. You also can't pass reference types between AX and .NET. (The magic of proxy classes was added only in AX 2012).

    By the way, don't forget to make types and needed members public - I forget quite often. :)

    This is how your code could look like after moving all functionality to MySocketWrapper class:

    MyNamespace.MySocketWrapper socket = new MyNamespace.MySocketWrapper(588384778, 9100);
    ;
    socket.SendCommand(contents);

    (You can design it differently, of course.)

    When you have your class library, build it and copy the assembly to the bin folder of AX client and/or AOS (depending on where it will run). Also add the reference to AOT. (As you know, there is no such thing in AX 4.0 as VS projects in AOT and automatic deployment.)

    Then you can call your public types in X++ code through .NET Interop, you'll just use your own library instead of .NET Base Class Library.

  • Community Member Profile Picture
    on at

    Hello Martin,

    after some investigations and trial & error I managed to create the dll and to call the "send" method successfully from my AX 4.0-application. There were some obstacles to overcome (.NET-version of dll, registering the dll on the AX-server,...) but fortunately I could solve this issues.

    Thanks for your tips and information! :-)

    Regards
    Patrick

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