Ferry Kurniawan

Every Accomplishment Start With The Decision to Try
See also: Other Geeks@INDC

Creating WCF Client in Windows Mobile

Setelah satu bulan tidak menulis di blog (dikarnakan keikut-sertaan saya dalam mengurus putri pertama saya yang baru lahir :-) ) , maka pada kesempatan kali ini saya akan sedikit berbagi tulisan mengenai pembuatan “WCF Client di Windows Mobile”. Meskipun windows phone 7 sudah diluncurkan pada tahun ini tetapi saya tetap mengutak-atik mengenai pengembangan windows mobile 6.5 kebawah yang tentunya dengan .Net Compact Frameworknya. Untuk penjelasan WCF dengan konsep ABC (Address, Binding, Contract ) tidak saya bahas ulang. Berikut ini langkah-langkahnya:

I. Pembuatan WCF Service :

1. Buka VS 2008 anda.

2. Buat project “WCF Service Library” baru kemudian beri nama dengan WCFLanguage (disini memakai  C#)

3. Kemudian kopikan code dibawah ini di file app.config project tersebut

    Catatan : - Address Host saya gunakan : http://192.168.10.68:8282/WCFLanguage/Language

                   - EndPoint  : <endpoint address="mex"   binding="mexHttpBinding"  contract="IMetadataExchange"/>

                   - Ganti sesuai settingan anda

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <configuration>
   3:   <system.web>
   4:     <compilation debug="true" />
   5:   </system.web>
   6:   <!-- When deploying the service library project, the content
   7: of the config file must be added to the host's app.config
   8: file. System.Configuration does not support config files
   9: for libraries. -->
  10:   <system.serviceModel>
  11:     <services>
  12:       <service name="WCFLanguage.Language"
  13:       behaviorConfiguration="WCFLanguage.LanguageBehavior">
  14:         <host>
  15:           <baseAddresses>
  16:             <add baseAddress ="http://192.168.10.68:8282/WCFLanguage/Language" />
  17:           </baseAddresses>
  18:         </host>
  19:         <!-- Service Endpoints -->
  20:         <!-- Unless fully qualified, address is relative to
  21: base address supplied above -->
  22:         <endpoint address =""
  23:         binding="basicHttpBinding"
  24:         contract="WCFLanguage.ILanguage" />
  25:         <!--
  26: Upon deployment, the following identity element
  27: should be removed or replaced.
  28: -->
  29:         <!-- Metadata Endpoints -->
  30:         <endpoint address="mex"
  31:         binding="mexHttpBinding"
  32:         contract="IMetadataExchange"/>
  33:       </service>
  34:       
  35:     </services>
  36:     <behaviors>
  37:       <serviceBehaviors>
  38:         <behavior name="WCFLanguage.LanguageBehavior">
  39:           <!-- To avoid disclosing metadata information,
  40: set the value below to false and remove the metadata
  41: endpoint above before deployment -->
  42:           <serviceMetadata httpGetEnabled="True"/>
  43:           <!-- To receive exception details in faults for
  44: debugging purposes, set the value below to true.
  45: Set to false before deployment to avoid disclosing
  46: exception information -->
  47:           <serviceDebug includeExceptionDetailInFaults="False" />
  48:         </behavior>
  49:  
  50:       </serviceBehaviors>
  51:     </behaviors>
  52:   </system.serviceModel>
  53: </configuration>

4. Ganti nama file IService1.cs ke ILanguage.cs untuk pembuatan Interface yang digunakan dalam pendefinisian contract Language.

    Kemudian ketikkan code berikut ini.

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Runtime.Serialization;
   5: using System.ServiceModel;
   6: using System.Text;
   7:  
   8: namespace WCFLanguage
   9: {
  10:     
  11:     [ServiceContract]
  12:     public interface ILanguage
  13:     {
  14:         [OperationContract]
  15:         string[] GetAllLanguages();
  16:         [OperationContract]
  17:         string ChooseLanguage(string Code);
  18:         
  19:     }
  20:  
  21:    
  22:     [DataContract]
  23:     public class DataLanguage
  24:     {
  25:         string strLanguage = "";
  26:         string strResult = "";
  27:  
  28:         [DataMember]
  29:         public string StringLanguage
  30:         {
  31:             get { return strLanguage; }
  32:             set { strLanguage = value; }
  33:         }
  34:  
  35:         [DataMember]
  36:         public string StringResult
  37:         {
  38:             get { return strResult; }
  39:             set { strResult = value; }
  40:         }
  41:     }
  42: }

 

5. Ganti nama file Service1.cs menjadi Language.cs kemudian ketikkan code berikut ini.

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Runtime.Serialization;
   5: using System.ServiceModel;
   6: using System.Text;
   7:  
   8: namespace WCFLanguage
   9: {
  10:     // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in App.config.
  11:     public class Language : ILanguage
  12:     {
  13:         public string[] GetAllLanguages()
  14:         {
  15:             string[] strLanguages = new string[3];
  16:             strLanguages[0] = "C#";
  17:             strLanguages[1] = "F#";
  18:             strLanguages[2] = "VB .Net";
  19:  
  20:             return strLanguages;
  21:         }
  22:         public string ChooseLanguage(string Code)
  23:         {
  24:             string strResult = "";
  25:             if (Code == "C#")
  26:             {
  27:                 strResult = "Amazing language";
  28:             }
  29:             else if (Code == "F#")
  30:             {
  31:                 strResult = "Interesting New Language";
  32:             }
  33:             else if (Code == "VB .Net")
  34:             {
  35:                 strResult = "Familiar Language";
  36:             }
  37:             return strResult;
  38:         }
  39:  
  40:         public DataLanguage YourFavLanguage (string Code)
  41:         {
  42:             var L = new DataLanguage();
  43:             L.StringLanguage  = Code;
  44:  
  45:             string strResult = "";
  46:             strResult = ChooseLanguage(L.StringLanguage);
  47:             L.StringResult = strResult;
  48:             return L;
  49:  
  50:         }
  51:     }
  52: }

6. Jalankan project tersebut maka akan tampil seperti berikut. Service WCFLanguage siap diimplementasikan.

2

II. Pembuatan WCF Client di Windows Mobile.

1. Download .Net Compact Framework Power Toys 3.5 disini, kemudian install.

2. Pada project tadi pilh menu File –> Add –> New Project.

3. Pilih SmartDevice (C#), kemudian simpan dengan nama WMClient.

4. Target Device ke Windows Mobile 6

5. Tambahkan namespace System.Runtime.Serialization dan System.ServiceModel melalui Add References

6. Kopikan file WCFLanguage.dll (di folder project anda) ke path “C:\Program Files\Microsoft.NET\SDK\CompactFramework\v3.5\bin

7. Jalankan Service WCFLanguage tadi (Point I.6)

8. Buka Command Prompt kemudian ketikkan “CD C:\Program Files\Microsoft.NET\SDK\CompactFramework\v3.5\bin”

9. Ketikkan di Command prompt tadi seperti berikut “NETCFSvcUtil.exe http://192.168.10.68:8282/WCFLanguage/Language

10. Ketikkan di Command prompt seperti berikut “NETCFSvcUtil.exe WCFLanguage.dll

11. Ketikkan di Command prompt seperti berikut “NETCFSvcUtil.exe *.wsdl *.xsd”

12. Kembali ke project WMClient anda, kemudian buat tampilan seperti berikut ini.

3

13. Pilih menu Project –> Add Existing Item. Kemudian buka path “C:\Program Files\Microsoft.NET\SDK\CompactFramework\v3.5\bin”

      Tambahkan file Language.cs dan CFClientBase.cs ke project tersebut.

13. Ketikkan di code berikut ini di Form1.cs

   1: using System;
   2: using System.Linq;
   3: using System.Collections.Generic;
   4: using System.ComponentModel;
   5: using System.Data;
   6: using System.Drawing;
   7: using System.Text;
   9: using System.Runtime.Serialization;
  10: using System.ServiceModel;
   8: using System.Windows.Forms;
  11:  
  12:  
  13: namespace WMClient
  14: {
  15:     public partial class Form1 : Form
  16:     {
  17:         public Form1()
  18:         {
  19:             InitializeComponent();
  20:         }
  21:  
  22:         private void button1_Click(object sender, EventArgs e)
  23:         {
  24:  
  25:             var l = new LanguageClient();
  26:             MessageBox.Show(l.ChooseLanguage(cboLanguage.SelectedItem.ToString()));
  27:  
  28:             
  29:            
  30:         }
  31:  
  32:         private void Form1_Load(object sender, EventArgs e)
  33:         {
  34:             var l = new LanguageClient();
  35:             string[] strLang = l.GetAllLanguages();
  36:  
  37:             cboLanguage.Items.Clear();
  38:             foreach (string str in strLang)
  39:             {
  40:                 cboLanguage.Items.Add(str);
  41:             }
  42:             cboLanguage.SelectedIndex = 0;
  43:             l = null;
  44:  
  45:         }
  46:  
  47:        
  48:     }
  49: }

14. Jalankan Service WCFLanguage tadi (Point I.6)

15 Kemudian jalankan pula Project SmartDevice WMClient melalui Klik Kanan –> Debug –> Start New Instance

16. Hasil akhir seperti berikut ini.

4

17. Source code dapat diunduh disini

18. Terima Kasih dan Semoga Bermanfaat :)

Share this post: | | | |

Comments

reyza said:

hasilnya amazing language? itu pasti setelah milih C# atau F# ya? :)

# May 31, 2010 9:47 AM

ferrykur said:

haha yup bener bos ( meski tetep cinta VB .Net  :p )

# May 31, 2010 9:59 AM

Twitter Trackbacks for Creating WCF Client in Windows Mobile - Ferry Kurniawan [netindonesia.net] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Creating WCF Client in Windows Mobile - Ferry Kurniawan         [netindonesia.net]        on Topsy.com

# May 31, 2010 10:46 AM