动态调用WebService服务

 

        private void CallbackStateChangeEvent(Guid instanceId, string state, bool valid)
        {
            string url = System.Configuration.ConfigurationManager.AppSettings["StateEx.CallbackWBS"];
            BaseStateMachineFlow flow = FlowLogic.GetOwnerStateMachineWorkflow(this);
            if (!string.IsNullOrEmpty(flow.Gi.CallbackWBS))//如果为该流程指定了节点状态改变时回调的WebService地址
                url = flow.Gi.CallbackWBS;
            if (string.IsNullOrEmpty(url))
                return;
            if (!url.ToUpper().EndsWith("?WSDL"))
                url += "?WSDL";
            Console.WriteLine("Callback WebService:"+url);

            // 1. 使用 WebClient 下载 WSDL 信息。
            WebClient web = new WebClient();
            Stream stream = web.OpenRead(url);//"http://localhost:5544/WebSite1/WebService.asmx?WSDL"

            // 2. 创建和格式化 WSDL 文档。
            ServiceDescription description = ServiceDescription.Read(stream);

            // 3. 创建客户端代理代理类。
            ServiceDescriptionImporter importer = new ServiceDescriptionImporter();

            importer.ProtocolName = "Soap"; // 指定访问协议。
            importer.Style = ServiceDescriptionImportStyle.Client; // 生成客户端代理。
            importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;

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

            // 4. 使用 CodeDom 编译客户端代理类。
            CodeNamespace nmspace = new CodeNamespace(); // 为代理类添加命名空间,缺省为全局空间。
            CodeCompileUnit unit = new CodeCompileUnit();
            unit.Namespaces.Add(nmspace);

            ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);
            CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");

            CompilerParameters parameter = new CompilerParameters();
            parameter.GenerateExecutable = false;
            parameter.GenerateInMemory = true;
            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);

            // 5. 使用 Reflection 调用 WebService。
            if (!result.Errors.HasErrors)
            {
                Assembly asm = result.CompiledAssembly;
                Type typeWBS = null;
                foreach(Type t in asm.GetTypes()) // 如果在前面为代理类添加了命名空间,此处需要将命名空间添加到类型前面。
                {
                    if (t.IsSubclassOf(typeof(System.Web.Services.Protocols.SoapHttpClientProtocol)))
                    {
                        typeWBS = t;
                        break;
                    }
                }

                object o = Activator.CreateInstance(typeWBS);
                MethodInfo method = typeWBS.GetMethod("OnStateChangeEvent");//WebService必须有一个名为OnStateChangeEvent的Web函数,格式如下:public string OnStateChangeEvent(Guid instanceId, string state, string billType, string billNo, bool valid)
                string s = (string)method.Invoke(o, new object[]{instanceId, state, flow.Gi.BillType, flow.Gi.BillNo, valid});
            }
        }

 

引用命名空间列表:

using System.Net;
using System.Web.Services;
using System.Web.Services.Description;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Xml.Serialization;

你可能感兴趣的:(webservice)