Is there a .NET solution for sending/receiving JSON other than IIS?
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 开发者_开发问答information to the console
Performance is not a problem as 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 application and 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 + kSOAP 2 (on the phone) causes more trouble than it solves, I really want to try a lightweight standalone JSON solution.
Maybe I'm incorrect, but I suppose you could use WCF hosted in a console application.
The Kayak project does pretty much exactly what you want to do. It's 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. Finally, it includes a built-in server that you can easily throw into a console application.
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.
精彩评论