動態引用WebService,建立WebService虛擬機器

來源:互聯網
上載者:User
web|動態 建立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);

}

}



}



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。