Compact 2013 Ebook

17.9 Web Service Client
Created by djones on 7/2/2013 2:16:38 AM

Managed Code Access to a Web Service

The task of an embedded device is of to "run forever" without intervention performing a specific task as an outcrop of a service. For example it may have sensors that periodically return climatic data or return activity data when events occur in its realm. To demonstrate that functionality the GetTime application will be modified to request the formatted DateTime string from a remote service. In particular a Web Service  will be used to service the DateTime request from the Compact 2013 client.  Whilst it is the Compact 2013 system that is data consumer, it would not be hard for the web method to modified so that it consumed data submitted to it by the Compact 2013 device.

Issues:

  • Web referencing not supported in Visual Designer
  • The web reference URL can't use localhost.

(A) Create the Web Service

Create a web service to generate the Date/Time string as an XML string. The web service will be written in C# and may reside on the development system or elsewhere except the target Compact 2013 system. The simplest option is to implement it on a system that has IIS implemented, which could even be Azure!. If no IIS web server is available, use the Visual Studio (Virtual) Web Server but you will need something like Fiddler to do port redirection because the Visual Studio server can only be used in the local context. Note that any emulator or virtual machine does not run in the local context of its host.

  1. Create the web service in a new solution in a separate instance of Visual Studio if using the OS/App development system, or on a separate system:
    Create a new project
    Menu, File—>New—> Project—>C#
    Set the .NET version to 3.5 
    —>ASP.NET Web Service
  2. Call it GetTimeSvc and generate the project
  3. If using the Visual Studio Web Server, in the Property Pages, set the IP Port to a fixed a port
  4. In Solution Explorer, rename the Service1.asmx page to TimeService.asmx
  5. Rename the Service1 class to TimeService
    Right-click on Service1 in the code –>Refactor—>Rename
    Change the name and accept the change.
  6. In Solution Explorer, set that page as the Start Page for the web service.
  7. Note that the functions exposed as web methods have the [WebMethod] declaration proceeding them.
    In the generated code is a HelloWorld method.
  8. Test run the Web Service:
    Right-click on the project in Solution explorer—>Set as Startup Project
    F5 to run it.
  9. When the "Debugging Not Enabled" dialog appears, enable debugging for the site.
  10. Select the HelloWorld method
  11. On the next page invoke it:

    HelloWorld Web Method Output
    1. xml version="1.0" encoding="utf-8" ?>
    2. <string xmlns="http://tempuri.org/">Hello Worldstring>

    Listing 17. The output of HelloWorld web method
  12. Add the following web method:

    GetTime Web Method
    1. public string GetTime()
    2. {
    3.     DateTime localTime = DateTime.Now;
    4.     string dateTimeStr = string.Format(
    5.         "Time {0}:{1}:{2} Date {3:2}//{4:2}//{5:4}",
    6.         localTime.Hour, localTime.Minute, localTime.Second,
    7.         localTime.Day, localTime.Month, localTime.Year);
    8.  
    9.     return dateTimeStr;
    10. }

    Listing 17. The GetTime web method

    Note that for simplicity a StringBuilder is not passed to the web method. For simplicity, the web method returns a string rather than a using pass by reference functionality.
  13. Test run the web service again and exercise the GetTime web method:

    GetTime Web Method Output
    1. xml version="1.0" encoding="utf-8" ?>
    2. <string xmlns="http://tempuri.org/">Time  23:52:22 Date  2//2//4string>

    Listing 17. The output of GetTime web method
  14. The absolute URL for the web service is required, Restart the web service and copy the URL in the web browser.
  15. Replace localhost in the url with the web server machine name or its IP Address.
  16. If using the Visual Studio Server replace the port with the redirected port.
    eg http://192.168.2.105:8000/TimeService/TimeWebService.asmx
  17. Test the new URL in the browser.
  18. Finally test the URL on another system:
    It is important that the URL for the web service work on a "non-local" system as the target system is "not local" to the web service.
    Non-local in this context can mean on the same subnet but not localhost.

(B) Web Reference

Normally a web reference is added to a .NET app in the project context in Solution Explorer. Once added to a project, the web service is instantiated as an object instance of the web service class, with the required web method being a method of that object.

Unfortunately the addition of web methods in the Visual Designer this has not been implemented for .NET CF 3.9.

It has though been implemented with Compact 7-Smart Device projects. The solution is to generate the app project with the web reference in that context and either copy the binary to the Compact 2013 system or copy the required web reference code and resources from the Compact 7 app project to the Compact 2013 project. The first method is simplest but debugging would need to resolved on the Compact 7 system. The second method is much more complex and can be considered "a hack". But once implemented it facilitates debugging and extensions in the Compact 2013 app development context.

  1. Develop the following in Visual Studio 2008 as a SmartDevice C# application, The target can be any SmartDevice such as Compact 7, the WinCE 500 SDK emulator or any of Windows Mobile Phone emulators
  2. Create a new C# SmartDevice project, call it WebSvcClient.
  3. Select the target, choose .NET CF 3.5 and select Device Application
  4. Recreate the C# MangedTime application from 17.5
  5. In the project context in Solution Explorer, select Web Reference.
    image
  6. Enter the web service URL (near top of dialog) from step 16 in part A.
    image
    ..and press Go the green icon).
  7. The service will show as well as a list of its methods:
    image
    ..Press [Add Reference]
  8. The Web Reference, WebReference will then show in Solution Explorer:
    image
  9. Right-click on it and select View in Object Browser.
    Expand WebReference in Object Browser to see the TimeService service,
    Copy that,
  10. In the ManagedTime form code, delete the contents of the TimeButton event handler (if it has already been embellished).
    Paste the TimeService reference and code the instantiation of it:
    eg ManagedTime.WebReference.TimeService ts = new ManagedTime.WebReference.TimeService( ) ;
  11. The ts object then can be used to action the GetTimed web method. In code Intellisense can give it to you. e.g.:
    image
  12. Extend the code to action the GetTime method as a string and copy it to textbox1eg:

    TimeButton Handler Web Method
    1. private void TimeButton_Click(object sender, EventArgs e)
    2. {
    3.     ManagedTime.WebReference.TimeService ts = new ManagedTime.WebReference.TimeService( ) ;
    4.     string timeStr = ts.GetTime();
    5.     textBox1.Text = timeStr;
    6. }

    Listing 17. TimeButton Event Handler consuming GetTime Web Method
  13. Test and run the app from this context,

(C) Test the VS 2008 Application on Compact 2013 System

Being Managed Code the  compiled Visual Studio 2008 version of the app should be compatible with Compact 2013 on a system with .NET Compact Framework.

  1. Copy the application .exe from section (B) to the Compact 2013 development system.
  2. Copy it to the release directory of the OS
    OR
    Build a CEComponentWiz project for it and rebuild the OS including it. Deploy the OS.
  3. Run the application using Target Control etc.

(D) Port the VS2008 app to Compact 2013

This activity is fraught with danger in that it requires directly modifying the application project file, .csproj. It is doable and does work though.

  1. IMPORTANT: Backup the ManageTime project file on the Compact 2013 development system:
    From the project, Project Context (in Solution Explorer) –>Open Folder in Windows Explorer
    Copy the .csproj file and paste it
    This will create "Copy of ManagedTime.csproj" or something like that
  2. Close this application project.
  3. Copy the Web References folder from the VS2008 project and paste it into the VS2012 version of the project.
  4. Merge the Web References in the VS2008 project into the VS2012 version of the project:
    • The project files have the .csproj extension.
    • Compare the Compact 2013 version of the project file with the VS 2008 version of the project file at a text or XML level:
    • You might do so by inspection using a copy of each in Notepad side-by-side or in XML editors.
      OR
    • Use a text file comparison application such as BeyondCompare or an XML file comparison application such as DiffDog.
    • Insert the required WebReference XML entries from the VS2008 project file into the Compact 2013 project file, in the corresponding locations.
    • This is considered in more detail in the next article: "17.9a Merge Web References"
  5. Reload the VS2012 version of the application and check that the WebReference show under Web References as in step 8. in section (B).
  6. Also check it in Object Browser as previous, for the GetTime web method.
  7. Replace the TimeButton event handler code as above, i.e. with:

    TimeButton Handler Web Method
    1. private void TimeButton_Click(object sender, EventArgs e)
    2. {
    3.     ManagedTime.WebReference.TimeService ts = new ManagedTime.WebReference.TimeService( ) ;
    4.     string timeStr = ts.GetTime();
    5.     textBox1.Text = timeStr;
    6. }

    Listing 17. TimeButton Event Handler consuming the GetTime web method in the Compact 2013 app
  8. If the web service isn't running start it.
  9. Build, deploy, test and debug the application.
  10. Build, test and debug the application on the Compact 2013 target.

Open-mouthed smile


NEXT: Merging the Web References from a VS2008 SmartDevice project into a Compact 2013 C# Project

print

Click here to provide feedback and input

  Comments

There is no comment.

Turkish porno izle video site in rokettubeporno izle