[AX40 SP2] - Consuming a Webservice

This question is not answered
Hi,
 
I want to consume a webservice from AX 40 SP2. The scenario is: A webservice created in .Net is already exposed and we would be consuming it from AX 40 SP2. I have not much clue on how to do this in AX40SP2 since all the material available over the internet either points to:
 
1. AX 30 webservices which is using the 'Webservice' class for this.
2. AX 40 which allows you to create and consume the webservice and 'Add reference' using the dll files which in my case is not possible since I dont have the dll file to consume the webservice.
3. AX 50 which allows you to consume a webservice by creating 'Add service reference' which I can not use in AX 40 SP2.
 
Hope the question is clear. Please let me know for any additional information.
You can also send me mail as well: Vishal.S.Kohli@Gmail.com
 
Cheers,
~ Vishal
 
 
All Replies
  • Hi Vishal,

    consuming web services is actually really simple.. it might look a bit confusing in the beginning, but you will love it once you get the hang of it.

    i wrote some quick code to help you out.. give it a shot...if you are able to get a response then we are 95% done :)

    static void MoWebServiceCall(Args _args)
    {
        COM             myXmlHttp;
        XMLDocument     xmlDocu;
        url             webServiceUrl;
        int             number1;
        int             number2;
        str             method;
        str             userName;
        str             password;
        ;
       
        myXmlHttp       = new COM('Microsoft.XMLHTTP'); // used ms xml http com object to communicate with the web service
       
        number1         = 5;
        number2         = 9;
       
        //format the url (i am passing paramters as a part of the url, you might want to encode this in a different manner)
        webServiceUrl   = strfmt("http://www.myWebservice.com/math.asmx/addNumbers?numOne=%1&numTWO=%2",number1,number2);
       
        method          = "GET"; // can be POST or PUT, etc
        username        = "Mohammed";
        password        = "www.dynamic-ax.co.uk"; // bad password..:)
       
        myXmlHttp.open(method,webServiceUrl,false,username,password);
       
        myXmlHttp.send();
        // how you call the web service is really depends on the serivce design
        // the followng calls are also valid
        // myXMLHttp.send("text : bla bla bla");
        // myXMLHttp.sendXML(xmldocu.text); //
       
        xmlDocu = XmlDocument::newXml(myXMLHttp.responseXML());
        // you can also use myXMLHttp.response(); // this returns text
       
        // what you do with the XML Document is really uptp you.
       
        info("Sum of the two munbers =" + xmlDocu.getNamedElement("SUM").text());
       
       
    }

    let me know if that works for you.

    feel free to contact me if you have any questions

    regards,

    Mohammed Rasheed

    www.dynamic-ax.co.uk 

    www.dynamic-ax.co.uk
  • Thanks for the help Rasheed, I am currently working on the code that you have sent. Although no success yet, I will update you once I get some success.

     Regards,
    Vishal

  • Hi Vishal,

      See this post. Hope it helps.

    http://daxguy.blogspot.com/2007/03/consuming-web-service-in-dax-4x.html

     

    Arijit Basu MVP - Microsoft Dynamics AX http://daxguy.blogspot.com
  • Hi Rasheed,

    Apologize for replying late. I have tried your code and was not able to make it work. My primary aim is to make the form 'Tutorial_WebServicesDemo' [available in AX 3.0] operational in AX 40 SP2 . From there, I picked up the currency conversion webservice URL and tried to use it in the code but seems like am missing something.

    static void WebService_001(Args _args)
    {
        COM             myXmlHttp;
        XMLDocument     xmlDocu;
        url             webServiceUrl;
        str             method;
        // Receive com
        Com             receiveCom;
        ;

        myXmlHttp       = new COM('Microsoft.XMLHTTP'); // used ms xml http com object to communicate with the web service

        //format the url (i am passing paramters as a part of the url, you might want to encode this in a different manner)
        webServiceUrl   = strfmt("http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl/getRate?countryOne=%1&countryTwo=%2","INR","USA");
        method          = "GET"; // can be POST or PUT, etc

        myXmlHttp.open(method,webServiceUrl,false);

        myXmlHttp.send();
        // how you call the web service is really depends on the serivce design
        // the followng calls are also valid
        // myXMLHttp.send("text : bla bla bla");
        // myXMLHttp.sendXML(xmldocu.text); //

        receiveCom  = myXMLHttp.responseXML(); // NEW

        //xmlDocu = XmlDocument::newXml(myXMLHttp.responseXML()); // Cottected to
        xmlDocu = XmlDocument::newXml(receiveCom.xml());

        // you can also use myXMLHttp.response(); // this returns text

        // what you do with the XML Document is really uptp you.

        //info("Sum of the two munbers =" + xmlDocu.getNamedElement("SUM").text());
        info("Exchange rate is:"+myXMLHttp.response());
    }
     
    This code gives the error "Root element is missing.".

    Best Regards,
    Vishal

  • Hi Vishal,

    try this out

    static void WebService_001(Args _args)
    {
        COM             myXmlHttp;
        XMLDocument     xmlDocu;
        url             webServiceUrl;
        str             method;
        // Receive com
        Com             receiveCom;
        ;

        myXmlHttp       = new COM('Microsoft.XMLHTTP');
        webServiceUrl   =  "http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=INR&ToCurrency=USD"


        method          = "GET";

        myXmlHttp.open(method,webServiceUrl,false);

        myXmlHttp.send();   

        receiveCom  = myXMLHttp.responseXML(); // NEW
        xmlDocu = XmlDocument::newXml(receiveCom.xml());

       
        info("Exchange rate is:"+ xmlDocu.GetNamedElement('double').text()); // cant remember the exact element name.. but u can look at the reply and figure that out
    }

    regards

    Mohammed Rasheed

    www.dynamic-ax.co.uk
  • Thanks a lot Rasheed, It was really a great help :)

    It worked just fine.

    I have documented what one needs to do for accessing a webservice for reference mentioned below:

    Reference website me in the code below that hosts the webservice: http://www.webservicex.net/
     
     
     
    Code:
     
    static void WebService_CurrencyConverter(Args _args)
    {
        COM             myXmlHttp;
        XMLDocument     xmlDocu;
        url             webServiceUrl;
        str             method;
        Com             receiveCom;
        ;
        myXmlHttp       = new COM('Microsoft.XMLHTTP');
        webServiceUrl   = strfmt("http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=USD&ToCurrency=INR");
        method          = "GET";
        myXmlHttp.open(method,webServiceUrl,false);
        myXmlHttp.send();
        receiveCom  = myXMLHttp.responseXML();
        xmlDocu = XmlDocument::newXml(receiveCom.xml());
        info("Exchange rate is:"+ xmlDocu.GetNamedElement('double').text());
    }
     
    static void WebService_SendSMS(Args _args)
    {
        COM             myXmlHttp;
        XMLDocument     xmlDocu;
        url             webServiceUrl;
        str             method;
        Com             receiveCom;
        Str             fromEmail, CountryCode, MobileNumber, txtMessage;
        ;
        myXmlHttp       = new COM('Microsoft.XMLHTTP');
        fromEmail       = 'xyz@Hotmail.com';
        CountryCode     = '91'; // replace the destination country code without prefixing '00' or '+'
        MobileNumber    = '99xxxxxxxx'; .//replace the destination number here without prefixing '0'
        txtMessage      = 'Message sent thru webservice';
        webServiceUrl   = strfmt("http://www.webservicex.net/sendsmsworld.asmx/sendSMS?FromEmailAddress=%1&CountryCode=%2&MobileNumber=%3&Message=%4",fromEmail, CountryCode, MobileNumber, txtMessage);
        method          = "GET";
        myXmlHttp.open(method,webServiceUrl,false);
        myXmlHttp.send();
        receiveCom  = myXMLHttp.responseXML();
        xmlDocu = XmlDocument::newXml(receiveCom.xml());
        info("Response from webservice is : "+ xmlDocu.GetNamedElement('string').text());
    }
     
     
    Hope this helps someone :)

    Cheersssss,
    Vishal

     

  • I am glad it worked for you... and thanks for posting your code for others... good stuff!

    Regards,

    Mohammed Rasheed

    www.dynamic-ax.co.uk

    www.dynamic-ax.co.uk
  • this is a correct working example

    public server static void sendXMLFileLIVE()
    {
        COM     myXmlHttp;
        url     webServiceUrl;
        str     method;
        str     userName;
        str     password;
        str     strXML;
        str     strTxt;
        str     result;
        str     fileName;
        str     listValue;
        int     listLength;
        int     i;
       

        System.Exception netExcepn;
        InteropPermission permission;
        FileIOPermission    fileIOPerm;
        COM             receiveCom;
        XMLDocument     xmlDocu;
        XMLDocument     doc = XMLDOcument::newBlank();
        XMLElement      node;
        XMLNodeList     elemlist;
        ;

        try {

        permission = new InteropPermission(InteropKind::ComInterop);
        permission.assert();

        myXmlHttp = new COM('Microsoft.XMLHTTP'); // used ms xml http com object to communicate with the web service
        webServiceUrl   = strfmt("https://gatewaybeta.fedex.com:443/web-services");
        method          = "POST";
        fileName = "c:\\temp\\getRateFile.xml";
        xmlDocu = XmlDocument::newBlank('utf-8');

        CodeAccessPermission::revertAssert();
        fileIOPerm = new FileIOPermission(fileName, 'r');
        fileIOPerm.assert();

        xmlDocu.load(fileName);

        CodeAccessPermission::revertAssert();
        permission = new InteropPermission(InteropKind::ComInterop);
        permission.assert();

        myXmlHttp.open(method,webServiceUrl,false,username,password);
        myXmlHttp.setRequestHeader("content-type","text/xml"); //application/x-www-form-urlencoded
        myXmlHttp.send(xmlDocu.toString());

        strTxt =strrtrim(myXMLHttp.responseText());
        info(strTxt);

        receiveCom  = myXMLHttp.responseXML();

        doc.loadXml(receiveCom.xml());
        elemList = doc.GetElementsByTagName("v3:Amount");
        listLength = elemList.length();

        for (i=0; i < elemList.length(); i++)
        {
         
            info(elemList.item(i).innerXml());
        }

        }
        catch (exception::CLRError)
        {
         netExcepn = CLRInterop::getLastException();
         info(netExcepn.ToString());
        }
        catch(exception::Error)
        {

         netExcepn = CLRInterop::getLastException();
         info(netExcepn.ToString());
        }  
    }

    xml file:

    <?xml version='1.0' encoding='UTF-8' ?>
    <soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:SOAP-ENC='http://schemas.xmlsoap.org/soap/encoding/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
        <soapenv:Body>
            <RateAvailableServicesRequest xmlns='http://fedex.com/ws/rate/v3'>
                <WebAuthenticationDetail>
                    <UserCredential>
                        <Key>-yourkey-</Key>
                        <Password>-password-</Password>
                    </UserCredential>
                </WebAuthenticationDetail>
                <ClientDetail>
                    <AccountNumber>-account no-</AccountNumber>
                    <MeterNumber>-meter no-</MeterNumber>
                </ClientDetail>
                <TransactionDetail>
                    <CustomerTransactionId>Rate Test</CustomerTransactionId>
                </TransactionDetail>
                <Version>
                    <ServiceId>crs</ServiceId>
                    <Major>3</Major>
                    <Intermediate>0</Intermediate>
                    <Minor>0</Minor>
                </Version>
                <Origin>
                    <StreetLines></StreetLines>
                    <City></City>
                    <StateOrProvinceCode>TN</StateOrProvinceCode>
                    <PostalCode>37115</PostalCode>
                    <CountryCode>US</CountryCode>
                </Origin>
                <Destination>
                    <StreetLines></StreetLines>
      <City></City>
                    <StateOrProvinceCode>TX</StateOrProvinceCode>
                    <PostalCode>73301</PostalCode>
                    <CountryCode>US</CountryCode>
                </Destination>
                <ShipDate>2008-12-15</ShipDate>
                <RateRequestPackageSummary>
                    <PieceCount>1</PieceCount>
                    <TotalWeight>
                        <Units>LB</Units>
                        <Value>10.00</Value>
                    </TotalWeight>
                </RateRequestPackageSummary>                                                           
            </RateAvailableServicesRequest>
        </soapenv:Body>
    </soapenv:Envelope>