views:

80

answers:

3

What I basically need is a small console application that listens on port 80 and is capable of putting json objects around.

  • Receiving value types, objects and List<T> (or array) from a JSON client and converting them to .net classes
  • Sending value types, objects and List<T> to the client
  • Outputting some info to the console

Performance is not a problem I expect about 20 - 30 request per hour. I don't want the IIS or cassini web server as a requirement on the client side. Only my console app + dependencies.

I already tried servicestack.net which looks very promising and has an example for a console host. Howevery I din't manage to get JSON out of the console host (only xml).

Any ideas how to use servicestack.net or alternatives are welcome.

By the way: The client will be an Android phone and since my current approach IIS + WDSL + kosap2 (on the phone) causes more trouble than it solves so I really want to try a lightweight standalone JSON solution.

+3  A: 

Maybe I'm incorrect, but I suppose you could use WCF hosted on a console app.

Carles
And it's very easy. Search google for "JSON over WCF" and "standalone WCF hosting"
Sergej Andrejev
It looks like WCF Service hosting requires at least Windows Vista http://www.devx.com/VistaSpecialReport/Article/33831/1954 but asp.net can also be hosted inside a managed app. I found this great article http://www.west-wind.com/presentations/aspnetruntime/aspnetruntime.aspI will try that.
SchlaWiener
+1  A: 

You could use a HttpListener to hande Http requests in your application. You would have to handle the Json serialization yourself, but that may not be a problem?

Depending on which framework version you are using you could use either the built-in Json serialization support or you could use the Json.NET library to do this. In either case it should be easy to detect the requests and to return a Json response.

Rune Grimstad
+2  A: 

The Kayak project does pretty much exactly what you want to do. Its very lightweight and very powerful, check out some of the examples (taken directly from the project page):

public class PostAPI 
{
  [Path("/widgets")]
  public Widget[] GetWidgets() 
  {
    return Widget.GetAll();
  }

  [Verb("POST")]
  [Path("/widgets")]
  public void CreateWidget([RequestBody] Widget w) 
  {
    w.Created = DateTime.UtcNow;
    w.Create();
  }
}

public class Widget 
{
  public string Author;
  public string Text;
  public string Created;

  // (methods would be here...)

}

It can automagically serialize and deserialize between JSON objects and CLR Objects and accept routes as well as both POSTs and GETs. Finaly, it includes a built in server that you can easily throw into a console app.

kersny
Very nice project. A usage is as easy as adding a reference to the dll and decorating your classes (+ one line of code for starting the server) but one thing I don't like is that if my client call is not correct the http connection is just closed instead of returning an error string or outputting some information on the console which makes debugging challenging.
SchlaWiener
It does, by default, log all requests to the console along with the HTTP response sent and the time taken. I do see how the lack of a custom 404 or catch-all can cause problems.
kersny