Exemple #1
0
        /// <summary>
        /// 创建WebService,生成客户端代理程序集文件
        /// </summary>
        /// <param name="error">错误信息</param>
        /// <returns>返回:true或false</returns>
        public static bool CreateWebService(DBandMES dbandMES, out string error)
        {
            bool result = false;

            try {
                error = string.Empty;
                m_OutputDllFilename = dbandMES.WebServiceName + ".dll";
                m_ProxyClassName    = dbandMES.WebServiceName;
                m_Methods           = dbandMES.GetMethodArray();
                string webServiceUrl = dbandMES.WebServiceAddress;
                string strWSDL       = dbandMES.WebServiceWSDL;

                // 如果程序集已存在,直接使用
                if (File.Exists(Path.Combine(Environment.CurrentDirectory, m_OutputDllFilename)))
                {
                    BuildMethods(dbandMES.GetMethodArray());
                    return(true);
                }

                if (dbandMES.UseURL)
                {
                    // 使用 WebClient 下载 WSDL 信息。
                    WebClient web    = new WebClient();
                    Stream    stream = web.OpenRead(webServiceUrl);
                    if (stream != null)
                    {
                        // 通过加载stream流来格式化WSDL
                        ServiceDescription description = ServiceDescription.Read(stream);
                        // 编译输出 WebService 程序集
                        result = CreateWebServiceAssembly(description, dbandMES, ref error);
                    }
                    else
                    {
                        error = "打开WebServiceUrl失败";
                    }
                    web.Dispose();
                    stream.Close();
                    stream.Dispose();
                }
                else
                {
                    if (strWSDL != "")
                    {
                        // 通过加载xml文件来格式化WSDL
                        ServiceDescription description = ServiceDescription.Read(strWSDL);
                        // 编译输出 WebService 程序集
                        result = CreateWebServiceAssembly(description, dbandMES, ref error);
                    }
                    else
                    {
                        error = "WSDL文件路径不能为空";
                    }
                }
            } catch (Exception ex) {
                error = ex.Message;
            }
            return(result);
        }
Exemple #2
0
        private static bool CreateWebServiceAssembly(ServiceDescription description, DBandMES dbandMES, ref string error)
        {
            // 创建客户端代理类。
            ServiceDescriptionImporter importer = new ServiceDescriptionImporter {
                ProtocolName          = "Soap",
                Style                 = ServiceDescriptionImportStyle.Client,
                CodeGenerationOptions =
                    CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync
            };

            // 添加 WSDL 文档。
            importer.AddServiceDescription(description, null, null);

            //使用 CodeDom 编译客户端代理类。
            CodeNamespace   nmspace = new CodeNamespace();
            CodeCompileUnit unit    = new CodeCompileUnit();

            unit.Namespaces.Add(nmspace);

            ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);

            error += "ServiceDescriptionImportWarnings\"" + GetWarningMeaning(warning) + "\", ";

            using (CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp")) {
                CompilerParameters parameter = new CompilerParameters {
                    GenerateExecutable = false,
                    // 指定输出dll文件名。
                    OutputAssembly = m_OutputDllFilename
                };

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

                // 使用 Reflection 调用 WebService。
                if (!result.Errors.HasErrors)
                {
                    BuildMethods(dbandMES.GetMethodArray());
                    if (error.Length > 0)
                    {
                        error = error.Substring(0, error.Length - 3);
                    }
                    return(true);
                }
                else
                {
                    error += "反射生成dll文件时异常";
                }
            }
            return(false);
        }