Calling Web Service From Windows Mobile Applicaion Using C#

Introduction:

In this article I'll discuss about how to call Web Service Application from windows mobile desktop application using C#.

Background:

It's not as you are thinking to create one web service application and call this web service from our windows mobile application. The same thing we are doing here but when you are adding web reference from C# windows form it will work but for windows mobile we have to follow some different technique. We will discuss every basic step of calling web service in windows mobile.

For execution of this application you need following software's installed on your system.

  1. .Net Framework
  2. .Net Compact Framework
  3. Windows Mobile SDK 5.0 and above.
  4. IIS 6.0 and above.
  5. Microsoft Active Sync 4.0 for Windows Xp and Windows Mobile Device center For Windows Vista or Windows 7.

Once you have all this software installed on your system we can develop web service application for windows mobile. Let's take a step-by-step look to develop Web services for Windows Mobile.

Step 1:

As our title we need to first to create our web service application which we will call from our windows mobile application.

Open Visual Studio -> New Web Site->Asp.Net Web Service->Name it as WinWebServie.

Step 2:

Write the methods with attribute [WebMethod] to perform our database or any other task. Let's create one sample webmethod as follows.

[WebMethod]
    public string GetGreetingsWebMessage(string _name)
    {
        return "Welcome "+_name+" To Windows Mobile Web Service Application.";
    }

This webmethod taking name as input and return the greetings message to user.

Step 3:

Build above created web service application and host it in IIS. For hosting web site under IIS check Hosting WebSite Under IIS. Here we have finished our web service application development part.

Step 4:

Now we have to create our windows mobile application which will call the web service.

Open Visual Studio->New Project->Smart Device->Pocket PC ->Name it as WinWebMobile. The result will be as one windows mobile application will be created. Design it with one textbox for accepting argument and one button to invoke the webmethod of web service application.

Step 5:

It's was only the GUI development part of our application now we can call the web service by adding web reference in our WinWebMobile application for that open solution explorer->right click on references->Add Web Reference will look like bellow and provide the URL of website hoted under IIS. But here you can't specify it as http://localhost/VirtualName/Service.asmx insted of this you have to specify Url as http://<SyatemName>/<virtualNameofWebsite>/Service.asmx then only the reference of webservice will be added to your application and our application can call the webmethods of webservice. If you provide localhost then it will return runtime error as no "No network connection" like this. Add the name to reference as WinWeb.

Web Service From Windows Mobile Application

Step 6:

Now we have added the reference to webservice application and the webmethods are accessible from our windows mobile application. Write the following code in button_click event to invoke the webmethod.

WinWeb.Service obj = new WinWebMobile.WinWeb.Service();
      if (textBox1.Text!="")
      {
         MessageBox.Show(obj.GetGreetingsWebMessage(textBox1.Text).ToString());
      }
      else
      {
         MessageBox.Show("Enter Name");
      }

Step 7:

Now we have done all the things and we are near to execute our application but it not as simple like press F5 and run our application for this we need to follow this step as bellow.

  1. Invoke pocket PC emulator image like below.

    Web Service From Windows Mobile Application
     
  2. It will open the pocket pc emulator like bellow and display the connection succeeded dialog box close this dialog box after scceeded connection.

    Web Service From Windows Mobile Application
     
  3. Open Device emulator manager from tools menu which will look like below.

    Web Service From Windows Mobile Application
     
  4. After clikcing on cradle option it will invoke Microsoft active sync for Xp if you have windows vista or windows 7 you need to install Windows Mobile Device Center.
     
  5. Here I'm using Xp so I've Microsoft active sync which will look like bellow with the confirmation message that connected.

    Web Service From Windows Mobile Application

    After this close this Microsoft ActiveSync.
     
  6. Now just click on Debug button and see the result in Pocket Pc Device like below.

    Web Service From Windows Mobile Application

Conclusion:

In this way we can call the web service from our windows mobile application.
 

Up Next
    Ebook Download
    View all
    Learn
    View all