Rash thoughts about .NET, C#, F# and Dynamics NAV.


"Every solution will only lead to new problems."

Thursday, 3. January 2013


F# and Microsoft Dynamics NAV 2009 Web Services

Filed under: C#,Dynamics NAV 2009,F#,Navision,Visual Studio,WCF — Steffen Forkmann at 9:25 Uhr

If you are a Dynamics NAV developer you have probably heared of the web services feature which comes with the 2009 version. In this walkthrough you can learn how to create and consume a Codeunit Web Service. This method works very well if you only need to create the C# proxy classes once. If you have more than one developer, an automated build system, changing web services or many web services then you will come to a point where this code generation system is very difficult to handle.

Microsoft Visual F# 3.0 comes with feature called “type providers” that helps to simplify your life in such a situation. For the case of WCF the Wsdl type provider allows us to automate the proxy generation. Let’s see how this works.

Web service generation

In the first step we create the Dynamics NAV 2009 Codeunit web service in exactly the same way as in the MSDN walkthrough.

Connecting to the web service

Now we create a new F# console project (.NET 4.0 or 4.5) in Visual Studio 2012 and add references to FSharp.Data.TypeProvidersSystem.Runtime.Serialization and System.ServiceModel. After this we are ready to use the Wsdl type provider:

At this point the type provider creates the proxy classes in the background – no “add web reference” dialog is needed. The only thing we need to do is configuring the access security and consume the webservice:

Access from C#

This is so much easier than the C# version from the walkthrough. But if you want you can still access the provided types from C#. Just add a new C# project to the solution and reference the F# project. In the F# project rename Program.fs to Services.fs and expose the service via a new function:

In the C# project you can now access the service like this:

Changing the service

Now let’s see what happens if we change the service. Go to the Letters Codeunit in Dynamics NAV 2009 and add a second parameter (allLetters:Boolean) to the Capitalize method. After saving the Codeunit go back to the C# project and try to compile it again. As you can see the changes are directly reflected as a compile error.

In the next blog post I will show you how you can easily access a Dynamics NAV 2013 OData feed from F#.

Tags: , , ,

3 Comments »

  1. Very useful post.
    I was just looked for a way to authenticate using Wsdl Type Provider. The same approach works for SharePoint 2010 services.
    Thank you!

    Comment by Sergey Tihon — Thursday, 3. January 2013 um 21:20 Uhr

  2. […] Steffen Forkmann blogged “F# and Microsoft Dynamics NAV 2009 Web Services“. […]

    Pingback by F# Weekly #1, 2013 « Sergey Tihon's Blog — Sunday, 6. January 2013 um 21:02 Uhr

  3. Hello.
    I tried to create F# console project as it is described in this post.
    But it doesn’t work. When I try to build solution a strange Error appears:
    The type provider ‘Microsoft.FSharp.Data.TypeProviders.DesignTime.DataProviders’ reported an error: ЋиЁЎЄ : ЌҐ г¤ Ґвбп Ї®«гзЁвм ¬Ґв ¤ ­­лҐ Ё§ http://navsql:7047/dynamicsnavKPA/WS/KAZPROM/Codeunit/DemoCU?WSDL

    Do you have any idea?
    Thanks

    Comment by Valery — Sunday, 28. April 2013 um 22:40 Uhr

RSS feed for comments on this post. | TrackBack URI

Leave a comment

XHTML ( You can use these tags): <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> .