Using Reflection to call NAV 2009 Web Services

Sometimes I just get an idea and want to see if I can get it working, and in the case of this one, I couldn’t think of a practical use for what I finished up with. Who knows, maybe you’ll find some use for it.

The idea started life as a .NET application that would help me get the URLs for every possible Web service that is exposed by NAV, I know I can do this using Internet Explorer, but I wanted something where I could just enter the machine name, port, and other details and get a list of URLs I can copy from. I guess that would still be fairly useful. What I ended up with was a .NET program that could call any NAV Web service with nothing more than a URL provided as a starting point. What was interesting was that, along the way, I learnt how to read the WSDL from our NAV Web service URL using Windows Credentials, and I also learnt how to set a properties on an instance of a Web service using reflection – something that I previously knew very little about.

There are a number of examples of how to expose NAV Pages and Codeunits as Web services using NAV 2009 and I worked through a few examples in my book. The sample chapter on Web services can be read online as a PACKT Article, there is also a wealth of information on this topic on various blogs and of course the Microsoft Dynanmics NAV Developer's Centre. My starting point with this example was to create the NAV Codeunit with my ConvertStrToUpperCase function that I wrote about in the book. I then expose this from NAV using the Web service form with a name of NAV Codeunit. In the NAV 2009 SP1 Marketing Beta VPC, this gives me the following URL for my Web service:

http://localhost:7047/DynamicsNAV/WS/CRONUS%20International%20International%20Ltd/Codeunit/NAV_Codeunit

Here is the program running:

image

When I run the program I enter the URL in the Web Service URL field and click the Load Service button.

The NAV_Codeunit appears in my list of available Services.

The ConvertStrToUpperCase appears in my list of Available Methods.

Select the CovertStrToUpperCase method and then type a parameter in to the Method Parameters box and click the Invoke Method button.

If everything is working on your virutal PC (that is you have the Web services running and I followed the explanation so far) you should see the uppercase text appear in the Method Return Value text box.

There are some limitations to how this works. For example, if you try to use the SystemService Web service in order to call the Companies method, the program won’t display the results. This is because the code to display the result is expecting a single value and not an array of values. It’s not that hard to change this.

Nearly all of the code was found online on at http://www.crowsprogramming.com/archives/66 and you can download the solution there and compile the executable. When I did this, I had problems trying to read the WSDL. I was getting an authentication error and I figured I needed to somehow tell the system to use my Windows Credentials when trying to get the WSDL for the Web service.

I found out how to do this on a post by Rick Strahl of West Wind Technologies. I had to change the BuildAssemblyFromWSDL method in the WebServiceInvoker.cs file. I have put some comments around the code so that you can see what has been changed.

After this change, I was able to see the methods, but I could not invoke them. I figured I needed to set the UseDefaultCredentials property of my service but I wasn’t sure how to do this. I actually managed to do this through guesswork. I have commented the changes in the InvokeMethod method.

You can download the .NET solution here and compile it up using the Visual Studio 2008 on the NAV 2009 SP1 Marketing Beta VPC image. If you think of a use for this, please let me know.

Related
Recommended