Twilio webhook integration into web api fails

wavemaster 311 Reputation points
2021-06-13T15:39:15.227+00:00

I have a working Blazor Net5 Web API in conjunction with a Blazor Server project that is working.

Would like to add a controller to support a Twilio webhook, as per this example.

The currently working controllers follow this pattern:

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
//using BtClassLibrary;
using BtApiEf5.Model;
using BtApiEf5.Model.Custom;


namespace BtApiEf5.Controllers
{
    [ApiController]
    [Route("api/[controller]")]

    public class OwnerController : ControllerBase
    {
        private readonly BTContext context;
        public OwnerController(BTContext context)
        {
            this.context = context;
        }

        [Route("All")]
        [HttpGet]
        public async Task<ActionResult<List<OwnerGrid>>> Get() => await context.Owners
            .Select(o => new OwnerGrid
            {
                OwnerId = o.OwnerId,
                ProviderId = o.EnteredByProviderId,
                Location = o.Location,
                OName = o.Oname,
                City = o.City,
                State = o.State,
                FromToOwner = o.FromToOwner,
                Email = o.Email,
                BillEmail = o.BillEmail,
                IsBarn = o.IsBarn,
                ClientCount = o.ClientOwnerBillTos.Count
            })
            .OrderBy(o => o.OName)
            .ToListAsync();

The project builds fine with Twilio controller added, however when I run it
there are two exceptions, the first one in Startup.cs

{"Could not load type 'System.Web.HttpContextBase' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.":"System.Web.HttpContextBase"} System.TypeLoadException

And the second one in Program.cs

{"Could not load type 'System.Web.HttpContextBase' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.":"System.Web.HttpContextBase"} System.TypeLoadException

My Twilio controller looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using Twilio.AspNet.Common;
using Twilio.AspNet.Mvc;
using Twilio.TwiML;
using BtApiEf5.Model;
using BtApiEf5.Model.Custom;

namespace BtApiEf5.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class TwController : TwilioController
{
[Route("Send")]
[HttpPost]
public TwiMLResult Index(SmsRequest request)
{
var response = new MessagingResponse();
response.Message("Hello World");
return TwiML(response);
}
}
}

I have investigated the exceptions and there is nothing that helps me to a corrective course of action.

When I exclude the TwilioController from the project everything start working again.

What is the way forward with this?

Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2021-06-13T16:37:15.227+00:00

    The Twilio dll you are using is for 4.* net, not core. . It appears their nuget package is only up to core 2.1 and for core you just implement a rest api

    webhook-request

    0 comments No comments

  2. wavemaster 311 Reputation points
    2021-06-13T17:50:40.993+00:00

    Yes, I now have a yellow exclamation park for the twilio.aspnet.mvc package.

    Can you elaborate on what you mean by "just implement a rest api"? Are you saying not use any of the Twilio Libraries?


  3. Duane Arnold 3,216 Reputation points
    2021-06-13T18:37:05.133+00:00

    Maybe, you need to use a .NET Standard classlib project, a proxy, that is going to use this Twilio webhook and the dependent DLL it needs System.Web.HttpContextBase' from assembly 'System.Web, Version=4.0.0.0.

    You can make the call from the Core 5 solution. Maybe, it will work for you.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.