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:
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
| 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); 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:
1
2
3
4
5
| // 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