lingdanglfw(DAX)

导航

Dynamics Webservice Call with Credential

Dynamics Webservice call with credential

 

 

    /// <summary>

    ///WebServiceHelper 的摘要说明

    /// </summary>

    public class WebServiceHelper

    {

        #region InvokeWebService

 

        /// <summary>

        /// 动态调用web服务

        /// </summary>

        /// <param name="url">URL</param>

        /// <param name="methodname">方法名</param>

        /// <param name="args">参数</param>

        /// <returns>object</returns>

        public static object InvokeWebService(string user,string password, string domain, string url, string methodname, object[] args)

        {

            return WebServiceHelper.InvokeWebService(user, password, domain, url, null, methodname, args);

        }

 

        /// <summary>

        /// 动态调用web服务

        /// </summary>

        /// <param name="url">URL地址</param>

        /// <param name="classname">类名</param>

        /// <param name="methodname">方法名</param>

        /// <param name="args">方法参数</param>

        /// <returns>方法返回值</returns>

        public static object InvokeWebService(string user, string password, string domain, string url, string classname, string methodname, object[] args)

        {

            string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";

 

            if ((classname == null) || (classname == ""))

            {

                classname = WebServiceHelper.GetWsClassName(url);

            }

 

            try

            {

                //获取WSDL

                WebClient wc = new WebClient();

 

 

                wc.Credentials = new NetworkCredential(user,password,domain);

               

 

                Stream stream = wc.OpenRead(url + "?WSDL");

                ServiceDescription sd = ServiceDescription.Read(stream);

                ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();

 

                sdi.ProtocolName = "Soap"; //指定访问协议。

                sdi.Style = ServiceDescriptionImportStyle.Client; //生成客户端代理。

                sdi.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;

 

 

                sdi.AddServiceDescription(sd, "", "");

                CodeNamespace cn = new CodeNamespace(@namespace);

 

                //生成客户端代理类代码

                CodeCompileUnit ccu = new CodeCompileUnit();

                ccu.Namespaces.Add(cn);

                sdi.Import(cn, ccu);

                CSharpCodeProvider csc = new CSharpCodeProvider();

 

                ICodeCompiler icc = csc.CreateCompiler();

                //设定编译参数

                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");

 

                //编译代理类

                CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);

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

                ParameterInfo[] pars = mi.GetParameters();

                for (int i = 0; i < pars.Length; i++)

                {

                    if (args[i].GetType().Name != pars[i].ParameterType.Name)

                    {

                        args[i] = Convert.ChangeType((object)args[i], pars[i].ParameterType);

                    }

                }

 

               

 

                Type type = obj.GetType();

                PropertyInfo propInfo = obj.GetType().GetProperty("Credentials");

                propInfo.SetValue(obj, new NetworkCredential(user, password, domain), null);

 

 

                return mi.Invoke(obj, args);

            }

            catch (Exception ex)

            {

                return "Error-" + ex.Message + ex.InnerException.Message;

            }

 

 

        }

 

        /// <summary>

        ///   GetWsClassName 

        /// </summary>

        /// <param name="strUrl">URL</param>

        /// <returns>ClassName</returns>

        private static string GetWsClassName(string strUrl)

        {

            string[] parts = strUrl.Split('/');

            string[] pps = parts[parts.Length - 1].Split('.');

            return pps[0];

        }

        #endregion

    }

 

posted on 2013-10-31 23:11  lingdanglfw  阅读(367)  评论(0编辑  收藏  举报