** 实现 ** ** **
上面的设计关键是要有一个虚拟的可以访问任何的 Web SERVICE 的代理,这个代理是基于 WSDL 描述的 。 WebServiceAccessor 这个类能处理这一个任务。我使用一个单独的 Assembly 来实现它,而且这样也是很容易的把它加入 .Net 项目之内。同样,对于 Callback 方法 WebService EventArgs 类也是必须的。
** 虚拟的 ** ** Web Service ** ** 代理 ** ** **
设计虚拟的服务代理的愿望是产生特定的 WEB SERVICE 在内存里 Reflection 进程的元数据。
如一个登陆叁数能被用一个直接的 WEB SERVICEdl 描述或者从它的描述中获得它的间接信息。 对于文件系统和 URL 地址可以被间接的实现。 所有的进程被分为三块如在下列图片中所显示 :
** WEB SERVICE ** 的源程序码仅仅为了测试目的被储存在类中。 这个图像与 wsdl.exe 程序产生的结果是恰好相同的。 我以前的一些文章已经讲述了这些。 有了代理的源程序 , 然后编译它并且产生它的 Assembly 就很容易了。 一旦我们有了代理 assembly 我们就能利用反射方法去初始化一个代理类并且调用它的成员方法。
下列代码片断显示了它的实现 :
_ // Virtual Web Service Accessor _
public class WebServiceAccessor
{
private Assembly _ass = null ;
_ // assembly of the web service proxy _
private string _protocolName = "Soap" ;
_ // communication protocol _
private string _srcWSProxy = string .Empty;
_ // source text (.cs) _
_ // _
public Assembly Assembly { get { return _ass; } }
public string ProtocolName
{ get { return _protocolName; } set {_protocolName = value; } }
public string SrcWSProxy { get { return _srcWSProxy; } }
public WebServiceAccessor()
{
}
public WebServiceAccessor( string wsdlSourceName)
{
AssemblyFromWsdl(GetWsdl(wsdlSourceName));
}
_ // Get the wsdl text from specified source _
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; _ // this can be a wsdl string _
}
else
if (source.StartsWith( "http://" ) == true )
{
return WsdlFromUrl(source); _ // this is a url address _
}