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

Sinch SMS get stuck after sending an SMS in an ASP.net web application


I've been trying to integrate Sinch SMS into my ASP.net Web app using MVC 4, so far no luck, nothing works. I literally copy the code from their tutorial page on this link , however it does work when I create a c# console application and also with PHP.

To me this Nuget package ( Sinch.ServerSDK) has some problems working with ASP.NET c#, both projects are new without any other refereces to other package but only the C# console app works.

Any idea about this?.

Update 1:

this is my c# console application project and it works

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sinch.ServerSdk;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            sendSMS().GetAwaiter().GetResult();
        }
        private static async Task sendSMS()
        {

            var smsApi = SinchFactory.CreateApiFactory("mykey", "mysecretkey").CreateSmsApi();

            var sendSmsResponse = await smsApi.Sms("+1mynumber", "Hello world. Sinch SMS here.").Send();

        }
    }
}

this is my asp.net mvc web app and it doesn't work

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sinch.ServerSdk;
using Sinch.ServerSdk.Messaging;
using Sinch.WebApiClient;
using System.Threading.Tasks;

namespace WebApplication3.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {

            sendSMS().GetAwaiter().GetResult();

            return View();
        }

        private async Task sendSMS() {                        
            var smsApi = SinchFactory.CreateApiFactory("mykey", "mysecretkey").CreateSmsApi();
            var sendSmsResponse = await smsApi.Sms("+1mynumber", "Hello world.  Sinch SMS here.").Send(); 
        }

    }
}

My question is , why it works with the COnsole application and not with my Web Applicaiton.

Instead of down voting my question , let me know why, I think it will be more helpful.

Thanks


Solution

  • You can use await in MVC, you just need to set the action to Async

    public async Task<ActionResult> Index()
    

    If you dont want to do that you can do smsApi.Sms("+1mynumber", "Hello world. Sinch SMS here.").Send().Result();