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

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Getting IP address of a system through code in ax 2009

(0) ShareShare
ReportReport
Posted on by 110

I want an alternative code for getting IP address of a system through code in AX 2009. Most of the blogs, when i searched in google, showed me the below code.

static void FetchIpAddress (Args _args)
{
System.String hostName = System.Net.Dns::GetHostName();
System.Net.IPHostEntry hostEntry = System.Net.Dns::GetHostEntry(hostName);
System.Net.IPAddress[] addresses = hostEntry.get_AddressList();
System.Net.IPAddress address;
System.Net.Sockets.AddressFamily addressFamily;
System.Collections.IEnumerator enumerator = addresses.GetEnumerator();

while (enumerator.MoveNext())
{
address = enumerator.get_Current();
addressFamily = address.get_AddressFamily();
if (addressFamily == System.Net.Sockets.AddressFamily::InterNetwork)
{
info(address.ToString());
}
}
}

If there is any alternative code, other than the above code, please share the code. I'm in great need.

*This post is locked for comments

I have the same question (0)
  • manasa133 Profile Picture
    110 on at
    RE: Getting IP address of a system through code in ax 2009

    Its working fine.  The CLR interop permission was missing.

    Thanks Martin and Noel.

  • manasa133 Profile Picture
    110 on at
    RE: Getting IP address of a system through code in ax 2009

    Thanks Douglas Noel :) :)

  • Verified answer
    Douglas Noel Profile Picture
    3,905 on at
    RE: Getting IP address of a system through code in ax 2009

    Hi,

    maybe you've missed the CLR interop permission? I can only detect the COM Interop permission.

    if you need both you've to give both - something like this:

       InteropPermission   permCOM;

       InteropPermission   permCLR;

       Set                 permSet;

       ;

       permSet = new Set(Types::Class);

       permCOM = new InterOpPermission(InteropKind::ComInterop);

       permCLR = new InterOpPermission(InteropKind::ClrInterop);

       permSet.add(permCOM);

       permSet.add(PermCLR);

       CodeAccessPermission::assertMultiple(permSet);

       //

       //your code method calls ... (the parts run on server) can now use COM and CLR

       //

       CodeAccessPermission::revertAssert();

    But again, as already described by Martin: Running the report on server tier only will lead to obtaining the IP address(es) from the connected AOS server, not the client. If you don't need to run this report in batch, you MUST use a static client method to get this information.

    regards

    Douglas

  • manasa133 Profile Picture
    110 on at
    RE: Getting IP address of a system through code in ax 2009

    Class name is "AXLog_ext".  "CreateLine" is the Method

    I'm calling the class in executesection of the report.

    public void executeSection()

    {

       AXLog_ext aXLog_ext = new AXLog_ext();

       Set               permissionSet;

       InteropPermission interopPermission;

       ;

       interopPermission = new InteropPermission(InteropKind::ComInterop);

       permissionSet =  new Set(Types::Class);

       permissionSet.add(interopPermission);

       CodeAccessPermission::assertMultiple(permissionSet);

       aXLog_ext.CreateLine(startdatetime, ObjecttName ,enum2str(element.report().applObjectType()));

       super();

    }

    The  method of class " AXLog_ext " contains the below code.

    public void CreateLine(utcdatetime _startdatetime, str _ObjectName, str _ObjectType)

    {

       COM adoConn;

       COM recordSet;

       COM fields;

       COM field;

       COMVariant fieldValue;

       System.String                       hostName = System.Net.Dns::GetHostName();

       System.Net.IPHostEntry              hostEntry = System.Net.Dns::GetHostEntry(hostName);

       System.Net.IPAddress[]              addresses = hostEntry.get_AddressList();

       System.Net.IPAddress                address;

       System.Net.Sockets.AddressFamily    addressFamily;

       System.Collections.IEnumerator      enumerator = addresses.GetEnumerator();

       str                                 IPaddress, hname, sqlquery;

       ;

       while (enumerator.MoveNext())

       {

           address = enumerator.get_Current();

           addressFamily = address.get_AddressFamily();

           if (addressFamily == System.Net.Sockets.AddressFamily::InterNetwork)

           {

               IPaddress = address.ToString();

           }

       }

       hname = hostname;

       try

       {

           sqlquery = "insert into AxReportUsageDetails(UserId, StartDateTime, EndDateTime, Timelapsed, ObjectName, ObjectType, ClientsIP, HostName)";

           sqlquery += " values ('" +  XUserInfo::find(false, curUserId()).name + "','"

            + datetime2Str(_startdatetime) + "','" + datetime2Str(DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::utcNow(), TimeZone::GMTPLUS0530CHENNAI_KOLKATA_MUMBAI)) + "','"

            + int2str(DateTimeUtil::getDifference(DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::utcNow(), TimeZone::GMTPLUS0530CHENNAI_KOLKATA_MUMBAI), _startdatetime)) + "', '"

            + _ObjectName + "', '" + _ObjectType + "', '" + IPaddress + "', '" + hname + "')";

           adoConn = new COM("ADODB.Connection");

           adoConn.open(@"Provider=SQLOLEDB;Integrated Security=SSPI;Data Source=ITSD-01\SQLEXPRESS;Initial Catalog=AxaptaReportUsage;");

           recordSet = adoConn.execute(sqlquery);

       }

       catch(Exception::Error)

       {

       }

       adoConn.close();

    }

  • manasa133 Profile Picture
    110 on at
    RE: Getting IP address of a system through code in ax 2009

    Request for the permission of type 'InteropPermission' failed.

    (S)\Classes\InteropPermission\demand

    (S)\Classes\CLRInterop\staticInvoke

    (S)\Classes\AXLog_ext\CreateLine - line 30

    (S)\Reports\LedgerJournal\Designs\ReportDesign1\AutoDesignSpecs\Epilog:Epilog\Methods\executeSection

    (S)\Classes\ReportRun\print

    (S)\Classes\ReportRun\run

    (S)\Classes\SysReportRun\run - line 26

    (S)\Classes\RunBaseReport\run - line 34

    (S)\Classes\SysReportRun\run - line 15

    (S)\Classes\MenuFunction\runServer

    Its the same error, even after giving permissions.

  • Martin Dráb Profile Picture
    236,275 Most Valuable Professional on at
    RE: Getting IP address of a system through code in ax 2009

    You can run a client static method, for example. But don't forget to check if there is any client as all, because reports can run in batch and there is no client in that case to execute client-bound code.

    It's still not clear to me what error are you getting. You'll have to be more explicit.

  • manasa133 Profile Picture
    110 on at
    RE: Getting IP address of a system through code in ax 2009

    Thanks Martin Drab.  

    Now i realized that it's the problem while capturing the client's IP.

    Are there any ways to capture the client's IP?

    Thanks in advance,

    Manasa  

  • manasa133 Profile Picture
    110 on at
    RE: Getting IP address of a system through code in ax 2009

    Along with the IP address, i'm also capturing other details like UserId, Report Start Time and Date, Report End Time and Date, Report name. If i comment the IP address code, then Report gets generated without any error. But IP will not be saved in the external database, whereas all other details gets saved.

    How do i alter the code in order to capture the client's IP?

  • Martin Dráb Profile Picture
    236,275 Most Valuable Professional on at
    RE: Getting IP address of a system through code in ax 2009

    If you're still getting InteropPermission, you still haven't set code access permissions correctly. If you're now getting a different error, don't you think it would be useful if you tokd us what the error said, at least?

    Even if it worked, it wouldn't meet your requirement ("to capture the report user's details"), because the code runs on server and therefore you would get IP addresses of your AOS server. You would have to run it in a client-bound piece of code.

  • manasa133 Profile Picture
    110 on at
    RE: Getting IP address of a system through code in ax 2009

    I have a class wherein, i have added the above code. The class also has got code to save the IP address and the hostname of a system to an external database.  I'm calling the class in all the reports in order to capture the IP and hostname. The main objective of doing this is to capture the report user's details. The code is working fine in the development, whereas it is throwing error for the users.

    Error:

    Request for the permission of type 'InteropPermission' failed.

    (S)\Classes\InteropPermission\demand

    (S)\Classes\CLRInterop\staticInvoke

    (S)\Classes\AXLog_ext\CreateLine - line 9

    (S)\Reports\LedgerJournal\Designs\ReportDesign1\AutoDesignSpecs\Epilog:Epilog\Methods\executeSection

    (S)\Classes\ReportRun\print

    (S)\Classes\ReportRun\run

    (S)\Classes\SysReportRun\run - line 26

    (S)\Classes\RunBaseReport\run - line 34

    (S)\Classes\SysReportRun\run - line 15

    (S)\Classes\MenuFunction\runServer

    (C)\Classes\FormFunctionButtonControl\Clicked

    (C)\Classes\FormMenuButtonControl\Clicked

    I managed to give permisssion. But now the  line number in the error is pointing to

    "System.String hostName = System.Net.Dns::GetHostName();"

    How do i come out of this. And why is it only the users are getting this error?

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Community Member Profile Picture

Community Member 4

#2
Guy Terry Profile Picture

Guy Terry 2 Moderator

#2
Nayyar Siddiqi Profile Picture

Nayyar Siddiqi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans