.Net Interop: Consume a third party web service (Check VAT Registration No.)

Some time ago, I did a session at Direction EMEA in Berlin. Apparently, some people were inspired with this. Not only did I introduce "cannibalism" (never expected this to be something "hot, new and inspiring" but rather something "obvious" - let's spend a blogpost on that as well.. :) ), but also I showed an example of consuming a third party web service. And some time ago (quite some time, actually), I promised one of the attendees to blog about the VAT example I showed there.

In fact, you can see this as "consuming a third party web service". There are tons of those out there .. and this example is quite useful, if you ask me (more useful than getting the weather info, or a list of songs of a certain genre, is it.. ;°) ).

What Web Service?

A functional consultant pointed me to a certain official webservice that had some promising capabilities. This is the website that uses that same webservice:

http://ec.europa.eu/taxation_customs/vies/

As you can see, you can validate the VAT Number in quite some European countries. This is something customers definitely want .. so what if we could build such a functionality in NAV. We would need a web service, right? Well, here it is, just for free:

http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl

Basically, using this web service, is kind of the same way as using any web service - so also quite the same as I described in this blogpost: NAV 2009 R2 .Net Interop: Calling a Web Service (come to think of it - I can make this blog quite short and let's share the objects I created here).

Building the Proxy

This will take the majority of your time - at least 2 minutes (because Visual Studio doesn't load very fast). I'm not going to create a service reference, but a web reference.

  • Create a new project in Visual Studio
  • Make sure you're using .Net Framework 3.5
  • Create a class library called "VATValidation" (at least I did)
  • So you get this:

Now, right click "References" and click "Service Reference":

As I mentioned, I'm going to use a web reference, so let's click Advanced / Add Web Reference and provide the url I mentioned above. Give it a proper name (I used "VATValidationService") and click "Add Reference":

That's it, people! Build you're solution and get the hell out of Visual Studio!

Deploy

You're safe now .. you're almost back in you familiar habitat (we like to call C/AL). Just first copy the dll (which is the proxy class you'll need in NAV) to your classic client and server:

  • Get the dll from yourproject\VATValidation\bin\Debug
  • Copy it to:
    • C:\Program Files (x86)\Microsoft Dynamics NAV\60\Classic\Add-ins
    • C:\Program Files (x86)\Microsoft Dynamics NAV\60\Service\Add-ins

(in my case, the server is running on my laptop, but obviously, you should put the dll wherever it will be used - NST, failover, ...). In this case, my NST will be executing the business logic, so I don't have to put it client-side.

Use

Let's go to nav and write some very simple code there - it's not functional in any way, it's just a test-codeunit that uses the service. You can build the business logic any way you like.

First declare some variables:

Name

DataType

Subtype

Length

CheckVATService

DotNet

'VATValidation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.VATValidation.VATValidationService.checkVatService

  

CountryCode

Code

  

10

VATNumber

Text

  

1024

Name

Text

  

1024

Address

Text

  

1024

Success

Boolean

  

  

Then, OnRun, I wrote these lines of code:

CheckVATService := CheckVATService.checkVatService(); //the constructor

CountryCode := 'BE';
VATNumber := '0885815777';
//VATNumber := '0863844386';
//VATNumber := '0891609053';

CheckVATService.checkVat(CountryCode,VATNumber,Success,Name,Address);

IF Success THEN
MESSAGE(Name + '\' + Address)
ELSE
MESSAGE('No Success');

I agree, the business logic is not useable at all .. but that was not the intention. I just provided you the way to use the CheckVat function .. and now it's up to you.

The Download

The download contains my test-objects from my demo-db. Take them "as is" .. so if you want to implement this at a customer, do that on your own risk - and reconsider the code (make it "partner ready" :-) ).

The download contains:

  • The RTC codeunit. You'll have to provide an action on some page to be able to run it.
  • A classic codeunit. Did't mention it above, but this is code what it would look like in the classic environment. It also works .. but who wants to write that? :-) It's just there for your reference/comparison.
  • The Proxy class. In the folder mentioned above, you'll find the dll that you'll have to deploy

Enjoy!

Posted: 2011-12-20 14:53 by waldo | with 5 comment(s) |
Bookmark and Share

Comments

# re: .Net Interop: Consume a third party web service (Check VAT Registration No.)

Great!!!

10x :)

Thursday, December 22, 2011 9:52 AM by N.Jordanov

# re: .Net Interop: Consume a third party web service (Check VAT Registration No.)

Good article, just one point:

We use this Web Service (not in Navision) for quite a time now.

If you can't reach the web service for some reason the result is not valid - although the VAT No. might be correct. You will definetly have to deal with that.

Monday, January 09, 2012 3:29 PM by Andy

# re: .Net Interop: Consume a third party web service (Check VAT Registration No.)

thanks for mentioning that!  

Monday, January 16, 2012 3:01 PM by waldo

# re: .Net Interop: Consume a third party web service (Check VAT Registration No.)

the weakeness of the system is that each country has responsiblity to have servers up. Some countries are better than others...

eg. my own country, Denmark, have an instable server sadly.

Thursday, February 23, 2012 5:52 PM by pedgaard

# re: .Net Interop: Consume a third party web service (Check VAT Registration No.)

That might explain my own problems sometimes when I'm unable to connect to the service.. :-).

Thursday, February 23, 2012 10:48 PM by waldo

Leave a Comment

(required) 
(required) 
(optional)
(required)