Share via


Developing a Managed C# Server Application Using Response Queue

Other versions of this page are also available for the following:

  • Windows Embedded CE 6.0 R3

8/27/2008

This section discusses the development of a managed web service on top of the Local Server Framework. The following service uses response queue to send responses back to the client requests. The responses added to the queue will be sent by the server in the order in which they were added to the queue. The response added to the default queue will be sent when the server exhausts its list of responses to send.

To create a managed (C#) server application using response queue

  1. Create a directory for your application.

  2. Create the sources file and copy over the makefile in the directory.

  3. Update the sources file to include the following managed reference:

    $(_QAOAKFILES)\Microsoft.MobileDevices.FakeServer.dll
    
  4. Include the following namespace in your main file:

    using Microsoft.MobileDevices.FakeServer.Core;
    
  5. Add the following class:

    using System;
    using System.Diagnostics;
    using System.Text;
    using System.Xml;
    using System.Threading;
    using System.Runtime.InteropServices;
    
    using Microsoft.MobileDevices.FakeServer.Core;
    
    //
    // Sample app that uses Queue and DefaultResponse list of the FakeServer
    //
    public class QueueApp
    {
        static void Main(string[] args)
        {
            UTF8Encoding utf8Enc = new UTF8Encoding();
    
            // Sample responses
            string responseStr1 = "HTTP/1.1 200 OK\r\nContent-Length: 25\r\n\r\nFirst Response from queue";
            string responseStr2 = "HTTP/1.1 200 OK\r\nContent-Length: 26\r\n\r\nSecond Response from queue";
            string responseStrDef = "HTTP/1.1 200 OK\r\nContent-Length: 16\r\n\r\nDEFAULT RESPONSE";
    
            //
            // Initialize and Start the FakeServer
            //
            FakeServer.Initialize();
            FakeServer.Start();
    
            //
            // Add a couple of responses to the FakeServer's Response queue
            //
            FakeServer.ResponseQueue.Enqueue(new FakeResponse(utf8Enc.GetBytes(responseStr1.ToCharArray())));
            FakeServer.ResponseQueue.Enqueue(new FakeResponse(utf8Enc.GetBytes(responseStr2.ToCharArray())));
    
            //
            // Add a response as the Default response that will be sent back by the FakeServer once the queue is empty
            //
            FakeServer.DefaultResponses.Add(new FakeResponse(utf8Enc.GetBytes(responseStrDef.ToCharArray())));
    
            Thread.Sleep(30000);
    
            //
            // Stop the FakeServer
            //
            FakeServer.Stop();
        }
    }
    
  6. Build and run your executable.

  7. Open up Internet Explorer Mobile and type https://localhost. Keep refreshing the page and the server will respond with the responses you added to the response queue. Once the response queue is exhausted the default response will be sent down for all subsequent requests.

See Also

Other Resources

Developing Server Applications with the Local Server Framework