web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Rashed Amini / Web service Proxy for NAV W...

Web service Proxy for NAV Web service

Rashed Profile Picture Rashed 3,765

As many of you know Dynamics NAV 2009 is using windows authentication to allow any web services to be consumed. Currently Nav 2009 when it negotiates to negotiate it only uses Kerberos and doesn’t fall back on NTML. This creates connection issues with Non Windows systems. For example Java based system (SOAPUI), or php based website running on Linux servers. To solve this problem you have to write a web service wrapper that allows you to bypass this problem. In this blob I will go through the process to create a WCF .NET web service that runs as windows service and proxies the communications for NAV. In this example I will use soap toolkit instead of referencing NAV web service. But the idea is the same.

In example below I will write a proxy for NAV web service “RunJob” (shown below).

This Codeunit simply takes two parameters, Object type and Object ID and runs that object. In the function I’m simply using “Job Queue Start Codeunit” to run the object and catch any errors which is returned as string.

  • First I will create a new console application in Visual Studio. I will name it NavWinProxy

  • Next rename program.cs to Service.cs

  • I will need to add references to the following assemblies.
    • System.ServiceModel.dll, System.ServiceProcess.dll, System.Configuration.Install.dll

     

  • Next add the following statements to the service.cs
    • using System;
    • using System.Text;
    • using System.ComponentModel;
    • using System.ServiceModel;
    • using System.ServiceProcess;
    • using System.Configuration;
    • using System.Configuration.Install;
    • using System.Net;
    • using System.Xml;
    • using System.IO;
  • Next define service contract as shown in below. Notice that I’ve created a function called RunObject that takes 3 parameters.
    • [ServiceContract(Namespace = “”)]
    • public
      interface
      INavWinProxy
    • {
    • [OperationContract]
    • string RunObject(string ObjectType, int ObjectID, string CompanyName);
    • }
  • Next Implement the service contract in class called RunNavObject as shown below. In the implementation I decided to use webrequest and build the xml document. You can also add the RunJob as web Reference and call it. The only issue is that you have add each company as web reference and call that for that company. You can also dynamically reference the web service. You can read more about it on Freddy’s Blog.
    • public
      class
      NAVService : INavWinProxy
    • {
    • public
      string RunObject(string ObjectType, int ObjectID, string CompanyName)
    • {
    • string Body = @”<?xml version=”"1.0″” encoding=”"utf-8″”?><soap:Envelope xmlns:soap=”"http://schemas.xmlsoap.org/soap/envelope/”">” +
    • @”<soap:Body><RunJob xmlns=”"urn:microsoft-dynamics-schemas/codeunit/RunJob”">” +
    • @”<objectType>” + ObjectType + “</objectType><objectID>” + ObjectID + “</objectID>” +
    • @”</RunJob></soap:Body></soap:Envelope>”;
    • WebRequest req = WebRequest.Create(”http://localhost:7047/DynamicsNAV/WS/” + CompanyName + “/Codeunit/RunJob”);
    • req.Headers.Add(”SOAPAction”, “\”urn:microsoft-dynamics-schemas/codeunit/RunJob:RunJob\”");
    • req.ContentType = “text/xml;charset=\”utf-8\”";
    • req.Method = “POST”;
    • req.UseDefaultCredentials = true;
    • Stream strWrite = req.GetRequestStream();
    • StreamWriter sw = new
      StreamWriter(strWrite);
    • sw.Write(Body.ToString());
    • sw.Close();
    • WebResponse wr = req.GetResponse();
    • HttpWebResponse httpRes = (HttpWebResponse)wr;
    • Stream s = httpRes.GetResponseStream();
    • StreamReader sr = new
      StreamReader(s, Encoding.ASCII);
    • XmlDocument xmlDoc = new
      XmlDocument();
    • xmlDoc.Load(sr);

 

  • sr.Close();
  • return xmlDoc.InnerXml;
  • }
  • }
  • Next create a new class NavWindowService that inherits from ServiceBase class. Add the necessary code and define the Main method. Overwrite the Onstop and OnStart methods. And remove the default class that Visual Studio added.
    • public
      partial
      class
      NAVWindowsService : ServiceBase
    • {
    • public
      ServiceHost serviceHost = null;
    • public NAVWindowsService()
    • {
    • // Name the Windows Service
    • ServiceName = “NAVWindowsService”;
    • }
    • public
      static
      void Main()
    • {
    • ServiceBase.Run(new NVWindowsService());
    • }
    • protected
      override
      void OnStart(string[] args)
    • {
    • if (serviceHost != null)
    • {
      • serviceHost.Close();
    • }
    • serviceHost = new
      ServiceHost(typeof(NAVService));
    • serviceHost.Open();
    • }
    • protected
      override
      void OnStop()
    • {
    • if (serviceHost != null)
    • {
      • serviceHost.Close();
      • serviceHost = null;
    • }
    • }
    • }
  • The last class we need to add is ProjectInstaller class. This will allow our program to be installed as windows service using Installutil.exe tool.

[RunInstaller(true)]

public
class
ProjectInstaller : Installer

{

private
ServiceProcessInstaller process;

private
ServiceInstaller service;

 

public ProjectInstaller()

{

process = new
ServiceProcessInstaller();

process.Account = ServiceAccount.LocalSystem;

service = new
ServiceInstaller();

service.ServiceName = “NAVWindowsService”;

Installers.Add(process);

Installers.Add(service);

}

}

  • The Last Step in the process is to add an application configuration file to the project. This xml file will contact web service settings.


Replace the content of the config file with the following. Change the property of xml file “Copy to Output Directory” to “Copy if newer”. Something very important thing to about config file is that binding is set to basicHttpBinding. This allows non nonwindows programs or Silverlight apps to connect to the web service.

<?xml
version=”1.0″
encoding=”utf-8″ ?>

<configuration>

    <system.serviceModel>

        <services>

            <service
name=”NavWinProxy.NAVService”

                     behaviorConfiguration=”NAVServiceBehavior”>

                <host>

                    <baseAddresses>

                        <add
baseAddress=”http://localhost:8000/service”/>

                    </baseAddresses>

                </host>

                <!– this endpoint is http://localhost:8000/service –>

                <endpoint
address=”"
binding=”basicHttpBinding”
contract=”NavWinProxy.INavWinProxy” />

            </service>

        </services>

        <behaviors>

            <serviceBehaviors>

                <behavior
name=”NAVServiceBehavior”>

                    <serviceMetadata
httpGetEnabled=”true”/>

                    <serviceDebug
includeExceptionDetailInFaults=”true”/>

                </behavior>

            </serviceBehaviors>

        </behaviors>

    </system.serviceModel>

</configuration>

 

Now that we are done, we can build the solution. Build->Build NavWinProxy.

To install it as windows server I’m going to use Visual Studio command prompt.

You can also use regular command prompt. It just you have to navigate to wherever Installutil.exe is and then run the following command

Installutil.exe “C:\Documents and Settings\YOURUSERNAME\My Documents\Visual Studio 2008\projects\NavWinProxy\NavWinProxy\bin\Debug\NavWinProxy.exe”

You’ll need to change the directory ofcourse to the location where your visual studio project resides. Once you run the command above, you will get a message that the transacted install completed. In windows services you should see NAVWindowsService. To uninstall the service simply add –u parameter. installutil.exe –u “Location of the exe”

 

You’ll need to click on properties of this service and change it to login as specific windows user and change it to start automatic if you like. You’ll need to check evenlog to see that it started correctly

If you get any error related to

Service cannot be started. System.InvalidOperationException: Service ‘NavWinProxy.NAVService’ has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.

 

It means you have a typo in your config file. Make sure the following elements are spelled correctly and in correct CASE.

<service
name=”NavWinProxy.NAVService”
behaviorConfiguration=”NAVServiceBehavior”>

<endpoint
address=”"
binding=”basicHttpBinding”
contract=”NavWinProxy.INavWinService” />

 

Next I will start SOAPUI and test the web service. Below is a screenshot of SOAPUI

 

 

Attached is the VS Project. NavProxy

Comments

*This post is locked for comments