zoukankan      html  css  js  c++  java
  • 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, 
    nullnull);
                            }
                            
    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, truetrue);
                    
    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];
            }
        }
    }


  • 相关阅读:
    Java 实现 蓝桥杯 生兔子问题
    Java实现 蓝桥杯 基因牛的繁殖
    Java实现 蓝桥杯 基因牛的繁殖
    Java实现 蓝桥杯 基因牛的繁殖
    Java实现 LeetCode 33 搜索旋转排序数组
    Java实现 LeetCode 33 搜索旋转排序数组
    Java实现 LeetCode 33 搜索旋转排序数组
    深入探究VC —— 资源编译器rc.exe(3)
    深入探究VC —— 编译器cl.exe(2)
    深入探究VC —— 编译器cl.exe(1)
  • 原文地址:https://www.cnblogs.com/liuwenjun830/p/1869704.html
Copyright © 2011-2022 走看看