My own small .NET Micro Framework Web Server

Tags: .NET, .NET Micro Framework, AJAX, Ajax.NET, HTML, JavaScript, JSON

While developing my small Ajax.NET M! library for creating Web applications that run on the .NET Micro Framework I have started to write my own Web server. Some years ago I wrote a simple SMTP/POP server and using some code from there was a great help. The first version will only return static HTML pages e.g. for documentation or help files. As there is no file system on those devices I use the embedded resource strings to return the content.

MS.Micro.WebServer srv = new MS.Micro.WebServer(80);
srv.Assembly = "MFConsoleApplication1";
srv.StartPage = "default.aspx";   // well, it is a fake, of course
srv.Start()

To find the correct embedded resource string the name of the resources have to follow a very simple syntax. If you want to open the file http://mydevice/deviceinfo.htm you have to use deviceinfo as ID for the resource string. The Web server then tries to get that from the specified assembly. If there is no such resource string it will generate an http 404 error.

One step forward is to allow several types of place holders like Utility.GetLastBootTime() or some other system info. You can simple add those place holders in the resource string and the Web server will replace it with the current value.

image

The next step is to use HTML forms to submit any data to the Web server like configuration values. A typical feedback form is already working. The file upload element is not yet finished, but the data is already available as a byte array.

image

And the last step is to use JavaScript and AJAX to update or requests data on the device. As there are a lot of devices running on a slow clock it makes sense to not refresh the whole page. Ajax.NET Professional marks AJAX methods with an attribute called AjaxPro.AjaxMethod. All those methods are available by a JavaScript proxy that is generated on the fly.

Look how your C# source code will look like when you want to use a AJAX method that will return the current date of the device:

public class MyAjaxMethods
{
    [AjaxPro.AjaxMethod]
    public static DateTime GetDeviceTime()
    {
        return DateTime.Now;
    }
}

Well, on the client you are working the same way as you may know from Ajax.NET Professional. Of course, you are able to use any other JavaScript library or the XMLHttpRequest object itself to access the JSON data served form the device.

Here is an example how to write it your own:

function test() {
    xhr = new XMLHttpRequest();
    xhr.open("POST", "/ajaxpro/MFCon....AjaxMethods/GetDeviceTime", true);
    xhr.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
    xhr.onreadystatechange = function() {
        if(xhr.readyState != 4) return;
        if(xhr.status == 200) {
            alert(xhr.responseText);
        }
    };
    xhr.send("");
}

I hope that I can publish the source code of the Web server at Codeplex in the next days.

5 Comments

  • Sangh said

    Hi Herr Schwarz, I am delighted to see your blog, after hours of searching for something similar on the net. I am trying to use the SocketServer example of the Digi JumpStart - Connect ME Kit, where a simple static HTML site is being sent to the socket: // Create a socket, bind it to the server's port and listen for client connections Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Wait for a client to connect Socket clientSocket = server.Accept(); using (m_clientSocket) { // Return a static HTML page back to the client String s = "Sample Website!"; m_clientSocket.Send(Encoding.UTF8.GetBytes(s)); } My problem however is, that I donot wish to send static HTML text like this, instead, a complex .ASP website. In my efforts to do this, the Einschränkungen of .NET Micro Framework (hardly any verfügbare classes/functions)is posing to be a huge problem. It would be extremely nice, if you could help me, in case you have done something similar in this direction. Thanks a lot!

  • Michael Schwarz said

    Hi Sangh, well, I have already finished a good embedded Web server that will be able to read any static html/image files from i.e. SD disk or embedded resources. Another option there is to use place holders to put it any dynamic content. Follow my blog in the next days as I will then upload new version of my source code. The current one is only dummy code. Michael

  • Sangh said

    Hi Michael, Thats just great!! I am soo happy and thankful for your prompt reply... I really hadn't expected it so soon... I shall follow your blog for sure.. :) Thanks a million!! Bis bald, Sangh

  • William Harrington said

    Hi Michael, I was just wondering when you were going to release your source code for the web server, It looks like a really interesting application of the .NET Micro framework. Regards, William Harrington william@usee.com.au

  • Gerben Luimes said

    Hello Michael, I bought a Digi Connect Me Development Kit and am very curious about your web server for the .NET micro framework. I'm looking at your blog for a couple of weeks and would like to know when you're planning to release the first source code. As William said, it looks like a very interesting application on the .NET Micro framework. Regards, Gerben Luimes gerben@nr36.nl

Comments have been disabled for this content.