3

I'm executing code based on a Twilio example for receiving and responding to SMS messages found here: https://www.twilio.com/docs/sms/tutorials/how-to-receive-and-reply-csharp

The only difference is that I am using .NET MVC Core and not the .NET MVC Framework as provided in the sample. The code I'm having an issue with is:

    [HttpPost]
    public TwiMLResult Index(SmsRequest incomingrequest)
    {
        var messagingResponse = new MessagingResponse();
        messagingResponse.Message("The Robots are coming! Head for the Hills!");
        return TwiML(messagingResponse);
    }

If I encapsulate the method inside a the Controller's Class which by default inherits from the ControllerBase class; the TwiML is identified as a base class and not a method which causes an error.

public class SmsResponseController : ControllerBase
{
    [HttpPost]
    public TwiMLResult Index(SmsRequest incomingrequest)
    {
        var messagingResponse = new MessagingResponse();
        messagingResponse.Message("The Robots are coming! Head for the Hills!");
        return TwiML(messagingResponse); -- <-- Error: Non-invokable member TwiML cannot be used like a method
    }

}

So explicitly following the example, changing the inheritance from the ControllerBase to the TwilioController will solve this specific problem and TwilML is recognized as method with 3 potential overloads.

public class SmsResponseController : TwilioController
{
    [HttpPost]
    public TwiMLResult Index(SmsRequest incomingrequest)
    {
        var messagingResponse = new MessagingResponse();
        messagingResponse.Message("The Robots are coming! Head for the Hills!");
        return TwiML(messagingResponse); -- Method recognized
    }
}

But attempting to execute the program will now throw error during main execution in the Program.cs The error is

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

And unfortunately non of the posts I've found so far seem to be applicable to this scenario nor do they solve the problem.

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

I can only guess that some portion of code somewhere within the Core Framework is reliant on the existence of the ControllerBase class.

But how can I get the sample to execute without error and dependency on the ControllerBase?

Seems a catch 22 scenario right now.

Mark
  • 1,667
  • 2
  • 24
  • 51
  • Class exists in other namespace so make sure you're using Microsoft.AspNetCore.Mvc and not the .NET framework version. – Mikhail Zhuikov Feb 26 '20 at 13:42
  • @Fox It's a .NET Core project so the framework version is core. But I'd already considered that before posting – Mark Feb 26 '20 at 13:52
  • I fell into this exact trap. Every that happened to you, happened to me. I have fed back to them. The root cause is naming methods the same as classes, the conspiring cause is not prefixing `this.` which I guess is because Twilio aren't a .NET shop. I will feedback again and cite this SO post. It's almost 2023 now. – Luke Puplett Oct 30 '22 at 11:55

1 Answers1

5

There is another package you can use for ASP.NET Core: https://www.nuget.org/packages/Twilio.AspNet.Core/

So, the code you want would be something like this:

using Twilio.AspNet.Common;
using Twilio.AspNet.Core;
using Twilio.TwiML;

namespace TwilioReceive.Controllers
{
    public class SmsController : TwilioController
    {
        public TwiMLResult Index(SmsRequest incomingMessage)
        {
            var messagingResponse = new MessagingResponse();
            messagingResponse.Message("The copy cat says: " +
                                      incomingMessage.Body);

            return TwiML(messagingResponse);
        }
    }
}

For more docs on using .NET Core, you can check out this document: https://www.twilio.com/docs/sms/quickstart/csharp-dotnet-core

dprothero
  • 2,683
  • 2
  • 21
  • 28
  • Thanks, that worked perfectly! I had opened a support ticket with support and they were looking into it so I'll just pass along the info to them as well. I still don't understand why the other Core version wouldn't work but it's of no concern now – Mark Feb 27 '20 at 10:55
  • This answer helped me. I have one other detail that wasn't immediately obvious until I looked at your code sample. I had to change my .Net Core controller method's return type from ActionResult to TwiMLResult. Didn't have to do that in a regular (non-Core) Asp.Net MVC project. – kenswdev Jan 17 '21 at 20:31