示例代码项目无法编译

时间:2018-08-16 03:03:55

标签: twilio

我正在尝试编译此project-“ AirTNG应用程序:第1部分-使用Twilio进行工作流自动化-ASP.NET MVC”。

我在Notifier.cs第40行中看到一个_client.SendMessage。 但是它没有建立,因为TwilioRestClient类中似乎没有任何方法可以解析为“ SendMessage”。

[使用Twilio程序集的15.1.0版。]

以下是通知程序的代码:

using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Services.Description;
using AirTNG.Web.Domain.Twilio;
using AirTNG.Web.Models;
using AirTNG.Web.Models.Repository;
using Twilio;
using Twilio.Clients;


namespace AirTNG.Web.Domain.Reservations
{
    public interface INotifier
    {
        Task<Message> SendNotificationAsync(Reservation reservation);
    }

    public class Notifier : INotifier
    {
        private readonly TwilioRestClient _client;
        private readonly IReservationsRepository _repository;

        public Notifier() : this(
            new TwilioRestClient(Credentials.AccountSid, Credentials.AuthToken),
            new ReservationsRepository()) { }

        public Notifier(TwilioRestClient client, IReservationsRepository repository)
        {
            _client = client;
            _repository = repository;
        }

        public async Task<Message> SendNotificationAsync(Reservation reservation)
        {
            var pendingReservations = await _repository.FindPendingReservationsAsync();
            if (pendingReservations.Count() > 1) return null;

            var notification = BuildNotification(reservation);
            return _client.SendMessage(notification.From, notification.To, notification.Messsage);
        }

        private static Notification BuildNotification(Reservation reservation)
        {
            var message = new StringBuilder();
            message.AppendFormat("You have a new reservation request from {0} for {1}:{2}",
                reservation.Name,
                reservation.VacationProperty.Description,
                Environment.NewLine);
            message.AppendFormat("{0}{1}",
                reservation.Message,
                Environment.NewLine);
            message.Append("Reply [accept] or [reject]");

            return new Notification
            {
                From = PhoneNumbers.Twilio,
                To = reservation.PhoneNumber,
                Messsage = message.ToString()
            };
        }
    }
}

下面是我的packages.config的内容,就像对所有软件包的最新稳定版进行更新之后的样子。

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Antlr" version="3.5.0.2" targetFramework="net451" />
  <package id="bootstrap" version="4.1.3" targetFramework="net471" />
  <package id="EntityFramework" version="6.2.0" targetFramework="net471" />
  <package id="FontAwesome" version="4.7.0" targetFramework="net471" />
  <package id="jQuery" version="3.3.1" targetFramework="net471" />
  <package id="jQuery.Validation" version="1.17.0" targetFramework="net471" />
  <package id="Microsoft.AspNet.Identity.Core" version="2.2.2" targetFramework="net471" />
  <package id="Microsoft.AspNet.Identity.EntityFramework" version="2.2.2" targetFramework="net471" />
  <package id="Microsoft.AspNet.Identity.Owin" version="2.2.2" targetFramework="net471" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.6" targetFramework="net471" />
  <package id="Microsoft.AspNet.Razor" version="3.2.6" targetFramework="net471" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.6" targetFramework="net471" />
  <package id="Microsoft.IdentityModel.JsonWebTokens" version="5.2.4" targetFramework="net471" />
  <package id="Microsoft.IdentityModel.Logging" version="5.2.4" targetFramework="net471" />
  <package id="Microsoft.IdentityModel.Tokens" version="5.2.4" targetFramework="net471" />
  <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.10" targetFramework="net471" />
  <package id="Microsoft.Owin" version="4.0.0" targetFramework="net471" />
  <package id="Microsoft.Owin.Host.SystemWeb" version="4.0.0" targetFramework="net471" />
  <package id="Microsoft.Owin.Security" version="4.0.0" targetFramework="net471" />
  <package id="Microsoft.Owin.Security.Cookies" version="4.0.0" targetFramework="net471" />
  <package id="Microsoft.Owin.Security.Facebook" version="4.0.0" targetFramework="net471" />
  <package id="Microsoft.Owin.Security.Google" version="4.0.0" targetFramework="net471" />
  <package id="Microsoft.Owin.Security.MicrosoftAccount" version="4.0.0" targetFramework="net471" />
  <package id="Microsoft.Owin.Security.OAuth" version="4.0.0" targetFramework="net471" />
  <package id="Microsoft.Owin.Security.Twitter" version="4.0.0" targetFramework="net471" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net451" />
  <package id="Modernizr" version="2.8.3" targetFramework="net451" />
  <package id="Newtonsoft.Json" version="11.0.2" targetFramework="net471" />
  <package id="Owin" version="1.0" targetFramework="net451" />
  <package id="popper.js" version="1.14.3" targetFramework="net471" />
  <package id="Portable.BouncyCastle" version="1.8.2" targetFramework="net471" />
  <package id="Portable.JWT" version="1.0.5" targetFramework="net451" />
  <package id="Respond" version="1.4.2" targetFramework="net451" />
  <package id="RestSharp" version="106.3.1" targetFramework="net471" />
  <package id="System.IdentityModel.Tokens.Jwt" version="5.2.4" targetFramework="net471" />
  <package id="Twilio" version="5.15.1" targetFramework="net471" />
  <package id="Twilio.AspNet.Common" version="5.8.3" targetFramework="net471" />
  <package id="Twilio.AspNet.Mvc" version="5.9.7" targetFramework="net471" />
  <package id="WebGrease" version="1.6.0" targetFramework="net451" />
</packages>

before and after compare

2 个答案:

答案 0 :(得分:0)

您应该使用Twilio最新的nuget软件包Twilio.AspNet.Mvc版本5.9.7。 因为您使用的版本具有对较早版本的ASP.NET MVC的依赖关系,但是其余软件包的依赖项取决于.NET的较新版本,例如Newtonsoft.Json 11.0.x。

请改用以下版本的5.9.7:https://www.nuget.org/packages/Twilio.AspNet.Mvc/5.9.7

并正确链接依赖项。

答案 1 :(得分:0)

原始示例使用API​​版本4编写。 为了使其与版本5兼容,我进行了一些更改。

更新通知程序代码:

using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AirTNG.Web.Domain.Twilio;
using AirTNG.Web.Models;
using AirTNG.Web.Models.Repository;
using Twilio.Clients;
using Twilio.Rest.Api.V2010.Account;

namespace AirTNG.Web.Domain.Reservations
{
    public interface INotifier
    {
        Task<MessageResource> SendNotificationAsync(Reservation reservation);
    }

    public class Notifier : INotifier
    {
        private readonly TwilioRestClient _client;
        private readonly IReservationsRepository _repository;

        public Notifier() : this(
            new TwilioRestClient(Credentials.AccountSid, Credentials.AuthToken),
            new ReservationsRepository())
        {
        }

        public Notifier(TwilioRestClient client, IReservationsRepository repository)
        {
            _client = client;
            _repository = repository;
        }

        public async Task<MessageResource> SendNotificationAsync(Reservation reservation)
        {
            var pendingReservations = await _repository.FindPendingReservationsAsync();
            if (pendingReservations.Count() > 1) return null;

            var notification = BuildNotification(reservation);

            var message = await MessageResource.CreateAsync(
                body: notification.Messsage,
                from: notification.From,
                to: notification.To,
                client: _client
            );

            return message;
        }

        private static Notification BuildNotification(Reservation reservation)
        {
            var message = new StringBuilder();
            message.AppendFormat("You have a new reservation request from {0} for {1}:{2}",
                reservation.Name,
                reservation.VacationProperty.Description,
                Environment.NewLine);
            message.AppendFormat("{0}{1}",
                reservation.Message,
                Environment.NewLine);
            message.Append("Reply [accept] or [reject]");

            return new Notification
            {
                From = PhoneNumbers.Twilio,
                To = reservation.PhoneNumber,
                Messsage = message.ToString()
            };
        }
    }
}