.net调用webservice,webservice地址可随便修改而不要重新编译

建一个winform项目

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.IO;
using System.Reflection;
using System.CodeDom.Compiler;
using System.CodeDom;
using System.Web.Services;
using System.Web.Services.Description;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
using System.Configuration;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        CompilerResults result = null;
        public Form1()
        {
            InitializeComponent();

          
         

        }

      

 

          public void GetService()
     {
         //try
         //{
         //    ServiceReference1.HandleDataSoapClient hr = new ServiceReference1.HandleDataSoapClient();
         //    string ttt= hr.HandlePSAM(textBox1.Text);
         //     MessageBox.Show(ttt);
         //}
         //catch (Exception ex)
         //{
         //    MessageBox.Show(ex.Message);
         //}
     }

     private void button4_Click(object sender, EventArgs e)
     {
         try {
             // 5. 使用 Reflection 调用 WebService。
             if (!result.Errors.HasErrors)
             {
                 Assembly asm = result.CompiledAssembly;
                 Type t = asm.GetType(ConfigurationManager.AppSettings["namespace"].ToString() + ".HandleData", true, true); // 如果在前面为代理类添加了命名空间,此处需要将命名空间添加到类型前面。

                 object o = Activator.CreateInstance(t);
                 MethodInfo method = t.GetMethod("HandleBusinessSingle");
                
                 MessageBox.Show(method.Invoke(o, new object[] { textBox1.Text }).ToString());
             }
            // ServiceReference1.HandleDataSoapClient hr = new ServiceReference1.HandleDataSoapClient();
           //  string ttt = hr.HandleBusinessSingle(textBox1.Text);
           //  MessageBox.Show(ttt);
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
       
     }

     private void button5_Click(object sender, EventArgs e)
     {
         try{
             if (!result.Errors.HasErrors)
             {
                 Assembly asm = result.CompiledAssembly;
                 Type t = asm.GetType(ConfigurationManager.AppSettings["namespace"].ToString() + ".HandleData",true,true); // 如果在前面为代理类添加了命名空间,此处需要将命名空间添加到类型前面。命名空间注意写法,如:http://www.aaa.com/这是可以的

                 object o = Activator.CreateInstance(t);
                 MethodInfo method = t.GetMethod("HandlePSAMSingle");//具体的方法名称

                 MessageBox.Show(method.Invoke(o, new object[] { textBox1.Text }).ToString());//方法参数
             }

         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
     }

     private void Form1_Load(object sender, EventArgs e)
     {
         try
         {
             // 1. 使用 WebClient 下载 WSDL 信息。
             WebClient web = new WebClient();

             Stream stream = web.OpenRead(ConfigurationManager.AppSettings["url"].ToString());//webservice地址

             // 2. 创建和格式化 WSDL 文档。
             ServiceDescription description = ServiceDescription.Read(stream);

             // 3. 创建客户端代理代理类。
             ServiceDescriptionImporter importer = new ServiceDescriptionImporter();//此方法在.net2.0下面才能找到

             importer.ProtocolName = "Soap"; // 指定访问协议。
             importer.Style = ServiceDescriptionImportStyle.Client; // 生成客户端代理。
             importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;

             importer.AddServiceDescription(description, null, null); // 添加 WSDL 文档。

             // 4. 使用 CodeDom 编译客户端代理类。
             CodeNamespace nmspace = new CodeNamespace(); // 为代理类添加命名空间,缺省为全局空间。
             nmspace.Name = ConfigurationManager.AppSettings["namespace"].ToString();//从配置文件里面读取命名空间
             CodeCompileUnit unit = new CodeCompileUnit();
             unit.Namespaces.Add(nmspace);

             ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);
             CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");

             CompilerParameters parameter = new CompilerParameters();
             parameter.GenerateExecutable = false;
             parameter.GenerateInMemory = true;
             parameter.ReferencedAssemblies.Add("System.dll");
             parameter.ReferencedAssemblies.Add("System.XML.dll");
             parameter.ReferencedAssemblies.Add("System.Web.Services.dll");
             parameter.ReferencedAssemblies.Add("System.Data.dll");

             result = provider.CompileAssemblyFromDom(parameter, unit);
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
       

     }


 
    }
}

 

 

这几点填好就可以了。这样只要在配置文件里面直接修改webservice地址就可以了。而不需要重新编译了。

posted @ 2010-11-08 18:50  隐者  阅读(1056)  评论(0)    收藏  举报