WCF JSON or HTTPHandler Service

I am implementing some AJAX that requires JSON to be returned from the server (in .NET4), but I'm a little confused in pro and con using either a custom HTTPHandler or a WCF service.

Can anyone shed some light that is best suited for a medium to large scale application that is almost entirely based on AJAX?

UPDATE

In my current use case, I would have to provide a custom object to the JQuery plugin, so I would have to choose between replicating these objects in .NET and then using WCF to serialize them, which seems like a little extra unnecessary step in this case. Therefore, I will go with the string builder / HTTPHandler model to interact with JQuery, but I will keep in mind the WCF method if I need to represent .NET objects on the client.

Thanks for the answer.

+4
source share
1 answer

Can you define a JSON messaging structure or are you expecting some kind of arbitrary structure?

If you can define the structure of JSON messages, you can use WCF and its assembly in serializing / deserializing JSON messages for .NET types. Requests will be directly redirected to operations, and you will encode them like any other .NET method, without worrying about JSON or serialization =>

  • Get parameters from an operation represented as .NET types / classes
  • Process them
  • Returns a .NET object as a result

WCF will handle everything related to routing request, deserialization and serialization operations.

If you cannot determine the structure of JSON messages, you cannot simply deserialize them into a .NET type. In this case, you can go with HttpHandler and somehow parse the JSON.

The difference is that WCF will do a lot of work for you, but you have to do it your own way. In HttpHandler you will have direct control over the request and response, but you will handle all the complexity yourself.

+1
source

Source: https://habr.com/ru/post/1342438/


All Articles