Diligent achievement genius ...

业精于勤荒于嬉 行成于思毁于随 voiow博客
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

webservice的配置动态调用方法

Posted on 2007-11-02 13:59  voiow  阅读(1161)  评论(0编辑  收藏  举报
在C#程序设计中,经常碰到调用webservice问题,一般都是在程序中进行引用后调用,但是如果webservice地址发生变化,调用webservice的程序就会出现问题,我们可以在webconfig文件预先配置好webservice的引用地址,通过方法实现动态调用,实现方式如下:
首选定义一个动态Web服务代理类:
 

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml;

using System.Web.Services.Description;

using System.CodeDom;

using System.CodeDom.Compiler;

using System.Reflection;

namespace PDBizTalkGetTargetAddress

{

    ///<summary>

    ///动态Web服务代理类

    ///</summary>

    ///<example>

    /// Agent agent = new Agent("http://sample.com/services/SampleService.asmx");

    ///</example>

    public class Agent

    {

        private object agent;

        private Type agentType;

        private const string CODE_NAMESPACE = "Beyondbit.WebServiceAgent.Dynamic";

        ///<summary>

        ///创建新的代理

        ///</summary>

        ///<param name="serviceUri">webserviced uri</param>

        public Agent(string serviceUri)

        {

            XmlTextReader reader = new XmlTextReader(serviceUri + "?wsdl");

            ServiceDescription sd = ServiceDescription.Read(reader);

            ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();

            sdi.AddServiceDescription(sd, null, null);

            CodeNamespace cn = new CodeNamespace(CODE_NAMESPACE);

            CodeCompileUnit ccu = new CodeCompileUnit();

            ccu.Namespaces.Add(cn);

            sdi.Import(cn, ccu);

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

            CompilerParameters cp = new CompilerParameters();

            CompilerResults cr = icc.CompileAssemblyFromDom(cp, ccu);

            agentType = cr.CompiledAssembly.GetTypes()[0];

            agent = Activator.CreateInstance(agentType);

        }

        ///<summary>

        ///调用指定的方法

        ///</summary>

        ///<param name="methodName">方法名,大小写敏感</param>

        ///<param name="args">参数,按照参数顺序赋值</param>

        ///<returns>Web服务的返回值</returns>

        public object Invoke(string methodName, params object[] args)

        {

            MethodInfo mi = agentType.GetMethod(methodName);

            return this.Invoke(mi, args);

        }

        ///<summary>

        ///调用指定方法

        ///</summary>

        ///<param name="method">方法信息</param>

        ///<param name="args">参数,按照参数顺序赋值</param>

        ///<returns>Web服务的返回值</returns>

        public object Invoke(MethodInfo method, params object[] args)

        {

            return method.Invoke(agent, args);

        }

        ///<summary>

        ///调用指定的方法

        ///</summary>

        ///<param name="methodName">方法名,大小写敏感</param>

        ///<param name="arg0">参数</param>

        ///<returns>Web服务的返回值</returns>

        public object Invoke(string methodName, object arg0)

        {

            MethodInfo mi = agentType.GetMethod(methodName);

            return this.Invoke(mi, new object[] { arg0 });

        }

        ///<summary>

        ///调用指定的方法

        ///</summary>

        ///<param name="methodName">方法名,大小写敏感</param>

        ///<param name="args0">参数</param>

        ///<param name="args1">参数</param>

        ///<returns>Web服务的返回值</returns>

        public object Invoke(string methodName, object arg0, object arg1)

        {

            MethodInfo mi = agentType.GetMethod(methodName);

            return this.Invoke(mi, new object[] { arg0, arg1 });

        }

        ///<summary>

        ///调用指定的方法

        ///</summary>

        ///<param name="methodName">方法名,大小写敏感</param>

        ///<param name="args0">参数</param>

        ///<param name="args1">参数</param>

        ///<param name="args2">参数</param>

        ///<returns>Web服务的返回值</returns>

        public object Invoke(string methodName, object arg0, object arg1, object arg2)

        {

            MethodInfo mi = agentType.GetMethod(methodName);

            return this.Invoke(mi, new object[] { arg0, arg1, arg2 });

        }

        ///<summary>

        ///调用指定的方法

        ///</summary>

        ///<param name="method">方法信息</param>

        ///<param name="args0">参数</param>

        ///<returns>Web服务的返回值</returns>

        public object Invoke(MethodInfo method, object arg0)

        {

            return this.Invoke(method, new object[] { arg0 });

        }

        ///<summary>

        ///调用指定的方法

        ///</summary>

        ///<param name="method">方法信息</param>

        ///<param name="arg0">参数</param>

        ///<param name="arg1">参数</param>

        ///<returns>Web服务的返回值</returns>

        public object Invoke(MethodInfo method, object arg0, object arg1)

        {

            return this.Invoke(method, new object[] { arg0, arg1 });

        }

        ///<summary>

        ///调用指定的方法

        ///</summary>

        ///<param name="method">方法信息</param>

        ///<param name="arg0">参数</param>

        ///<param name="arg1">参数</param>

        ///<param name="arg2">参数</param>

        ///<returns>Web服务的返回值</returns>

        public object Invoke(MethodInfo method, object arg0, object arg1, object arg2)

        {

            return this.Invoke(method, new object[] { arg0, arg1, arg2 });

        }

        ///<summary>

        ///获取所有公共方法

        ///</summary>

        public MethodInfo[] Methods

        {

            get

            {

                return agentType.GetMethods();

            }

        }

    }

}


调用方法:
 public void sendStr(string message,string messs)
    {
            string SMWebserviceURL = System.Configuration.ConfigurationManager.AppSettings["URL"].ToString();
            Agent agent = new Agent(SMWebserviceURL);
            object[] Par = new object[8];
            Par[0] = message;
            Par[1] = messs;
            agent.Invoke("SendMessage", Par);
        }
    }