C#动态调用WebService

 
代码
namespace  DynamicCallWebService
{
    
public   class  WebServiceCaller
    {
        
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)
        {
            
string  @namespace  =   " EnterpriseServerBase.WebService.DynamicWebCalling " ;
            
if  ( string .IsNullOrEmpty(className))
            {
                className 
=  GetWsClassName(url);
            }
            
try
            {
                
// Get WSDL
                 string  wsdlUrl  =  url  +   " ?WSDL " ;
                WebClient webClient 
=   new  WebClient();
                Stream stream 
=  webClient.OpenRead(wsdlUrl);
                ServiceDescription sd 
=  ServiceDescription.Read(stream);
                
if  ( ! string .IsNullOrEmpty(sd.Name)  &&  sd.Name  !=  className)
                    className 
=  sd.Name;
                ServiceDescriptionImporter sdi 
=   new  ServiceDescriptionImporter();
                sdi.AddServiceDescription(sd, 
"" "" );

                
                
//  Add any child namespaces First
                 foreach  (Import schemaImport  in  sd.Imports)
                {
                    Uri baseUri 
=   new  Uri(wsdlUrl);
                    
string  schemaLocation  =  schemaImport.Location;
                    
if  (schemaLocation  ==   null )
                        
continue ;
                    Uri schemaUri 
=   new  Uri(baseUri, schemaLocation);

                    
using  (Stream schemaStream  =  webClient.OpenRead(schemaUri))
                    {
                        
try
                        {
                            ServiceDescription sdImport 
=  ServiceDescription.Read(schemaStream,  true );
                            sdImport.Namespaces.Add(
" wsdl " , schemaImport.Namespace);
                            sdi.AddServiceDescription(sdImport, 
null null );
                        }
                        
catch  { }   //  ignore schema import errors
                    }
                }

                
//  Download and inject any imported schemas (ie. WCF generated WSDL)            
                 foreach  (XmlSchema wsdlSchema  in  sd.Types.Schemas)
                {
                    
//  Loop through all detected imports in the main schema
                     foreach  (XmlSchemaObject externalSchema  in  wsdlSchema.Includes)
                    {
                        
//  Read each external schema into a schema object and add to importer
                         if  (externalSchema  is  XmlSchemaImport)
                        {
                            Uri baseUri 
=   new  Uri(wsdlUrl);
                            
string  exSchemaLocation  =  ((XmlSchemaExternal)externalSchema).SchemaLocation;
                            
if  ( string .IsNullOrEmpty(exSchemaLocation))
                                
continue ;

                            Uri schemaUri 
=   new  Uri(baseUri, exSchemaLocation);

                            
using  (Stream schemaStream  =  webClient.OpenRead(schemaUri))
                            {
                                
try
                                {
                                    System.Xml.Schema.XmlSchema schema 
=  XmlSchema.Read(schemaStream,  null );
                                    sdi.Schemas.Add(schema);
                                }
                                
catch  { }   //  ignore schema import errors                                                        
                            }
                        }
                    }
                }

                CodeNamespace cn 
=   new  CodeNamespace(@namespace);
                
// Create Client proxy class
                CodeCompileUnit ccu  =   new  CodeCompileUnit();
                ccu.Namespaces.Add(cn);
                sdi.Import(cn, ccu);
                CSharpCodeProvider csc 
=   new  CSharpCodeProvider();
                ICodeCompiler icc 
=  csc.CreateCompiler();
                
// Set complier 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 " );
                
// complie proxy class
                CompilerResults cr  =  icc.CompileAssemblyFromDom(cplist, ccu);
                
if  ( true   ==  cr.Errors.HasErrors)
                {
                    StringBuilder sb 
=   new  StringBuilder();
                    
foreach  (CompilerError ce  in  cr.Errors)
                    {
                        sb.Append(ce.ToString());
                        sb.Append(Environment.NewLine);
                    }
                    
throw   new  Exception(sb.ToString());
                }
                
// Generate proxy instance and invoke mehtod
                Assembly assembly  =  cr.CompiledAssembly;
                Type t 
=  assembly.GetType(@namespace  +   " . "   +  className,  true true );
                
object  obj  =  Activator.CreateInstance(t);
                MethodInfo method 
=  t.GetMethod(methodName);
                
return  method.Invoke(obj, args);
            }
            
catch  (Exception ex)
            {
                
throw   new  Exception(ex.InnerException.Message,  new  Exception(ex.InnerException.StackTrace));
            }
        }

        
public   static   string  GetWsClassName( string  wsUrl)
        {
            
string [] parts  =  wsUrl.Split( ' / ' );
            
string [] pps  =  parts[parts.Length  -   1 ].Split( ' . ' );
            
return  pps[ 0 ];
        }
    }
}


你可能感兴趣的:(webservice)