Search code examples
c#asp.netasp.net-webhooks

How to make a "webhook"?


I need the "WinForm" application for correspondence in viber.
 "Webhook" is planned to receive data (events) from viber, then the data will be used in the application "WinForm".

I did:

  1. created the project "ASP.NET Web Application (.NET Framework)";
  2. chose a template - "Empty" + "MVC" + "API";
  3. added controller "Controller MVC 5 - empty". Controller name "HookController";
  4. I run the application "Postman";
  5. "Postman". I set the request "POST";
  6. "Postman". I set the link http://localhost:44836/Hook;
  7. "Postman". Click "SEND";
  8. The result, see the picture "- = RESULT = -";

If I understand the theory correctly, then after performing the "Postman" action. I click "SEND", the ViberProcess (HttpContext context) method should be executed in the HookController.cs controller and the code should stop at the breakpoint.
This is not happening.

Documentation Viber REST API - link

Question.
How to make a Webhook?

Code HookController.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

using System.Web;
using System.Web.Mvc;

//
using System.Runtime.Remoting.Contexts;

namespace WebAppl1.Controllers
{
    public class HookController : Controller
    {
        // GET: Hook
        //public ActionResult Index()
        //{
        //    return View();
        //}

         [HttpPost]
        // [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public void ViberProcess(HttpContext context)
        {
            try
            {
                Stream s = context.Request.InputStream;
                // Stream s = Context.Request.InputStream;
                // or  Stream s  =  HttpContext.Current.Request.InputStream;
                s.Position = 0;
                StreamReader reader = new StreamReader(s);
                string jsonText = reader.ReadToEnd();

                // Other code that converts json text to classes
            }
            catch (Exception e)
            {
                // .....
            }
        }

    }

}

enter image description here

7. "Postman". Click "SEND"; enter image description here

8. The result, see the picture "- = RESULT = -"; enter image description here

Server error in application '/'. Could not find this resource. Description: HTTP 404. The resource (or one of its dependencies) may have been deleted, received a different name, or may be temporarily unavailable. Review the following URL and verify that it is correct. Requested URL: / Hook Version Information: Microsoft .NET Framework Version 4.0.30319; ASP.NET version: 4.7.3062.0

Update_1
I use the link http://localhost:44836/api/Hook
The code does not stop at breakpoint.
Result:
{
"Message": "Could not find the HTTP resource corresponding to the request URI \" http://localhost:44836/api/Hook\ ".", "MessageDetail": "Could not find the type corresponding to the controller \" Hook \ " . " }

I use the link http://localhost:44836/Hook/ViberProcess
The code does not stop at breakpoint.
Result
Server error in application '/'. For this object, no parameterless constructors are defined. Description: An unhandled exception occurred during the execution of the current web request. Examine the stack trace for more information about this error and the code snippet that caused it. Exception Details: System.MissingMethodException: No parameter-less constructors are defined for this object. Source Error: An unhandled exception occurred during the execution of the current web request. Information on the origin and location of the exception can be obtained using the following exception stack trace.

enter image description here


Solution

  • Just remove the HttpContext context in your ViberProcess action.

    So, the method will become

    public IActionResult ViberProcess()
    {
        Stream s = HttpContext.Current.Request.InputStream;
    
        //... continue your code from here.
    }
    

    The reason behind this is, You have mention HttpContext context as an Argument of ViberProcess but while you are sending request it will search with the Exact schema.

    So, in your request, you can not pass the HttpContext from anywhere. So, this request will never be found.

    Here is the screenshot: Code

    Try this an let me know if you still have an issue.