Dynamically referencing WebService, establishing WebService virtual machines
Last Update:2017-02-28
Source: Internet
Author: User
Web| dynamically establishes WebService virtual proxies and uses XML to register WebService reference addresses. Implement dynamic reference WebService.
Use of technology
1, dynamic compilation
2, Reflective technology
Implementation code
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);
}
}
}