编程语言

img lotustao

动态引用WebService,建立WebService虚拟机

发表于2004/10/27 13:55:00  1027人阅读

分类: .Net

动态引用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("<?xml version") == 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);

         }

     }

 

}

 

阅读全文
0 0

相关文章推荐

img
取 消
img