Exemplo n.º 1
0
        public static object UploadData(string url, string @namespace, string classname,
                                        string methodname, object[] args, ArrayList al)
        {
            try
            {
                System.Net.WebClient wc = new System.Net.WebClient();
                //System.IO.Stream stream = wc.OpenRead(url + "?WSDL");
                System.IO.Stream stream = wc.OpenRead(url);
                System.Web.Services.Description.ServiceDescription         sd  = System.Web.Services.Description.ServiceDescription.Read(stream);
                System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter();
                sdi.AddServiceDescription(sd, string.Empty, string.Empty);                 //将sd描述导入到sdi中
                System.CodeDom.CodeNamespace   cn  = new System.CodeDom.CodeNamespace(@namespace);
                System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit();
                ccu.Namespaces.Add(cn);
                sdi.Import(cn, ccu);

                Microsoft.CSharp.CSharpCodeProvider   csc = new Microsoft.CSharp.CSharpCodeProvider();
                System.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompiler();

                System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.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");

                System.CodeDom.Compiler.CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
                if (cr.Errors.HasErrors)
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
                    {
                        sb.Append(ce.ToString());
                        sb.Append(System.Environment.NewLine);
                    }
                    throw new Exception(sb.ToString());
                }

                System.Reflection.Assembly assembly = cr.CompiledAssembly;
                Type   t   = assembly.GetType(@namespace + "." + classname, true, true);
                object obj = Activator.CreateInstance(t);

                ParameterModifier[] paramsModifer = new ParameterModifier[1];
                paramsModifer[0] = new ParameterModifier(args.Length);
                //paramsModifer[0][7] = true;

                object result = t.InvokeMember(methodname, BindingFlags.Default | BindingFlags.InvokeMethod, null,
                                               obj, args, paramsModifer, null, null);

                //al.Add(args[7]);
                return(result);
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message, EXCEPTION_LOG_TITLE);
                return(null);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 根据指定的信息,调用远程WebService方法
        /// </summary>
        /// <param name="url">WebService的http形式的地址</param>
        /// <param name="namespace">欲调用的WebService的命名空间</param>
        /// <param name="classname">欲调用的WebService的类名(不包括命名空间前缀)</param>
        /// <param name="methodname">欲调用的WebService的方法名</param>
        /// <param name="args">参数列表</param>
        /// <returns>WebService的执行结果</returns>
        /// <remarks>
        /// 如果调用失败,将会抛出Exception。请调用的时候,适当截获异常。
        /// 异常信息可能会发生在两个地方:
        /// 1、动态构造WebService的时候,CompileAssembly失败。
        /// 2、WebService本身执行失败。
        /// </remarks>
        /// <example>
        /// <code>
        /// object obj = InvokeWebService("http://localhost/Service.asmx","method",new object[]{"1"});
        /// </code>
        /// </example>
        public static object InvokeWebService(string url, string methodname, object[] args)
        {
            try
            {
                string               @namespace = "WebService.DynamicWebCalling";
                string[]             parts      = url.Split('/');
                string[]             pps        = parts[parts.Length - 1].Split('.');
                string               classname  = pps[0];
                System.Net.WebClient wc         = new System.Net.WebClient();
                System.IO.Stream     stream     = wc.OpenRead(url + "?WSDL");
                System.Web.Services.Description.ServiceDescription         sd  = System.Web.Services.Description.ServiceDescription.Read(stream);
                System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter();
                sdi.AddServiceDescription(sd, "", "");
                System.CodeDom.CodeNamespace   cn  = new System.CodeDom.CodeNamespace(@namespace);
                System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit();
                ccu.Namespaces.Add(cn);
                sdi.Import(cn, ccu);

                Microsoft.CSharp.CSharpCodeProvider   csc = new Microsoft.CSharp.CSharpCodeProvider();
                System.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompiler();

                System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.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");

                System.CodeDom.Compiler.CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
                if (true == cr.Errors.HasErrors)
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
                    {
                        sb.Append(ce.ToString());
                        sb.Append(System.Environment.NewLine);
                    }
                    throw new Exception(sb.ToString());
                }
                System.Reflection.Assembly assembly = cr.CompiledAssembly;
                Type   t   = assembly.GetType(@namespace + "." + classname, true, true);
                object obj = Activator.CreateInstance(t);
                System.Reflection.MethodInfo mi = t.GetMethod(methodname);
                return(mi.Invoke(obj, args));
            }
            catch (Exception ex)
            {
                StringBuilder str = new StringBuilder();
                for (int m = 0; m < args.Length; m++)
                {
                    str.Append(args[m]);
                    str.Append(",");
                }
                LogUtility.DataLog.WriteLog(LogUtility.LogLevel.Info, ex.Message + "--" + ex.Source.ToString() + "--" + url + methodname + "--" + str, new LogUtility.RunningPlace("WebServiceHelper", "InvokeWebService"), "WebService获取数据失败");
                return("");
            }
        }
Exemplo n.º 3
0
        /*
         * 优点:不用引用,通过网址,目标命名空间,调用WebServices,用于主站向子站请求数据
         * 缺点:使用上不如直接Web引用进来易用,如果只是单个并确定的,最好引用进来
         */
        /// <summary>
        /// 通过反射完成调用
        /// </summary>
        public static object InvokeWebSer(string url, string @namespace, string classname, string methodname, object[] args)
        {
            System.Net.WebClient wc = new System.Net.WebClient();
            string URL = string.Empty;

            if ((url.Substring(url.Length - 5, 5) != null) && ((url.Substring(url.Length - 5, 5).ToLower() != "?wsdl")))
            {
                URL = url + "?WSDL";
            }
            else
            {
                URL = url;
            }
            System.IO.Stream stream = wc.OpenRead(URL);
            System.Web.Services.Description.ServiceDescription         sd  = System.Web.Services.Description.ServiceDescription.Read(stream);
            System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter();
            sdi.AddServiceDescription(sd, "", "");
            System.CodeDom.CodeNamespace   cn  = new System.CodeDom.CodeNamespace(@namespace);
            System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit();
            ccu.Namespaces.Add(cn);
            sdi.Import(cn, ccu);

            Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider();

            System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.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");

            System.CodeDom.Compiler.CompilerResults cr = csc.CompileAssemblyFromDom(cplist, ccu);

            if (true == cr.Errors.HasErrors)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
                {
                    sb.Append(ce.ToString());
                    sb.Append(System.Environment.NewLine);
                }
                throw new Exception(sb.ToString());
            }
            System.Reflection.Assembly assembly = cr.CompiledAssembly;
            Type   t   = assembly.GetType(@namespace + "." + classname, true, true);
            object obj = Activator.CreateInstance(t);

            System.Reflection.MethodInfo mi = t.GetMethod(methodname);
            return(mi.Invoke(obj, args));
        }
Exemplo n.º 4
0
        private int InvokeWebservice(string url, string @namespace, string classname, string methodname, object[] args)
        {
            try
            {
                System.Net.WebClient wc     = new System.Net.WebClient();
                System.IO.Stream     stream = wc.OpenRead(url);
                System.Web.Services.Description.ServiceDescription         sd  = System.Web.Services.Description.ServiceDescription.Read(stream);
                System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter();
                sdi.AddServiceDescription(sd, "", "");
                System.CodeDom.CodeNamespace   cn  = new System.CodeDom.CodeNamespace(@namespace);
                System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit();
                ccu.Namespaces.Add(cn);
                sdi.Import(cn, ccu);

                Microsoft.CSharp.CSharpCodeProvider   csc = new Microsoft.CSharp.CSharpCodeProvider();
                System.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompiler();

                System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.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");

                System.CodeDom.Compiler.CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
                if (true == cr.Errors.HasErrors)
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
                    {
                        sb.Append(ce.ToString());
                        sb.Append(System.Environment.NewLine);
                    }
                    throw new Exception(sb.ToString());
                }
                System.Reflection.Assembly assembly = cr.CompiledAssembly;
                Type   t   = assembly.GetType(@namespace + "." + classname, true, true);
                object obj = Activator.CreateInstance(t);
                System.Reflection.MethodInfo mi = t.GetMethod(methodname);

                mi.Invoke(obj, args);
            }
            catch
            {
                return(-1);
            }

            return(1);
        }
Exemplo n.º 5
0
        //public void SendMail(string mailTo, string mailSubject, string content)
        //{
        //    object[] methodParams = new object[4];
        //    methodParams[0] = new object();
        //    methodParams[1] = new object();
        //    methodParams[2] = new object();
        //    methodParams[3] = new object();
        //    methodParams[0] = mailTo;
        //    methodParams[1] = true;
        //    methodParams[2] = mailSubject;
        //    methodParams[3] = content;
        //    InvokeWebservice(ConfigData.MailURL, "Achievo.MMIP.CommonWebService", "EMail", "SendByAdmin", methodParams);
        //}
        /// 根据指定的信息,调用远程WebService方法
        /// </summary>
        /// <param name="url">WebService的http形式的地址</param>
        /// <param name="namespace">欲调用的WebService的命名空间</param>
        /// <param name="classname">欲调用的WebService的类名(不包括命名空间前缀)</param>
        /// <param name="methodname">欲调用的WebService的方法名</param>
        /// <param name="args">参数列表</param>
        /// <returns>WebService的执行结果</returns>
        /// <remarks>
        /// 如果调用失败,将会抛出Exception。请调用的时候,适当截获异常。
        /// 异常信息可能会发生在两个地方:
        /// 1、动态构造WebService的时候,CompileAssembly失败。
        /// 2、WebService本身执行失败。
        /// </remarks>
        /// <example>
        /// <code>
        /// object obj = InvokeWebservice("http://localhost/GSP_WorkflowWebservice/common.asmx",
        ///                               "Genersoft.Platform.Service.Workflow",
        ///                               "Common",
        ///                               "GetToolType",
        ///                               new object[]{"1"});
        /// </code>
        /// </example>
        public object InvokeWebservice(string url, string @namespace, string classname,
                                              string methodname, object[] args)
        {
            try
            {
                System.Net.WebClient wc = new System.Net.WebClient();
                System.IO.Stream stream = wc.OpenRead(url + "?WSDL");
                System.Web.Services.Description.ServiceDescription sd = System.Web.Services.Description.ServiceDescription.Read(stream);
                System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter();
                sdi.AddServiceDescription(sd, "", "");
                System.CodeDom.CodeNamespace cn = new System.CodeDom.CodeNamespace(@namespace);
                System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit();
                ccu.Namespaces.Add(cn);

                sdi.Import(cn, ccu);
                Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider();
                System.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompiler();
                System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.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");
                System.CodeDom.Compiler.CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);

                if (true == cr.Errors.HasErrors)
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
                    {
                        sb.Append(ce.ToString());
                        sb.Append(System.Environment.NewLine);
                    }
                    throw new Exception(sb.ToString());
                }

                System.Reflection.Assembly assembly = cr.CompiledAssembly;
                Type t = assembly.GetType(@namespace + "." + classname, true, true);
                object obj = Activator.CreateInstance(t);
                System.Reflection.MethodInfo mi = t.GetMethod(methodname);
                return mi.Invoke(obj, args);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
            }
        }
        /// < summary> 
        /// 动态调用web服务 
        /// < /summary> 
        /// < param name="url">WSDL服务地址< /param> 
        /// < param name="classname">类名< /param> 
        /// < param name="methodname">方法名< /param> 
        /// < param name="args">参数< /param> 
        /// < returns>< /returns> 
        public static object Invoke(string url, string classname, string methodname, object[] args)
        {
            string ns = "EnterpriseServerBase.WebService.DynamicWebCalling";
            if ((classname == null) || (classname == ""))
            {
                classname = WebServiceInvoker.GetWsClassName(url);
            }
            System.Net.WebClient wc = new System.Net.WebClient();
            System.IO.Stream stream = wc.OpenRead(url + "?WSDL");
            System.Web.Services.Description.ServiceDescription sd = System.Web.Services.Description.ServiceDescription.Read(stream);
            System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter();
            sdi.AddServiceDescription(sd, "", "");
            System.CodeDom.CodeNamespace cn = new System.CodeDom.CodeNamespace(ns);
            System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit();
            ccu.Namespaces.Add(cn);
            sdi.Import(cn, ccu);

            Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider();
            // System.CodeDom.Compiler.ICodeCompiler csc = .CreateCompiler();

            System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.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");

            System.CodeDom.Compiler.CompilerResults cr = csc.CompileAssemblyFromDom(cplist, ccu);
            if (true == cr.Errors.HasErrors)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
                {
                    sb.Append(ce.ToString());
                    sb.Append(System.Environment.NewLine);
                }
                throw new NHExt.Runtime.Exceptions.RuntimeException("调用WebService开始编译发生错误:" + sb.ToString());
            }
            System.Reflection.Assembly assembly = cr.CompiledAssembly;
            Type t = assembly.GetType(ns + "." + classname, true, true);
            object obj = Activator.CreateInstance(t);
            System.Reflection.MethodInfo mi = t.GetMethod(methodname);
            return mi.Invoke(obj, args);
        }
Exemplo n.º 7
0
        public object CallWebService(WsDTO param)
        {
            string ErrWebService = string.Empty;

            System.Net.WebClient client = new System.Net.WebClient();

            // Connect To the web service

            System.IO.Stream stream = client.OpenRead(this.UrlWebService + "?wsdl");

            // Now read the WSDL file describing a service.

            var description = System.Web.Services.Description.ServiceDescription.Read(stream);

            ///// LOAD THE DOM /////////

            // Initialize a service description importer.

            var importer = new System.Web.Services.Description.ServiceDescriptionImporter();

            importer.ProtocolName = "Soap12"; // Use SOAP 1.2.

            importer.AddServiceDescription(description, null, null);

            // Generate a proxy client.

            importer.Style = System.Web.Services.Description.ServiceDescriptionImportStyle.Client;

            // Generate properties to represent primitive values.

            importer.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties;

            // Initialize a Code-DOM tree into which we will import the service.

            var nmspace = new System.CodeDom.CodeNamespace();

            var unit1 = new System.CodeDom.CodeCompileUnit();

            unit1.Namespaces.Add(nmspace);

            // Import the service into the Code-DOM tree. This creates proxy code that uses the service.

            var warning = importer.Import(nmspace, unit1);

            if (warning == 0) // If zero then we are good to go
            {
                // Generate the proxy code

                var provider1 = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp");

                // Compile the assembly proxy with the appropriate references

                string[] assemblyReferences = new string[5] {
                    "System.dll", "System.Web.Services.dll", "System.Web.dll", "System.Xml.dll", "System.Data.dll"
                };

                var parms = new System.CodeDom.Compiler.CompilerParameters(assemblyReferences);

                var results = provider1.CompileAssemblyFromDom(parms, unit1);

                // Check For Errors

                if (results.Errors.Count > 0)
                {
                    foreach (System.CodeDom.Compiler.CompilerError oops in results.Errors)
                    {
                        ErrWebService += " " + oops.ErrorText;
                    }

                    throw new System.Exception(string.Format("Error al llamar a WS [{0}]", ErrWebService));
                }

                // Finally, Invoke the web service method

                object wsvcClass = results.CompiledAssembly.CreateInstance(param.ServiceName);

                var mi = wsvcClass.GetType().GetMethod(param.MethodName);

                return(mi.Invoke(wsvcClass, param.Args));
            }

            else
            {
                return(null);
            }
        }
Exemplo n.º 8
0
		public static object UploadData(string url, string @namespace, string classname, 
			string methodname, object[] args, ArrayList al)
		{
			try
			{
				System.Net.WebClient wc = new System.Net.WebClient();
				//System.IO.Stream stream = wc.OpenRead(url + "?WSDL");
				System.IO.Stream stream = wc.OpenRead(url);
				System.Web.Services.Description.ServiceDescription sd = System.Web.Services.Description.ServiceDescription.Read(stream);
				System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter();
				sdi.AddServiceDescription(sd, string.Empty, string.Empty); //将sd描述导入到sdi中
				System.CodeDom.CodeNamespace cn = new System.CodeDom.CodeNamespace(@namespace);
				System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit();
				ccu.Namespaces.Add(cn);
				sdi.Import(cn, ccu);

				Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider();
				System.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompiler();

				System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.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");

				System.CodeDom.Compiler.CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
				if (cr.Errors.HasErrors)
				{
					System.Text.StringBuilder sb = new System.Text.StringBuilder();
					foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
					{
						sb.Append(ce.ToString());
						sb.Append(System.Environment.NewLine);
					}
					throw new Exception(sb.ToString());
				}

				System.Reflection.Assembly assembly = cr.CompiledAssembly;
				Type t = assembly.GetType(@namespace + "." + classname, true, true);
				object obj = Activator.CreateInstance(t);

				ParameterModifier[] paramsModifer = new ParameterModifier[1];
				paramsModifer[0] = new ParameterModifier(args.Length);
				//paramsModifer[0][7] = true;

				object result = t.InvokeMember(methodname, BindingFlags.Default | BindingFlags.InvokeMethod, null,
					obj, args, paramsModifer, null, null);

				//al.Add(args[7]);
				return result;
			}
			catch (Exception ex)
			{
				WriteLog(ex.Message, EXCEPTION_LOG_TITLE);
				return null;
			}
		}