承接基于.Net的系统研发,精通物流系统,特别是仓储物流管理,有意者请联系。

PDA访问WCF

 

    最近在做一个系统,客户端有两种类型,有PC和PDA。为了能支持不同的平台,并且复用业务逻辑,采用了WCF。PC及机部署的是WinForm的应用程序,比较容易。现在通过一个简单的例子说一下PDA如何做,注重的是这个过程。

   现在从最开始的服务创建开始:

1、创建服务接口、定义服务端和客户端之间的数据传输类 

[ServiceContract]
    public interface IWcfServcie
    {
        [OperationContract]
        double Add(double x, double y);

        [OperationContract]
        DTO Create(string name, int age);
    }

 

   [DataContract]
    public class DTO
    {
        [DataMember]
        public string Name = string.Empty;
        [DataMember]
        public int Age = 0;
    }

2、实现接口 

功能非常的简单,一个是将两个数加起来,一个是构造一个DTO对象。

public class WcfServcie : IWcfServcie
    {      
        public double Add(double x, double y)
        {
            return x + y;
        }

        public DTO Create(string name, int age)
        {
            DTO obj = new DTO();
            obj.Name = name;
            obj.Age = age + 1;
            return obj;
        }
    }

3、启动服务端发布服务

   当然发布的方式很多,IIS、Windows Service和WinForm应用程序等都可以作为发布WCF的宿主程序。这里为了简单,我使用console程序进行发布。最关键的还是Uri和binding,如果想在PDA上调用wcf服务,那么binding必须采用BasicHttpBinding,这点必须注意。

            Uri baseUri = new Uri("http://localhost:8080/wcfService");

            using (ServiceHost wcfServiceHost = new ServiceHost(typeof(Service.WcfServcie), baseUri))
            {
                BasicHttpBinding binding = new BasicHttpBinding();

                wcfServiceHost.AddServiceEndpoint(typeof(IWcfServcie), binding, string.Empty);

                ServiceMetadataBehavior behavior = wcfServiceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();

                if (behavior == null)
                {
                    behavior = new ServiceMetadataBehavior();
                    behavior.HttpGetEnabled = true;
                    behavior.HttpGetUrl = baseUri;
                    wcfServiceHost.Description.Behaviors.Add(behavior);
                }
                else
                {
                    behavior.HttpGetEnabled = true;
                    behavior.HttpGetUrl = baseUri;
                }

                wcfServiceHost.Open();

                Console.Read();
            }

4、检查服务是否已发布

编译后启动服务端程序,使用“:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\WcfTestClient.exe”,可以检查服务是否正常发布,当然也可以使用IE。我一般使用WcfTestClient.exe,它可以针对每个方法做测试。

5、创建PDA上WCF服务代理类

可以手动写这部分代码,如果不想自己写代理类,那就下载NETCFv35PowerToys.msi并安装,然后“:\Program Files\Microsoft.NET\SDK\CompactFramework\v3.5\bin”会有一个程序NetCFSvcUtil.exe。通过cmd执行“NetCFSvcUtil.exe http://localhost:8080/wcfService" ,\Program Files\Microsoft.NET\SDK\CompactFramework\v3.5\bin目录下会出现生成的两个文件CFClientBase.cs和WcfServcie.cs,这就是服务的代理类。需要注意的是WcfServcie.cs中”public static System.ServiceModel.EndpointAddress EndpointAddress = new System.ServiceModel.EndpointAddress("http://localhost:8080/wcfService");“,将”localhost“改为服务端的Ip。

6、创建SmartSeviceProject,平台根据具体项目情况决定,然后将上面创建的两个文件加入到项目中

      WcfServcieClient service = new WcfServcieClient();//服务代理对象

        private void button1_Click(object sender, EventArgs e)
        {
            this.textBox6.Text = service.Add(Convert.ToDouble(this.textBox1.Text), Convert.ToDouble(this.textBox2.Text)).ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DTO obj = null;
            obj = service.Create(textBox4.Text, Convert.ToInt32(textBox3.Text));
            textBox5.Text = string.Format("Name is : {0} Age is : {1}", obj.Name, obj.Age);
        }

7、编译SmartDevice项目后运行。打完,收工。

 

 项目代码:https://files.cnblogs.com/yiping06993010/WCFTest.rar

 

posted @ 2009-11-01 13:44  阿修罗一平  阅读(3410)  评论(5编辑  收藏  举报