/// 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;
}
}