2

I've built a simple C# app (.Net 4.0 + WPF) which can send and receive JSON messages via TCP sockets.

As a next step, it should be possible that JavaScript apps on websites and PHP scripts can send and receive JSON messages to/from my app. Is that possible?

Since JS/PHP will use stateless HTTP connections, how should a request to my app work, for example, should the JS/PHP apps send a JSON message to my app and my app response (HTTP response) with a JSON message? Is that even possible? And should I use GET or POST method to send the JSON messages to/from my app?

Hope my questions do not cause too much confusion ;-) I but I appreciate every tip, clarification or feedback you can give me.

Mike

4
  • Why would a WPF (fat-client) app of yours act as a host for JSON messages? That does not sound like a very robust server solution. Commented Jul 5, 2011 at 18:19
  • @Kirk Woll It's a peer-to-peer app, so there will be no central host/server Commented Jul 5, 2011 at 18:23
  • Ah, so you expect all your users to poke a hole in their firewall? Commented Jul 5, 2011 at 18:24
  • Every p2p-network has this problem. If only every 20th client will open the door, I think it should be enough (but I have no experience). I mean, hey, if you open the JSON port you will earn the "P2P King Badge" ;-) Commented Jul 5, 2011 at 18:46

1 Answer 1

3

You can accomplish this via a .NET web service using special JSON directives on the web method, e.g.

[ScriptMethod(UseHttpGet = true, ResponseFormat=ResponseFormat.Json)]
public string DoSomething(string param1, int param2) 
{
   // Do Something
}

When the ResponseFormat.Json property is specified, the data returned will be serialized into the appropriate JSON format. Also note, in order to recieve a true JSON response, you'll need to set your content-type to "application/json" from the requesting application. Otherwise, the method will attempt to wrap the response in XML.

Also, I am enabling a HttpGet on this method so that you can post via a query string to the method, e.g.

http://www.example.com/service.asmx?param1='Hello'&param2=1;
Sign up to request clarification or add additional context in comments.

4 Comments

Wow, thanks for your quick answer! I've two questions in regard to your example: 1. Since my app is a "normal" WPF app, is it a problem if it has only an IP/Port, for example: http: //156.25.365.25:4500/service.asmx?param1='Hello'&param2=1;[/code] ?
@Mike, that will not cause a problem. (though I assume you have accounted for your users' firewalls?)
Second question: Since you are using "UseHttpGet = true", does this mean, the JS/PHP apps have to send JSON messages to my app always with the GET method?
@George Since I want to host the JSON interface (web service) in a WPF app, it seems that I'm not able to use .asmx "The traditional ASMX Web services were hosted only on Microsoft Internet Information Services (IIS)." msdn.microsoft.com/en-us/library/bb332338.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.