Using WCF in Windows Mobile Application


Create Sample WCF service

  • Open Visual studio 2008

  • Create new project from File --> New --> Project

    1.gif

  • Create WCF service Application

  • Select Web project types under project type section

  • Select WCF Service Application to create the WCF service project

  • Save the application in your local drive

    2.gif

  • After creating the project the following files will be created in the solution.

    3.gif

  • Open service interface file (IService1) and delete the automatically created code.

  • Copy below mentioned code in IService1 file.

    namespace WcfService1
    {
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            int GetAddResult(int value1,int value2);
        }
    }

    4.gif

  • Open service svc file (Service1.svc.cs) and delete the automatically created code.

  • Copy below mentioned code in Service1.svc.cs file.

    namespace WcfService1
    {
        public class Service1 : IService1
        {
            public int GetData(int value1,int value2)
            {
                return value1 + value2;
            }
        }
    }

    5.gif

  • Browse the WCF service application in internet explore

  • Create service methods will appear in the internet explorer as shown in the below screen capture

  • Note down the WCF service URL to add the web reference in mobile application.

    6.gif

Create Sample Mobile application
  • Open Visual studio 2008

  • Create new project from File --> New --> Project

  • Select Smart Device project type and Template is Smart Device Project

    7.gif

  • Select Target platform as per requirement. In the target platform dropdown select suitable mobile platform.

    8.gif

  • Select Device Application Template and click ok.

    9.gif

  • Creates the mobile window form to develop the mobile application

  • Add two text boxes and one button controls to test the service as shown in the below screen shot.

    10.gif

To Consume WCF service in Windows 7 Mobile application
  • Download NETCv35PowerToys.msi from following link.

    http://www.microsoft.com/downloads/en/details.aspx?FamilyID=c8174c14-a27d-4148-bf01-86c2e0953eab&displaylang=en

  • After installing above msi file it will copy the NetCFSvcUtil.exe, but this will work for Windows 7.

  • To work in windows 7 we need to down load updated NetCFSvcUtil.exe file from following link.

    http://blogs.msdn.com/b/habibh/archive/2009/06/26/netcfsvcutil-exe-and-windows-7.aspx

  • Copy the downloaded file to the following location

    C:\Program Files\Microsoft.NET\SDK\CompactFramework\v3.5\bin\

  • Open WCF service web.conig file and change the binding mode in endpoint section as mentioned below

    <endpoint address="" binding="wsHttpBinding" contract="WcfService1.IService1">

    To

    <endpoint address="" binding="basicHttpBinding" contract="WcfService1.IService1">

  • Open command prompt and run the following command

    co1.gif

     co2.gif

    11.gif

  • It generates two files as shown in the above screen shot (Service1.cs and CFClientBase.cs).

  • Add he created two files into mobile application project.

    12.gif

  • Add following code in link event

    private void linkLabel1_Click(object sender, EventArgs e)
    {
        Service1Client proxy = new Service1Client();
        int resutl = proxy.GetAddResult(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text));
        MessageBox.Show(resutl.ToString());
    }

  • Open Device Emulator Manager and Windows Mobile Device Center.

  • Select the Emulator which is our running application.

  • Click on Cradle to connect to the Windows Mobile Device Center.

    13.gif

    14.gif

  • Once connected to the Windows Mobile Device Center Run the out mobile application.

  • Once started the application will open Deploy SmartDeviceProject1 dialog box to chose the Emulator

    15.gif

  • Click on Deploy button in the dialog box.

  • After deploying it will open the mobile window with our added controls.

    16.gif

Note: - Before running the application
  • Check properly Cradle.
  • Check Windows Mobile Device Center connected or not.
  • Check our service is running or not.

Contributors 

Kalyan Yerranagu
Indra Bushan Prasad


Similar Articles