C# 訊息發送功能(支援多種發送方式)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   使用   ar   for   

總體設計:

IMessageChannel介面:一個訊息的發送方式,即訊息的發送通道。有一個Process方法,此方法處理本訊息發送通道怎麼去發送訊息。接受一個IDictionary<string,object>參數,此參數提供發送的內容,是一個索引值對集合。希望訊息有多種發送方式。每種方式實現此介面即可。

IMessageChannelSelector: 訊息通道選取器,用於訊息通道管理器,便於管理器能正確的選擇出一個訊息發送的通道。

IMessageChannelManager:訊息通道管理器,管理各種訊息發送通道。

IMessageService: 訊息發送服務。也是訊息發送功能調用入口。

二。具體實現。

  /// <summary>    /// 訊息通道    /// </summary>    public interface IMessageChannel    {        /// <summary>        /// 通道對資訊的處理方法        /// </summary>        /// <param name="parameters">參數列表</param>        void Process(IDictionary<string, object> parameters);    }
    /// <summary>    /// 訊息通道選取器    /// </summary>    public interface IMessageChannelSelector    {        /// <summary>        /// 擷取訊息通道        /// </summary>        /// <param name="messageType"></param>        /// <param name="payload"></param>        /// <returns></returns>        MessageChannelSelectorResult GetChannel(string messageType, object payload);    }    /// <summary>    /// 訊息通道選取器的結果    /// </summary>    public class MessageChannelSelectorResult    {        /// <summary>        /// 優先順序        /// </summary>        public int Priority { get; set; }              /// <summary>        /// 訊息通道        /// </summary>        public Func<IMessageChannel> MessageChannel { get; set; }    }
  /// <summary>    /// 訊息通道管理器    /// </summary>    public interface IMessageChannelManager    {        /// <summary>        /// 擷取訊息通道        /// </summary>        /// <param name="type"></param>        /// <param name="parameters"></param>        /// <returns></returns>        IMessageChannel GetMessageChannel(string type, IDictionary<string, object> parameters);    }    /// <summary>    /// 訊息通道管理器的預設實現    /// </summary>    public class MessageChannelManager : IMessageChannelManager    {        /// <summary>        /// 通道選取器列表        /// </summary>        private readonly IEnumerable<IMessageChannelSelector> _messageChannelSelectors;        public MessageChannelManager(IEnumerable<IMessageChannelSelector> messageChannelSelectors)        {            _messageChannelSelectors = messageChannelSelectors;        }        public IMessageChannel GetMessageChannel(string type, IDictionary<string, object> parameters)        {            var messageChannelResult = _messageChannelSelectors                .Select(x => x.GetChannel(type, parameters))                .Where(x => x != null)                .OrderByDescending(x => x.Priority)                .FirstOrDefault();            return messageChannelResult == null ? null : messageChannelResult.MessageChannel();        }    }
    /// <summary>    /// 資訊服務介面    /// </summary>    public interface IMessageService     {        void Send(string type, IDictionary<string, object> parameters);    }
 public class DefaultMessageService : IMessageService {        //訊息通道管理器        private readonly IMessageChannelManager _messageChannelManager;        public DefaultMessageService(IMessageChannelManager messageChannelManager)        {            _messageChannelManager = messageChannelManager;        }        /// <summary>        /// 發送訊息        /// </summary>        /// <param name="type">訊息類型</param>        /// <param name="parameters">參數列表</param>        public void Send(string type, IDictionary<string, object> parameters)        {            //從訊息通達管理器中擷取訊息通道對象            var messageChannel = _messageChannelManager.GetMessageChannel(type, parameters);            if (messageChannel == null)return;            //調用訊息通道對象,處理具體的發送方法            messageChannel.Process(parameters);        }}

三 使用方式

 現在我需要使用電子郵件發送一個訊息。 怎麼使用此訊息發送設計呢?

1.我們通過實現IMessageChannel介面,來建立一個電子郵件發送通道(電子郵件發送方式)來發送訊息。

  public class SmtpMessageChannel : IMessageChannel    {
    public static readonly string MessageType = "Email"; //以某種方式得到SMTP對象。 SmtpClient smtpClient; public void Process(IDictionary<string, object> parameters) { //從參數中提取出發送的相關資訊。 var emailMessage = new EmailMessage { Body = parameters["Body"] as string, Subject = parameters["Subject"] as string, Recipients = parameters["Recipents"] as string }; //構建Mail Message var mailMessage = new MailMessage { Subject = emailMessage.Subject, Body = emailMessage.Body,//這裡你還可以加入模板功能。 IsBodyHtml = true }; try { //迴圈添加收件者 foreach(var recipient in emailMessage.Recipients.Split(new [] {‘;‘}, StringSplitOptions.RemoveEmptyEntries)) { mailMessage.To.Add(new MailAddress(recipient)); } //使用SMTP 發送郵件 smtpClient.Send(mailMessage); } catch(Exception e) { //進行異常的相關處理。 } }

2.實現通道選取器

public class EmailMessageChannelSelector : Component, IMessageChannelSelector
{ public const string ChannelName = "Email"; public MessageChannelSelectorResult GetChannel(string messageType, object payload)
{ if (messageType == "Email")
{
return new MessageChannelSelectorResult
{ Priority = 50, MessageChannel = () =>new SmtpMessageChannel(),//這裡你可以使用IoC得到IMessageChannel對象
};
             } return null; 
}
}

 

3.然後,我們只要在我們的程式中得到一個IMessageService的一個執行個體,然後調用Send方法,發送資訊即可。

C# 訊息發送功能(支援多種發送方式)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.