PDA訪問WCF

來源:互聯網
上載者:User

 

    最近在做一個系統,用戶端有兩種類型,有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項目後運行。打完,收工。

 

 項目代碼:http://files.cnblogs.com/yiping06993010/WCFTest.rar

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.