Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have wsdl url(https://sample/url/serviceName?wsdl), I have service name and service method name and I have to pass two parameters while invoking that method.
I can't consume web service by adding web reference to project. So I have to do it in c# code behind of a windows application. Please see the below code which I have.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
//Added for samplecode
using System.CodeDom.Compiler;
using System.Security.Permissions;
using System.Web.Services.Description;
using System.Reflection;
using System.CodeDom;
using System.Diagnostics;
 
namespace DynamicSoap
{
 
    public static class DynamicWebService
    {
 
        public static Object CallWebService(string webServiceAsmxUrl,
           string serviceName, string methodName, object[] args)
        {
 
            try
            {
                System.Net.WebClient client = new System.Net.WebClient();
 
                //-Connect To the web service
                System.IO.Stream stream = client.OpenRead(webServiceAsmxUrl + "?wsdl");
 
                //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 codenamespace = new CodeNamespace();
                CodeCompileUnit codeunit = new CodeCompileUnit();
                codeunit.Namespaces.Add(codenamespace);
 
                //Import the service into the Code-DOM tree. 
                //This creates proxy code that uses the service.

                ServiceDescriptionImportWarnings warning = importer.Import(codenamespace, codeunit);
 
                if (warning == 0)
                {
 
                    //--Generate the proxy code
                    CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
 
                    //--Compile the assembly proxy with the 
                    //  appropriate references
                    string[] assemblyReferences = new string[]  {
                       "System.dll", 
                       "System.Web.Services.dll", 
                       "System.Web.dll", 
                       "System.Xml.dll", 
                       "System.Data.dll"};
 
                    //--Add parameters
                    CompilerParameters parms = new CompilerParameters(assemblyReferences);
                    parms.GenerateInMemory = true; //(Thanks for this line nikolas)
                    CompilerResults results = provider.CompileAssemblyFromDom(parms, codeunit);
                    
                    //--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 Exception("Compile Error Occured calling WebService.");
                    }
 
                    //--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;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

In the above code while importing
ServiceDescriptionImportWarnings warning = importer.Import(codenamespace, codeunit);
warning should come 0 then only it would go inside the if statement. But I'm getting 'CodeNotGenerated'

I'm not able to figure out what the problem is. Can anyone pls explain??















web
Posted
Updated 11-May-15 19:42pm
v2
Comments
Priyatam Upadrasta 12-May-15 2:34am    
Can someone suggest a better way to do it.
Sinisa Hajnal 12-May-15 2:43am    
Yes, add web reference :) Why can't you?
Priyatam Upadrasta 12-May-15 3:34am    
No I cant add web Reference as the URl is not accessible internally.
Sinisa Hajnal 12-May-15 3:53am    
Create your own web service with same signature that is accessible internally. Then when you get access to the original service, all you have to do is change the target service.

Or ask your admin for the tunnel so you GET the access.
samec 15-Feb-21 12:11pm    
ii have some problem with code ServiceDescription.Read(stream) not load or assembly for read property.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900