{"id":1253,"date":"2013-01-03T09:25:36","date_gmt":"2013-01-03T09:25:36","guid":{"rendered":"http:\/\/www.navision-blog.de\/?p=1253"},"modified":"2013-01-04T14:02:15","modified_gmt":"2013-01-04T14:02:15","slug":"fsharp-and-dynamics-nav-2009-web-services","status":"publish","type":"post","link":"http:\/\/www.navision-blog.de\/blog\/2013\/01\/03\/fsharp-and-dynamics-nav-2009-web-services\/","title":{"rendered":"F# and Microsoft Dynamics NAV 2009 Web Services"},"content":{"rendered":"<p>If you are a Dynamics NAV developer you have probably heared of the web services feature which comes\u00a0with the 2009 version. In this <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd339004.aspx\">walkthrough<\/a>\u00a0you 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.<\/p>\n<p><a href=\"http:\/\/fsharp.org\/\">Microsoft Visual F# 3.0<\/a> comes with feature called &#8220;<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/hh156509.aspx\">type providers<\/a>&#8221; that helps to simplify your life in such a situation. For the case of WCF the\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/hh362328.aspx\">Wsdl type provider<\/a>\u00a0allows us to automate the proxy generation. Let&#8217;s see how this works.<\/p>\n<h4>Web service generation<\/h4>\n<p>In the first step we create the Dynamics NAV 2009 Codeunit web service in exactly the same way as in the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd339004.aspx\">MSDN walkthrough<\/a>.<\/p>\n<h4>Connecting to the web service<\/h4>\n<p>Now we create a new F# console project (.NET 4.0 or 4.5) in Visual Studio 2012 and add references to\u00a0<i>FSharp.Data.TypeProviders<\/i>,\u00a0<i>System.Runtime.Serialization<\/i> and\u00a0<i>System.ServiceModel<\/i>. After this we are ready to use the Wsdl type provider:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/4441943.js\"><\/script><\/p>\n<p>At this point the type provider creates the proxy classes in the background &#8211; no &#8220;add web reference&#8221; dialog is needed. The only thing we need to do is configuring the access security and consume the webservice:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/4441980.js\"><\/script><\/p>\n<h4>Access from C#<\/h4>\n<p>This is so much easier than the C# version from the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd339004.aspx\">walkthrough<\/a>. 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:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/4442051.js\"><\/script><\/p>\n<p>In the C# project you can now access the service like this:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/4442072.js\"><\/script><\/p>\n<h4>Changing the service<\/h4>\n<p>Now let&#8217;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.<\/p>\n<p>In the <a href=\"http:\/\/www.navision-blog.de\/2013\/01\/04\/fshar-and-dynamics-nav-2013-odata-services\/\">next blog post<\/a> I will show you how you can easily access a Dynamics NAV 2013 OData feed from F#.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are a Dynamics NAV developer you have probably heared of the web services feature which comes\u00a0with the 2009 version. In this walkthrough\u00a0you 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,222,448,5,3,13],"tags":[493,166,157,635],"_links":{"self":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1253"}],"collection":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/comments?post=1253"}],"version-history":[{"count":13,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1253\/revisions"}],"predecessor-version":[{"id":1260,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1253\/revisions\/1260"}],"wp:attachment":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/media?parent=1253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/categories?post=1253"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/tags?post=1253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}