Friday, June 7, 2013

Call WebService from AX

Create class library project in VS, add services references, add to AOT, and deploy the project to both AX server and client.
In AX, add code some reference code.

VERAxServicesNameServcies.AxCustomerSvc.CustomerClient customerClient;
    VERAxServicesNameServcies.AxCustomerSvc.ICustomer customer;
    System.Exception Exception;
     System.ServiceModel.Description.ServiceEndpoint endPoint;
    System.ServiceModel.EndpointAddress endPointAddress;
    System.Type type;
    ;
    try
    {
        // Create a service client proxy
        type= CLRInterop::getType('VERAxServicesNameServcies.AxCustomerSvc.CustomerClient');
        customerClient = AifUtil::createServiceClient(type);
          // Create and endpoint address, This should be a parameter stored in the system
        endPointAddress = new System.ServiceModel.EndpointAddress("http://10.242.25.20:8081/Customer.svc");
        // Get the WCF endpoint
        endPoint = customerClient.get_Endpoint();
        // Set the endpoint address.
        endPoint.set_Address(endPointAddress);
        // Use the zipcode to find a place name
        //customer= customerClient.GetCustomer(1);
        // Use the getAnyTypeForObject to marshal the System.String to an
        //Ax anyType
        // so that it can be used with info()
        info(strFmt('%1', CLRInterop::getAnyTypeForObject(customerClient.GetCustomerTest(1))));
    }
    catch
    {
        // Get the .NET Type Exception
        exception = CLRInterop::getLastException();
        // Go through the inner exceptions
        while(exception)
        {
            // Print the exception to the infolog
            info(CLRInterop::getAnyTypeForObject(exception.ToString()));
            // Get the inner exception for more details
            exception = exception.get_InnerException();
        }
    } 

No comments:

Post a Comment