Dynamically call WebService and cache the object locally ....

Source: Internet
Author: User

/// The first version completes dynamic loading in the memory
/// Perform the following steps to dynamically call WebService:
/// 1. Download the WSDL data from the target URL.
/// 2. Use servicedescription to create and format the WSDL document file.
/// 3. Use servicedescriptionimporter to create the client proxy class.
/// 4. Use codedom to dynamically create a client proxy assembly.
/// 5. Call related WebService Methods Using Reflection

 

Public static methodinfo getpoliceserviceproxy (string functionname, out object OBJ)
{
OBJ = NULL;
Try
{
Try
{
// Cache mechanism cache proxy objects
WebServicesObjectCacheManager webServicesObjectCacheManager = new WebServicesObjectCacheManager ();
Object serviceType = webServicesObjectCacheManager. getWebServicesObject ("com. HS. services ");

If (serviceType! = Null)
{
Type t = serviceType as Type;
Obj = Activator. CreateInstance (t );

MethodInfo method = t. GetMethod (functionName );

If (method! = Null)
{
Return method;
}
Else
{
Return null;
}
}
}
Catch (Exception)
{
}

// 1. Use WebClient to download the WSDL information.
WebClient web = new WebClient ();

// Obtain the WSDL address of the model service.
String wsdlAddress = WebservicesWSDLXML. getWSDLAddress (2 );
If (wsdlAddress = null)
{
Return null;
}

Stream stream = web. openread (wsdladdress );

// 2. Create and format the WSDL document.
Servicedescription description = servicedescription. Read (Stream );

// 3. Create a client proxy class.
Servicedescriptionimporter importer = new servicedescriptionimporter ();

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.

// 4. Use CodeDom to compile the client proxy class.
CodeNamespace nmspace = new CodeNamespace ("com. HS. services"); // Add a namespace for the proxy class. The default value is global space.
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. generateinmemory = true;
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 );

// 5. Use Reflection to call WebService.
If (! Result. Errors. HasErrors)
{
Assembly asm = result. CompiledAssembly;
Type t = asm. getType ("com. HS. services. dataProcessServiceImplService ", true, true); // if you have added a namespace for the proxy class before, add the namespace to the front of the type.

Obj = Activator. CreateInstance (t );

// Cache service proxy
WebServicesObjectCacheManager webServicesObjectCacheManager = new WebServicesObjectCacheManager ();
WebServicesObjectCacheManager. cache ("com. HS. services", t );

MethodInfo method = t. GetMethod (functionName );

If (method! = NULL)
{
Return method;
}
Else
{
Return null;
}
}
Else
{
Return null;
}
}
Catch (Exception)
{

Return null;
}
}

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.