Changing EndpointAddress while consuming an external webservice

This post is intended to show you guys something that was quite useful for me once. I was working in an environment full of third-party integrations basically made using webservices.

We have a very good walkthrough available at Microsoft TechNet – Calling an External Web Service from X++ – which can give you the needed direction:

https://technet.microsoft.com/en-us/library/hh500185.aspx

But once you create a Class Library to consume a webservice, the endpoint address is stored inside it. When you have different environments such as development, testing and production, you can face some trouble managing the URLs.

If you want to do a quick test, validate the address or for any other testing purposes, this example can come in handy:

public static void main(Args args)
{
    DynamicsAXPizzaService.WebService1.Service1SoapClient   wcfClient;
    DynamicsAXPizzaService.WebService1.PizzaInfo[]          pizzaInfoArray;
    DynamicsAXPizzaService.WebService1.PizzaInfo            pizzaInfo;
 
    System.ServiceModel.Description.ServiceEndpoint         endPoint;
    System.ServiceModel.EndpointAddress                     endPointAddress;
    System.Exception                                        ex;
    System.Type                                             type;
    int                                                     i, numOfPizzas;
 
    str name, description, prize;
    ;
 
    try
    {
        type            = CLRInterop::getType('DynamicsAXPizzaService.WebService1.Service1SoapClient');
        wcfClient       = AifUtil::createServiceClient(type);
 
        endPointAddress = new System.ServiceModel.EndpointAddress("http://localhost/service1.asmx");
        endPoint        = wcfClient.get_Endpoint();
        endPoint.set_Address(endPointAddress);
 
        pizzaInfoArray  = wcfClient.SearchPizza("mozarella");
        numOfPizzas     = pizzaInfoArray.get_Count();
 
        for(i = 0; i < numOfPizzas; i++)
        {
            pizzaInfo   = pizzaInfoArray.get_Item(i);
            name        = pizzaInfo.get_Name();
            description = pizzaInfo.get_Description();
            prize       = pizzaInfo.get_Prize();
 
            info(strFmt("%1 - %2 - %3", name, description, prize));
        }
    }
    catch(Exception::CLRError)
    {
        ex = CLRInterop::getLastException();
 
        while(ex)
        {
            info(CLRInterop::getAnyTypeForObject(ex.ToString()));
            ex = ex.get_InnerException();
        }
    }
}

Take a better look at this part:

//
    endPointAddress = new System.ServiceModel.EndpointAddress("http://localhost/service1.asmx");
    endPoint        = wcfClient.get_Endpoint();
    endPoint.set_Address(endPointAddress);
//

What we’re doing here is basically setting the Endpoint Address the same way we did creating the Service Reference back in the Visual Studio. However, this will override the address we’ve put there.

I also recommend using Fiddler so you can track your TCP/UDP packets as well as size and other information.

See you guys next time 🙂