C # dynamically call the webservice Method

Source: Internet
Author: User

[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 );
}
 
}
}

Related Article

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.