エンコードによる動的呼び出しWebServiceの実現方法


    ,      ref   
        ///   <summary>   

        ///          ,    WebService     

        ///   </summary>   

        ///   <param   name="url">WebService http     </param>   

        ///   <param   name="namespace">    WebService     </param>   

        ///   <param   name="classname">    WebService   (         )</param>   

        ///   <param   name="methodname">    WebService    </param>   

        ///   <param   name="args">    </param>   

        ///   <returns>WebService     </returns>   

        ///   <remarks>   

        ///         ,    Exception。      ,      。   

        ///                 :   

        ///   1、    WebService   ,CompileAssembly  。   

        ///   2、WebService      。   

        ///   </remarks>   

        ///   <example>   

        ///   <code>   

        ///   

        ///       

        ///   object [] mArgs = new object[2];

        ///   mArgs[0] = "1321321321312";

        ///   mArgs[1] = null;

        ///   object mReturn = InvokeWebservice("http://localhost:3841/Service1.asmx", "WebService1", "Service1", "HelloWorldRef", mArgs);

        ///   </code>   

        ///   </example>   

        public static object InvokeWebservice(string url, string @namespace, string classname, string methodname, object[] args)

        {

            try

            {

                System.Net.WebClient wc = new System.Net.WebClient();

                System.IO.Stream stream = wc.OpenRead(url + "?WSDL");

                System.Web.Services.Description.ServiceDescription sd = System.Web.Services.Description.ServiceDescription.Read(stream);

                System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter();

                sdi.AddServiceDescription(sd, "", "");

                System.CodeDom.CodeNamespace cn = new System.CodeDom.CodeNamespace(@namespace);

                System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit();

                ccu.Namespaces.Add(cn);

                sdi.Import(cn, ccu);



                Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider();

                System.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompiler();



                System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.CompilerParameters();

                cplist.GenerateExecutable = false;

                cplist.GenerateInMemory = true;

                cplist.ReferencedAssemblies.Add("System.dll");

                cplist.ReferencedAssemblies.Add("System.XML.dll");

                cplist.ReferencedAssemblies.Add("System.Web.Services.dll");

                cplist.ReferencedAssemblies.Add("System.Data.dll");



                System.CodeDom.Compiler.CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);

                if (true == cr.Errors.HasErrors)

                {

                    System.Text.StringBuilder sb = new System.Text.StringBuilder();

                    foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)

                    {

                        sb.Append(ce.ToString());

                        sb.Append(System.Environment.NewLine);

                    }

                    throw new Exception(sb.ToString());

                }

                System.Reflection.Assembly assembly = cr.CompiledAssembly;

                Type t = assembly.GetType(@namespace + "." + classname, true, true);

                object obj = Activator.CreateInstance(t);

                System.Reflection.MethodInfo mi = t.GetMethod(methodname);

                return mi.Invoke(obj, args);

            }

            catch (NullReferenceException NullEx)

            {

                throw NullEx;

            }

            catch (Exception Ex)

            {

                throw Ex;

            }

        }   

呼び出し方法
using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Xml.Linq;



namespace WebService1

{

    /// <summary>

    /// Service1      

    /// </summary>

    [WebService(Namespace = "http://tempuri.org/")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    [ToolboxItem(false)]

    //        ASP.NET AJAX         Web   ,         。

    // [System.Web.Script.Services.ScriptService]

    public class Service1 : System.Web.Services.WebService

    {



        [WebMethod]

        public string HelloWorld(string pPara)

        {

            return "Hello World " + pPara;

        }



        [WebMethod]

        public string HelloWorldRef(string pPara, ref string pReturn)

        {

            pReturn = "Hello World " + pPara;



            return pReturn;

        }

    }

}