动态引用 建立WebService虚拟机

ZDNet软件频道 时间:2008-06-29 作者: | 中国IT实验室 我要评论()
本文关键词:虚拟机 WebService xml 软件
建立WebService虚拟代理,使用xml登记WebService的引用地址。实现动态引用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: 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);

         }

     }

 

}

虚拟机

WebService

xml

软件


百度大联盟认证黄金会员Copyright© 1997- CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号 京ICP备09041801号-159
京公网安备:1101082134