Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have found such piece of code at "<a href="http://blogs.msdn.com/kaevans/archive/2006/04/27/585013.aspx" rel="nofollow noreferrer">Dynamically Invoking a Web Service</a>" on <a href="http://blogs.msdn.com/kaevans/default.aspx" rel="nofollow noreferrer">Kirk Evans Blog</a>. Hope will help someone...</p> <hr> <p>(the original code needed some work. This should be equivalent)</p> <pre><code>using System; using System.CodeDom; using System.CodeDom.Compiler; using System.Diagnostics; using System.Net; using System.Security.Permissions; using System.Web.Services.Description; using System.Xml.Serialization; namespace ConnectionLib { internal class WsProxy { [SecurityPermission(SecurityAction.Demand, Unrestricted = true)] internal static object CallWebService( string webServiceAsmxUrl, string serviceName, string methodName, object[] args) { var description = ReadServiceDescription(webServiceAsmxUrl); var compileUnit = CreateProxyCodeDom(description); if (compileUnit == null) { return null; } var results = CompileProxyCode(compileUnit); // Finally, Invoke the web service method var wsvcClass = results.CompiledAssembly.CreateInstance(serviceName); var mi = wsvcClass.GetType().GetMethod(methodName); return mi.Invoke(wsvcClass, args); } private static ServiceDescription ReadServiceDescription(string webServiceAsmxUrl) { using (var client = new WebClient()) { using (var stream = client.OpenRead(webServiceAsmxUrl + "?wsdl")) { return ServiceDescription.Read(stream); } } } private static CodeCompileUnit CreateProxyCodeDom(ServiceDescription description) { var importer = new ServiceDescriptionImporter { ProtocolName = "Soap12", Style = ServiceDescriptionImportStyle.Client, CodeGenerationOptions = CodeGenerationOptions.GenerateProperties }; importer.AddServiceDescription(description, null, null); // Initialize a Code-DOM tree into which we will import the service. var nmspace = new CodeNamespace(); var compileUnit = new CodeCompileUnit(); compileUnit.Namespaces.Add(nmspace); // Import the service into the Code-DOM tree. This creates proxy code // that uses the service. var warning = importer.Import(nmspace, compileUnit); return warning != 0 ? null : compileUnit; } private static CompilerResults CompileProxyCode(CodeCompileUnit compileUnit) { CompilerResults results; using (var provider = CodeDomProvider.CreateProvider("CSharp")) { var assemblyReferences = new[] { "System.dll", "System.Web.Services.dll", "System.Web.dll", "System.Xml.dll", "System.Data.dll" }; var parms = new CompilerParameters(assemblyReferences); results = provider.CompileAssemblyFromDom(parms, compileUnit); } // Check For Errors if (results.Errors.Count == 0) { return results; } foreach (CompilerError oops in results.Errors) { Debug.WriteLine("========Compiler error============"); Debug.WriteLine(oops.ErrorText); } throw new Exception( "Compile Error Occurred calling webservice. Check Debug output window."); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload