c# - creating proxy using wsdl programmatically and wsdl parsing -
i working on xml web services. client web service "client" has url of wsdl of server web service "service" @ run time. in order "client" use "service" need following thing "programmatically":
1) wsdl file on fly "service" or location on disk. 2) create proxy programmatically i.e not using wsdl.exe or add web reference. 3)invoke methods on created proxy.
is possible it? if 1 has done greatful take suggestions how accomplish.
using system; using system.collections.generic; using system.text; using system.reflection; using system.codedom; using system.codedom.compiler; using system.security.permissions; using system.web.services.description; namespace connectionlib { public class wsproxy { [securitypermissionattribute(securityaction.demand, unrestricted = true)] public static object callwebservice(string webserviceasmxurl, string servicename, string methodname, object[] args) { system.net.webclient client = new system.net.webclient(); // connect web service system.io.stream stream = client.openread(webserviceasmxurl + "?wsdl"); // read wsdl file describing service. servicedescription description = servicedescription.read(stream); ///// load dom ///////// // initialize service description importer. servicedescriptionimporter importer = new servicedescriptionimporter(); importer.protocolname = "soap12"; // use soap 1.2. importer.addservicedescription(description, null, null); // generate proxy client. importer.style = servicedescriptionimportstyle.client; // generate properties represent primitive values. importer.codegenerationoptions = system.xml.serialization.codegenerationoptions.generateproperties; // initialize code-dom tree import service. codenamespace nmspace = new codenamespace(); codecompileunit unit1 = new codecompileunit(); unit1.namespaces.add(nmspace); // import service code-dom tree. creates proxy code uses service. servicedescriptionimportwarnings warning = importer.import(nmspace, unit1); if (warning == 0) // if 0 go { // generate proxy code codedomprovider provider1 = codedomprovider.createprovider("csharp"); // compile assembly proxy 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 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 web service method object wsvcclass = results.compiledassembly.createinstance(servicename); methodinfo mi = wsvcclass.gettype().getmethod(methodname); return mi.invoke(wsvcclass, args); } else { return null; } } } }
Comments
Post a Comment