扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:中国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领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者