[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Net;
Using System. IO;
Using System. CodeDom. Compiler;
Using System. Reflection;
Using System. Web. Services. Description;
Using System. Xml. Serialization;
Using System. CodeDom;
Namespace CN100.Member. Utility
{
Public class WebServiceHelper
{
Private static WebServiceHelper webService = null;
Public static WebServiceHelper Instance (string webServiceUrl, string NamSpace)
{
If (webService = null)
{
WebService = new WebServiceHelper (webServiceUrl, NamSpace );
}
Return webService;
}
Private WebServiceHelper ()
{
}
/// <Summary>
/// WebService address
/// </Summary>
Public string ServerUrl
{
Get;
Set;
}
/// <Summary>
/// Call the class namespace
/// </Summary>
Public string NameSpace
{
Get;
Set;
}
Private WebServiceHelper (string webServiceUrl, string namSpace)
{
ServerUrl = webServiceUrl;
NameSpace = namSpace;
}
/// <Summary>
/// Generate dynamic reference DLL
/// </Summary>
/// <Returns> </returns>
Public bool GenerateWebService ()
{
WebClient client = new WebClient ();
String url = ServerUrl + "? WSDL "; // This address can be written in the Config file. Just extract it here. Add the following to the original address :? WSDL
Stream stream = client. OpenRead (url );
ServiceDescription description = ServiceDescription. Read (stream );
ServiceDescriptionImporter importer = new ServiceDescriptionImporter (); // create a client proxy class.
Importer. ProtocolName = "Soap"; // specify the access protocol.
Importer. Style = ServiceDescriptionImportStyle. Client; // generate a Client proxy.
Importer. CodeGenerationOptions = CodeGenerationOptions. GenerateProperties | CodeGenerationOptions. GenerateNewAsync;
Importer. AddServiceDescription (description, null, null); // Add a WSDL document.
CodeNamespace nmspace = new CodeNamespace (); // namespace
Nmspace. Name = NameSpace;
CodeCompileUnit unit = new CodeCompileUnit ();
Unit. Namespaces. Add (nmspace );
ServiceDescriptionImportWarnings warning = importer. Import (nmspace, unit );
CodeDomProvider provider = CodeDomProvider. CreateProvider ("CSharp ");
CompilerParameters parameter = new CompilerParameters ();
Parameter. GenerateExecutable = false;
Parameter. OutputAssembly = NameSpace + ". dll"; // name of the output assembly
Parameter. ReferencedAssemblies. Add ("System. dll ");
Parameter. ReferencedAssemblies. Add ("System. XML. dll ");
Parameter. ReferencedAssemblies. Add ("System. Web. Services. dll ");
Parameter. ReferencedAssemblies. Add ("System. Data. dll ");
CompilerResults result = provider. CompileAssemblyFromDom (parameter, unit );
If (result. Errors. HasErrors)
{
// Display the compilation error message
Return false;
}
Return true;
}
Private Assembly LoadAssembly (string nameSpace)
{
Assembly asm = null;
Try
{
Asm = Assembly. LoadFrom (nameSpace + ". dll"); // load the previously generated Assembly
}
Catch (FileNotFoundException ex)
{
If (GenerateWebService ())
{
Asm = Assembly. LoadFrom (nameSpace + ". dll"); // load the previously generated Assembly
}
}
Catch (Exception e)
{
Throw e;
}
Return asm;
}
/// <Summary>
/// Execute the method without return value
/// </Summary>
/// <Param name = "methodName"> </param>
/// <Param name = "nameSpace"> </param>
/// <Param name = "args"> </param>
Public void ExcuteMethod (string methodName, string nameSpace, params object [] args)
{
Assembly asm = LoadAssembly (nameSpace );
Type t = asm. GetType (nameSpace );
Object o = Activator. CreateInstance (t );
MethodInfo method = t. GetMethod (methodName );
Method. Invoke (o, args );
}
Public void ExcuteMethod (string methodName, params object [] args)
{
String nameSpace = NameSpace;
ExcuteMethod (methodName, nameSpace, args );
}
/// <Summary>
/// Execute the method with return value
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "methodName"> </param>
/// <Param name = "nameSpace"> </param>
/// <Param name = "args"> </param>
/// <Returns> </returns>
Public T ExcuteMethod <T> (string methodName, string nameSpace, params object [] args)
{
Assembly asm = LoadAssembly (nameSpace );
Type t = asm. GetType (nameSpace );
Object o = Activator. CreateInstance (t );
MethodInfo method = t. GetMethod (methodName );
T result = (T) method. Invoke (o, args );
Return result;
}
Public T ExcuteMethod <T> (string methodName, params object [] args)
{
String nameSpace = NameSpace;
Return ExcuteMethod <T> (methodName, nameSpace, args );
}
}
}