Connecting to NAV Web Services from C# using Service Reference (config file version)


Please read this post to get a brief explanation of the scenario I will implement in C# using Web References.

For C# we can leave the Service Tier running Negotiate or we can use Ntlm as PHP and Java. In this example I will assume that the Service Tier is running SPNEGO (which is the default)

BTW. Basic knowledge about C# is required to understand the following post:-)

Version and download

I am using Visual Studio 2008 professional with SP1 when writing this sample, to be honest I have NOT tried to see whether this will work in the Express versions of Visual Studio, but they do have Service Reference and Web Reference so I cannot see why not.

What is the difference between a Web Reference and a Service Reference?

In short, the Web Reference is a .net 2.0 compatible Web Service reference, the Service Reference is a .net 3.5 WCF based Service Reference.

Add Web Reference is a wrapper over wsdl.exe and can be used to create proxies for .NET 1.1 or 2.0 clients. Of course this means when you are pointing to a WCF service you have to be pointing to an endpoint that uses basicHttpBinding.

Add Service Reference is a wrapper over svcutil.exe and also creates clients proxies. These proxies, however, can only be consumed by .NET 3.5 clients.

In this post I will describe how to use Service References, where all settings are stored in the .config file.

How to add a Service Reference

Select Add Service Reference


Type the URL and namespace for the SystemService Service Reference:


and for the Customer Page Service Reference:


The .config file

After having added these Service References, all the properties and settings about the references are stored in app.config (which gets autocreated by Visual Studio). The Proxy classes are generated and everything seems fine until you start using it.

You have to change a couple of things in the app.config file before using these.

Under every binding configuration setting you will find a section like this:

<security mode="None">
    <transport clientCredentialType="None" proxyCredentialType="None"
        <extendedProtectionPolicy policyEnforcement="Never" />
    <message clientCredentialType="UserName" algorithmSuite="Default" />

this does not match whats needed for NAV Web Services. NAV Web Services absolutely do not run without security. You will have to change this section with:

<security mode="TransportCredentialOnly">
  <transport clientCredentialType="Windows"  />

which matches the security mode and transport of the binding used by NAV when using Windows Authentication (SPNEGO). If the Service Tier is setup to run Ntlm – the ClientCredentialType needs to be “Ntlm” in the config file.

Furthermore you will have to add a behavior indicating that the Web Service Listener is allowed to use Delegation on your credentials (between </bindings> and <client>:

    <behavior name="allowDelegation">
        <windows allowedImpersonationLevel="Delegation"

and last, but not least you will have to add this behavior to all endpoints like:

<endpoint address="http://localhost:7047/DynamicsNAV/WS/CRONUS%20International%20Ltd/Page/Customer"
          name="Customer_Port" />

If we strip away all the unnecessary defaults and modify a couple of things by hand, the ENTIRE config file could look like this:

<?xml version="1.0" encoding="utf-8" ?>
        <binding name="NavWSBinding">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Windows"  />
        <behavior name="allowDelegation">
            <windows allowedImpersonationLevel="Delegation"
      <endpoint address="
                name="Customer_Port" />
      <endpoint address="
                name="SystemService_Port" />


The code

First a couple of using statements (including the two reference namespaces) and the main body of a console app:

using System;
using System.Net;
using testAppWCF2.SystemServiceRef;
using testAppWCF2.CustomerPageRef;

namespace testAppWCF2
    class Program
        static void Main(string[] args)
            // main program code

The main code follows.

First, connect to the System Web Service and list all companies:

string baseURL = "http://localhost:7047/DynamicsNAV/WS/";

// Create the SystemService Client
SystemService_PortClient systemService = new SystemService_PortClient("SystemService_Port", baseURL + "SystemService");

string[] companies = systemService.Companies();
foreach (string company in companies)
string cur = companies[0];

Note, that when creating a System Service Client, I specify the name of an endpoint configuration and a URL. I didn’t have to specify anything, then all defaults would be taken from the Config file, but I like to show how you can calculate the URL and specify that at runtime.

Now I have the company I want to use in cur and the way I create a URL to the Customer page is by doing:

string customerPageURL = baseURL + Uri.EscapeDataString(cur) + "/Page/Customer";
Console.WriteLine("\nURL of Customer Page: " + customerPageURL);

and then I can create a Service Class to the Customer Page by specifying the config section and a URL again:

// Create the SystemService Client
Customer_PortClient customerService = new Customer_PortClient("Customer_Port", customerPageURL);

and using this, I read customer 10000 and output the name:

Customer customer10000 = customerService.Read("10000");
Console.WriteLine("\nName of Customer 10000: " + customer10000.Name);

Last, but not least – lets create a filter and read all customers in GB that has Location Code set to RED or BLUE:

Customer_Filter filter1 = new Customer_Filter();
filter1.Field = Customer_Fields.Country_Region_Code;
filter1.Criteria = "GB";

Customer_Filter filter2 = new Customer_Filter();
filter2.Field = Customer_Fields.Location_Code;
filter2.Criteria = "RED|BLUE";

Console.WriteLine("\nCustomers in GB served by RED or BLUE warehouse:");
Customer_Filter[] filters = new Customer_Filter[] { filter1, filter2 };
Customer[] customers = customerService.ReadMultiple(filters, null, 0);
foreach (Customer customer in customers)

Console.WriteLine("\nTHE END");

As you can see the code is actually as simple as the Web Reference version, but the config file complicates things a lot. All of the above will output the following to a console prompt (on my machine running NAV 2009SP1 W1)



BTW – this sample will by default run Windows Authentication. If you want to specify a different user you will need to set the ClientCredential property like this:

customerService.ClientCredentials.Windows.ClientCredential = new NetworkCredential("user", "password", "domain");

You would need to set this on each Service Client you create.

I hope this is helpful.

Good luck

Freddy Kristiansen
PM Architect
Microsoft Dynamics NAV

Comments (10)
  1. Lukas says:

    Hi Freddy,

    how can I set the culture for a service request, for example: if the service request fails with an exception

    that was generated by NAV (ERROR, TESTFIELD, ecc.) then the exception message is allways in english.

    How can I do to get the message in german, like in the NAV GUI when the current language is german.


  2. denis Exel says:


    Worth mentioning that, here, Visual Studio service reference needs to be case sensitive!

    Great intro to wcf consumption!

  3. greenwebapp says:

    I looking for example about .

    Webservice with c# console app and web service.

    For my web

  4. std says:

    Very bad tutorial  ,i found this article not helpful

  5. student says:

    remove this article or modify all This Example

    if you want to make tutorial make it understandable for other . <<<<< you should make it clear and understandable >>> all this example is wrong ..

  6. reg says:

    Thank you so much for this article. After long browsing, I finally found what I needed.

  7. peter says:

    Great article, this is exactly what I needed, thanks!

  8. Mohammad says:

    This really helped me to overcome authorization issue. Thank you so much.

  9. Rob says:

    Interesting tutorial, I'd love to see an example for NAV 2015 for windows auth and for NAV user password authentication.

  10. Julien says:


    Thanks for this article !!

    I have a question: I can't manage having a 2nd filter on an "option" field. Is there a specific  way to write it ? (for example with CrossReference,  the Cros Reference Type is an option. When I write the value between quotes like this:

    RefFiltre2.Criteria = "Code barre"

    I get an error "the filter "code barre" is not valid for the field. The value "code barre" can't be evaluated in option type

    Thanks … 😉

Comments are closed.

Skip to main content