Dynamically referencing WebService, establishing WebService virtual machines

Source: Internet
Author: User
Tags wsdl
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);

}

}



}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.