2

Calling this async Web Method, I get a 500 error. Are there other ways to call a web method async?

Stacktrace:

Server Error in '/' Application.

Unknown web method SendMessage. Parameter name: methodName
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Unknown web method SendMessage. Parameter name: methodName

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[ArgumentException: Unknown web method SendMessage. Parameter name: methodName]
System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender, EventArgs eventArgs) +728343
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +92 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +165

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34248

ASPX Codebehind:

namespace Any.Contact
{
    using System.Threading.Tasks;
    using System.Web.Services;

    public partial class ContactFormPage : PublishingLayoutPage
    {    
        [WebMethod]
        public static async Task<bool> SendMessage(string topic, string message)
        {
            return await SendEmail(topic, message);
        }
    }
}

JavaScript:

$.ajax({
  type: "GET",
  url: window.location.href + "/SendMessage",
  data: {
    "topic": jQuery('.ddl-topic').val(),
    "message": jQuery('.tb-message').val()
  },
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  async: true,
  cache: false,
  success: function() {
    $('.alert.alert-success').toggleClass('hidden', 'show');
  },
  error: function() {
    $('.alert.alert-danger').toggleClass('hidden', 'show');
  }
});
Sebastian
  • 1,109
  • 4
  • 17
  • 33

1 Answers1

0

You don't need to use Task, it'll be called asynchronous from JS. It can be regular method that returns bool. Try:

namespace Any.Contact
{
    using System.Threading.Tasks;
    using System.Web.Services;

    public partial class ContactFormPage : PublishingLayoutPage
    {    
        [WebMethod]
        public static bool SendMessage(string topic, string message)
        {
            return SendEmail(topic, message);
        }
    }
}
Lokki
  • 372
  • 1
  • 6
  • 20