Dynamic webservice Compilation Method

Source: Internet
Author: User
Tags wsdl

Using System. CodeDom. Compiler;
Using System;
Using System. Net;
Using System. CodeDom;
Using Microsoft. CSharp;
Using System. IO;
Using System. Web. Services. Description;
Using System. Collections. Generic;
Using System. Reflection;

Namespace VacationCategory
{
Public class DynamicWebServices
{
Static SortedList <string, Type> _ typeList = new SortedList <string, Type> ();

# Region InvokeWebService

Static string GetCacheKey (string url, string className)
{
Return url. ToLower () + className;
}
Public static Type GetTypeFromCache (string url, string className)
{
String key = GetCacheKey (url, className );
Foreach (KeyValuePair <string, Type> pair in _ typeList)
{
If (key = pair. Key)
{
Return pair. Value;
}
}

Return null;
}
Public static Type GetTypeFromWebService (string url, string className)
{
String @ namespace = "EnterpriseServerBase. WebService. DynamicWebCalling ";
If (className = null) | (className = ""))
{
ClassName = GetWsClassName (url );
}

// Obtain the WSDL
WebClient wc = new WebClient ();
Stream stream = wc. OpenRead (url + "? WSDL ");
ServiceDescription sd = ServiceDescription. Read (stream );
ServiceDescriptionImporter sdi = new ServiceDescriptionImporter ();
Sdi. AddServiceDescription (sd ,"","");
CodeNamespace cn = new CodeNamespace (@ namespace );

 

// Generate client proxy code
CodeCompileUnit ccu = new CodeCompileUnit ();
Ccu. Namespaces. Add (cn );
Sdi. Import (cn, ccu );
CSharpCodeProvider csc = new CSharpCodeProvider ();
// ICodeCompiler icc = csc. CreateCompiler ();

// Set compilation Parameters
CompilerParameters cplist = new CompilerParameters ();
Cplist. GenerateExecutable = false;
Cplist. GenerateInMemory = true;
Cplist. ReferencedAssemblies. Add ("System. dll ");
Cplist. ReferencedAssemblies. Add ("System. XML. dll ");
Cplist. ReferencedAssemblies. Add ("System. Web. Services. dll ");
Cplist. ReferencedAssemblies. Add ("System. Data. dll ");

// Compile the proxy class
CompilerResults cr = csc. CompileAssemblyFromDom (cplist, ccu );
If (true = cr. Errors. HasErrors)
{
System. Text. StringBuilder sb = new System. Text. StringBuilder ();
Foreach (System. CodeDom. Compiler. CompilerError ce in cr. Errors)
{
Sb. Append (ce. ToString ());
Sb. Append (System. Environment. NewLine );
}
Throw new Exception (sb. ToString ());
}

// Generate a proxy instance and call the Method
System. Reflection. Assembly assembly = cr. CompiledAssembly;
Type t = assembly. GetType (@ namespace + "." + className, true, true );

Return t;
}

// Dynamically call the web Service
Public static object InvokeWebService (string url, string methodName, object [] args)
{
Return InvokeWebService (url, null, methodName, args );
}

Public static object InvokeWebService (string url, string className, string methodName, object [] args)
{
Try
{
Type t = GetTypeFromCache (url, className );
If (t = null)
{
T = GetTypeFromWebService (url, className );

// Add to the buffer
String key = GetCacheKey (url, className );
_ TypeList. Add (key, t );
}

Object obj = Activator. CreateInstance (t );
MethodInfo mi = t. GetMethod (methodName );

Return mi. Invoke (obj, args );
}
Catch (Exception ex)
{
Throw new Exception (ex. InnerException. Message, new Exception (ex. InnerException. StackTrace ));
}
}

Private static string GetWsClassName (string wsUrl)
{
String [] parts = wsUrl. Split ('/');
String [] pps = parts [parts. Length-1]. Split ('.');

Return pps [0];
}
# Endregion

}
}
Possible problems:
Warning 1 'System. codeDom. compiler. codeDomProvider. createCompiler () 'is obsolete: 'callers shocould not use the ICodeCompiler interface and shocould instead use the methods directly on the CodeDomProvider class. those inheriting from CodeDomProvider must still implement this interface, and shoshould exclude this warning or also obsolete this method. 'd: \ Lion \ Study \ CompanyCalendar \ VacationCategory \ DynamicWebServices. cs 62 33 VacationCategory

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.