Gentlemen developers! Tell me what "toolkit" to use to create a service that accepts json requests and sends responses. Option c node.js I know. However, I would like to get by with the .net platform only. I do not need pieces of code ready, just indicate the direction. Requests, in particular, will come from VK Api Callback. thank
3 answers
If you want to have a sufficiently complete control over the processing of requests, I would recommend to postpone the trendy WCF , ASP.NET Web API and even more so ASP.NET MVC . Start with the old, kind, simple and reliable HttpModule + HttpHandler . Nothing superfluous, maximum transparency and atomicity.
- Understanding HTTP Handlers and HTTP Modules
- Walkthrough: Creating an HTTP Synchronous Handler
- Walkthrough. Creating and registering a custom HTTP module
All you need is to start processing requests, and not just json, but any:
Create an assembly:
using System.Web; public class HelloWorldHandler : IHttpHandler { public HelloWorldHandler() { } public void ProcessRequest(HttpContext context) { HttpRequest Request = context.Request; HttpResponse Response = context.Response; Response.Write("Hello from a synchronous custom HTTP handler."); } public bool IsReusable { get { return false; } } } And squeak next to it in <assemblyName>.web.config :
<configuration> <system.web> <httpHandlers> <add verb="*" path="*.sample" type="HelloWorldHandler"/> </httpHandlers> </system.web> </configuration> Everything. You can place this pair of files in the site folder on IIS and process any requests. When you get comfortable with this and there will be a clear understanding of how IIS works, you can be entertained with more advanced technologies.
- WCF
- ASP.NET Web Services (deprecated)
- ASP.NET MVC
- ASP.NET Web Api
For the "clean" work with JSON, the last option is best. WCF should be used if you want to provide soap-api as well as json-api. MVC should be used if in addition to api you also need a website.
Try NancyFX
public class MainModule : NancyModule { public MainModule() { Post["/callback"] = (_, ctx) => { var requestBody = SimpleJson.DeserializeObject<dynamic>(request); //Обработка запроса return Task.FromResult<dynamic>(requestBody.type == "confirmation" ? Response.AsText("701ab1ef") : Response.AsText("ok")); }; } }