PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/App_Code/Extensions/SendPings.cs

#
C# | 113 lines | 60 code | 18 blank | 35 comment | 9 complexity | abcfc11a8a0e093748ca71dd056ccf0e MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. #region using
  2. using System;
  3. using System.Threading;
  4. using BlogEngine.Core;
  5. using BlogEngine.Core.Ping;
  6. using BlogEngine.Core.Web.Controls;
  7. using BlogEngine.Core.Web.Extensions;
  8. using System.Collections.Specialized;
  9. using BlogEngine.Core.Providers;
  10. #endregion
  11. /// <summary>
  12. /// Pings all the ping services specified on the
  13. /// PingServices admin page and send track- and pingbacks
  14. /// </summary>
  15. [Extension("Pings all the ping services specified on the PingServices admin page and send track- and pingbacks", "1.3",
  16. "BlogEngine.NET")]
  17. public class SendPings
  18. {
  19. #region Constructors and Destructors
  20. /// <summary>
  21. /// Initializes static members of the <see cref="SendPings"/> class.
  22. /// Hooks up an event handler to the Post.Saved event.
  23. /// </summary>
  24. static SendPings()
  25. {
  26. Post.Saved += PostSaved;
  27. Page.Saved += PostSaved;
  28. }
  29. #endregion
  30. #region Methods
  31. /// <summary>
  32. /// Executes the pings from the new thread.
  33. /// </summary>
  34. /// <param name="item">
  35. /// The publishable item.
  36. /// </param>
  37. /// <param name="itemUrl">
  38. /// The item Url.
  39. /// </param>
  40. private static void Ping(IPublishable item, Uri itemUrl)
  41. {
  42. try
  43. {
  44. Thread.Sleep(2000);
  45. // Ping the specified ping services.
  46. PingService.Send(itemUrl);
  47. // Send trackbacks and pingbacks.
  48. if (!BlogSettings.Instance.EnableTrackBackSend && !BlogSettings.Instance.EnablePingBackSend)
  49. {
  50. return;
  51. }
  52. if (item.Content.ToUpperInvariant().Contains("\"HTTP"))
  53. {
  54. Manager.Send(item, itemUrl);
  55. }
  56. }
  57. catch (Exception)
  58. {
  59. // We need to catch this exception so the application doesn't get killed.
  60. }
  61. }
  62. /// <summary>
  63. /// Handles the Saved event of the Post control.
  64. /// Sends the pings in a new thread.
  65. /// <remarks>
  66. /// It opens a new thread and executes the pings from there,
  67. /// because it takes some time to complete.
  68. /// </remarks>
  69. /// </summary>
  70. /// <param name="sender">The source of the event.</param>
  71. /// <param name="e">The <see cref="BlogEngine.Core.SavedEventArgs"/> instance containing the event data.</param>
  72. private static void PostSaved(object sender, SavedEventArgs e)
  73. {
  74. if (!ExtensionManager.ExtensionEnabled("SendPings"))
  75. return;
  76. if (e.Action == SaveAction.None || e.Action == SaveAction.Delete)
  77. return;
  78. var item = (IPublishable)sender;
  79. if (!item.IsVisibleToPublic)
  80. return;
  81. var url = item.AbsoluteLink;
  82. // Need blogSettings to pass to Ping since the current blog instance won't
  83. // be detectable once in a BG thread.
  84. Guid blogId = Blog.CurrentInstance.Id;
  85. ThreadPool.QueueUserWorkItem(state =>
  86. {
  87. // because HttpContext is not available within this BG thread
  88. // needed to determine the current blog instance,
  89. // set override value here.
  90. Blog.InstanceIdOverride = blogId;
  91. Ping(item, url);
  92. });
  93. }
  94. #endregion
  95. }