VRS简易接口<连载5>—C#通过SOAP调用

在VS中制作一个HelloSoap类,将姓名参数传递给规则服务,并调用hello规则包,显示返回的欢迎辞。
新建C#工程
在VS中,新建一个名为HelloSoap的控制台应用程序的工程:





添加引用
将RuleEngine.dll添加到引用中。该文件一般位于VisualRulesSolution安装目录的samples\notnet\RuleEngine\bin\Release目录下:





添加服务引用





然后输入Soap服务所在的地址,在地址栏中输入http://192.168.19.128:8880/soap/services/RuleSoap?wsdl 。
点击“前往”后,可以看到规则服务提供的接口。

输入一个命名空间后,点击确认。






图3-1-2-5-4 添加服务引用
编写Program.cs
在此中输入三个类,RuleSoapFactory、RuleSoapService、Program,其中RuleSoapFactory、RuleSoapService是对规则调用接口的实现。其他程序都可以像Program操作的那样,调用规则包。:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RuleEngine;
namespace HelloSoap
{
    public class RuleSoapFactory : RuleServiceFactory
    {
        override public RuleService RuleService
        {
            get
            {
                return new RuleSoapService(this);
            }
        }
        public RuleSoapFactory()
            : base()
        {
        }
        public virtual void close()
        {
        }
        public virtual void forceClose()
        {
            this.close();
        }
        public virtual void open()
        {
        }
    }
    public class RuleSoapService : AbstractRuleService
    {
        private RuleSoapFactory Factory
        {
            get
            {
                return (RuleSoapFactory)factory;
            }
        }
        public RuleSoapService(RuleSoapFactory factory)
            : base(factory)
        {
        }
        protected override System.String send(System.String xml)
        {
            try
            {
                HelloSoap.ServiceReference1.RuleSoapClient soap = new HelloSoap.ServiceReference1.RuleSoapClient();
                return soap.getRule(xml);
            }
            catch (System.Exception e)
            {
                Factory.forceClose();
                throw new RuleServiceException(e);
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            RuleSoapFactory factory = new RuleSoapFactory();
            RuleService service = factory.RuleService;
            service.put("name", "测试Soap访问");
            service.execute("hello");
            Console.WriteLine(service.getString("welcome"));
            Console.Read();
        }
    }
}
执行测试类
点击执行后,看到执行结果如下,说明已经调用规则包成功:




猜你喜欢

转载自silencelight.iteye.com/blog/2257022