科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道动态引用 建立WebService虚拟机

动态引用 建立WebService虚拟机

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

   概述 建立WebService虚拟代理,使用xml登记WebService的引用地址。

作者:中国IT实验室 来源:中国IT实验室 2007年9月28日

关键字: WebService 动态 编程

  • 评论
  • 分享微博
  • 分享邮件
  

概述

      建立WebService虚拟代理,使用xml登记WebService的引用地址。实现动态引用WebService.

      使用技术

1、          动态编译

2、          反射技术

实现代码

using System;

using System.Reflection ;

using System.Web .Services .Description ;

using Microsoft.CSharp;

using System.CodeDom ;

using System.CodeDom.Compiler ;

using  System.IO ;

using  System.Text ;

using System.Xml ;

using System.Net ;

using WebServiceProxy;

namespace WebServiceProxy

{

        public class WebServiceProxy

     {

         private Assembly _ass = null;              

 

         private string _protocolName = "Soap";     

         private string _srcWSProxy = string.Empty; 

 

         public Assembly Assembly { get{ return _ass; } }

 

         public string ProtocolName

 

         { get{ return _protocolName; } set {_protocolName = value; } }

 

         public string SrcWSProxy { get{ return _srcWSProxy; } }

 

 

 

         public WebServiceProxy ()

 

         {

 

         }

 

         public WebServiceProxy (string wsdlSourceName)

 

         {

 

              AssemblyFromWsdl(GetWsdl(wsdlSourceName));

 

         }

 

         public string WsdlFromUrl(string url)

 

         {

 

              WebRequest req = WebRequest.Create(url);

 

              WebResponse result = req.GetResponse();

 

              Stream ReceiveStream = result.GetResponseStream();

 

              Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

 

              StreamReader sr = new StreamReader( ReceiveStream, encode );

 

              string strWsdl = sr.ReadToEnd();

 

              return strWsdl;

 

         }

 

         public string GetWsdl(string source)

         {

 

              if(source.StartsWith("true)

 

              {

                   return source;             

              }

              else

 

              if(source.StartsWith("http://") == true)

              {

                   return WsdlFromUrl(source);  

              }

 

              return WsdlFromFile(source);  

         }

         public string WsdlFromFile(string fileFullPathName)

         {

              FileInfo fi = new FileInfo(fileFullPathName);

              if(fi.Extension == "wsdl")

              {

                   FileStream fs = new FileStream(fileFullPathName, FileMode.Open,

                       FileAccess.Read);

                   StreamReader sr = new StreamReader(fs);

                   char[] buffer = new char[(int)fs.Length];

                   sr.ReadBlock(buffer, 0, (int)fs.Length);

                   return new string(buffer);

              }

               

              throw new Exception("This is no a wsdl file");

         }

 

         public Assembly AssemblyFromWsdl(string strWsdl)

         {

              // Xml text reader

              StringReader  wsdlStringReader = new StringReader(strWsdl);

              XmlTextReader tr = new XmlTextReader(wsdlStringReader);

              ServiceDescription sd = ServiceDescription.Read(tr);

              tr.Close();

 

              // WSDL service description importer

              CodeNamespace cns = new CodeNamespace("WebServiceProxy.WebServiceAccessor");

              ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();

              sdi.AddServiceDescription(sd, null, null);

              sdi.ProtocolName = _protocolName;

              sdi.Import(cns, null);

 

              // source code generation

              CSharpCodeProvider cscp = new CSharpCodeProvider();

              ICodeGenerator icg = cscp.CreateGenerator();

              StringBuilder srcStringBuilder = new StringBuilder();

              StringWriter sw = new StringWriter(srcStringBuilder);

              icg.GenerateCodeFromNamespace(cns, sw, null);

              _srcWSProxy = srcStringBuilder.ToString();

              sw.Close();

 

              // assembly compilation.

              CompilerParameters cp = new CompilerParameters();

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

              cp.ReferencedAssemblies.Add("System.Xml.dll");

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

              cp.GenerateExecutable = false;

              cp.GenerateInMemory = true;

              cp.IncludeDebugInformation = false;

              ICodeCompiler icc = cscp.CreateCompiler();

              CompilerResults cr = icc.CompileAssemblyFromSource(cp, _srcWSProxy);

              if(cr.Errors.Count > 0)

                   throw new Exception(string.Format("Build failed: {0} errors",

                       cr.Errors.Count));

 

              return _ass = cr.CompiledAssembly;

         }

 

 

         public object CreateInstance(string objTypeName)

         {

              Type t = _ass.GetType("WebServiceProxy.WebServiceAccessor" + "." + objTypeName);

              return Activator.CreateInstance(t);

         }

 

 

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

         {

              MethodInfo mi = obj.GetType().GetMethod(methodName);

              return mi.Invoke(obj, args);

         }

     }

 

}

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章