1

I am working on a project that requires me to connect to a Microsoft Dynamics NAV web service, and get some data. However, we do not have access to this web service from the development environment, so I am unable to add a Web Reference to my project, in the usual way.

Is there a way, to "dynamically" connect to the web reference, in run-time, by providing URL, user, password & domain? Then create dynamic objects, and call the methods of the web service? (So I would make a console application, that only runs on the production environment, because there's the access to the web service)

From my initial research I found a couple of ways, where there's a function, that returns object type objects. I usually have credential problems with this : Unauthorized access (401)

I am very new to these web services, by the way...

Method i've been using so far, without any luck:

internal static object CallWebService(string webServiceAsmxUrl, string serviceName, string methodName, object[] args)

    {
        System.Net.WebClient client = new System.Net.WebClient();
        CredentialCache cc = new CredentialCache();
        cc.Add(
            new Uri(webServiceAsmxUrl),
            "NTLM",
            new NetworkCredential("user", "password", "domain"));
        client.Credentials = cc;
        // Connect To the web service
        System.IO.Stream stream = client.OpenRead(webServiceAsmxUrl + "?wsdl");

        // Now read the WSDL file describing a service.
        ServiceDescription description = ServiceDescription.Read(stream);
        ///// LOAD THE DOM /////////

        // Initialize a service description importer.

        ServiceDescriptionImporter importer = new ServiceDescriptionImporter();

        importer.ProtocolName = "Soap12"; // Use SOAP 1.2.

        importer.AddServiceDescription(description, null, null);

        // Generate a proxy client.

        importer.Style = ServiceDescriptionImportStyle.Client;

        // Generate properties to represent primitive values.

        importer.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties;

        // Initialize a Code-DOM tree into which we will import the service.

        CodeNamespace nmspace = new CodeNamespace();

        CodeCompileUnit unit1 = new CodeCompileUnit();

        unit1.Namespaces.Add(nmspace);

        // Import the service into the Code-DOM tree. This creates proxy code that uses the service.

        ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit1);

        //THIS IS WHERE IT's NOT COMPLYING. warning will be "NoGeneratedCode"
        if (warning == 0) // If zero then we are good to go

        {

            // Generate the proxy code

            CodeDomProvider provider1 = CodeDomProvider.CreateProvider("CSharp");

            // Compile the assembly proxy with the appropriate references

            string[] assemblyReferences = new string[5] { "System.dll", "System.Web.Services.dll", "System.Web.dll", "System.Xml.dll", "System.Data.dll" };

            CompilerParameters parms = new CompilerParameters(assemblyReferences);

            CompilerResults results = provider1.CompileAssemblyFromDom(parms, unit1);

            // Check For Errors
            if (results.Errors.Count > 0)

            {

                foreach (CompilerError oops in results.Errors)

                {

                    System.Diagnostics.Debug.WriteLine("========Compiler error============");

                    System.Diagnostics.Debug.WriteLine(oops.ErrorText);

                }

                throw new System.Exception("Compile Error Occured calling webservice. Check Debug ouput window.");

            }

            // Finally, Invoke the web service method

            object wsvcClass = results.CompiledAssembly.CreateInstance(serviceName);

            MethodInfo mi = wsvcClass.GetType().GetMethod(methodName);

            return mi.Invoke(wsvcClass, args);

        }

        else

        {

            return null;

        }

    }

It always returns null.

Henk van Boeijen
  • 7,357
  • 6
  • 32
  • 42
Laureant
  • 979
  • 3
  • 18
  • 47

2 Answers2

0

Is it web service (or) a WCF service? Cause if it a WCF service then you can use SvcUtil.exe to access the service. If it's a webservice then you can access the service from any web browser provided you know the WSDL file name like below

http://servername/apppath/webservicename.asmx

(OR)

 http://servername/vdir/webservicename.asmx/Methodname?parameter=value

See this Documentation for more information

Rahul
  • 76,197
  • 13
  • 71
  • 125
  • It's a web service, because I can access the WSDL file from Internet Explorer, or from the Web Service Studio. – Laureant Dec 18 '15 at 13:39
  • Check this post https://social.msdn.microsoft.com/Forums/vstudio/en-US/614b4651-f061-4e68-8779-6389014852a6/create-web-service-proxy-dynamically. looks like you are in the right direction. – Rahul Dec 18 '15 at 13:43
  • Thanks for the reply, but it's not helping sadly. The code I added to the question should do the trick, but the "warning" variable has an invalid value. – Laureant Dec 18 '15 at 14:10
0

I know it's an old post but I just stumbled across the exact same issue. I fixed it by changing

importer.ProtocolName = "Soap12"; // Use SOAP 1.2.

to

importer.ProtocolName = "Soap"; // Woohoo

The allowed values for the ProtocolName are:

  • "Soap"
  • "Soap12"
  • "HttpPost"
  • "HttpGet"
  • "HttpSoap"

as per the documentation.

Hope that helps someone who comes across a similar issue!

Myshkin
  • 96
  • 5