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;
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();
System.IO.Stream stream = client.OpenRead(webServiceAsmxUrl + "?wsdl");
ServiceDescription description = ServiceDescription.Read(stream);
ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
importer.ProtocolName = "Soap12";
importer.AddServiceDescription(description, null, null);
importer.Style = ServiceDescriptionImportStyle.Client;
importer.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties;
CodeNamespace codenamespace = new CodeNamespace();
CodeCompileUnit codeunit = new CodeCompileUnit();
codeunit.Namespaces.Add(codenamespace);
ServiceDescriptionImportWarnings warning = importer.Import(codenamespace, codeunit);
if (warning == 0)
{
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
string[] assemblyReferences = new string[] {
"System.dll",
"System.Web.Services.dll",
"System.Web.dll",
"System.Xml.dll",
"System.Data.dll"};
CompilerParameters parms = new CompilerParameters(assemblyReferences);
parms.GenerateInMemory = true;
CompilerResults results = provider.CompileAssemblyFromDom(parms, codeunit);
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.");
}
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