Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.87 KB | None | 0 0
  1. private void SchedularCallback(object e)
  2. {
  3. this.WriteToFile("Simple Service Log: {0}");
  4. getData();//Email function
  5. this.ScheduleService();
  6. }
  7. public void ScheduleService()
  8. {
  9. try
  10. {
  11. Schedular = new Timer(new TimerCallback(SchedularCallback));
  12. string mode = "DAILY";
  13. this.WriteToFile("Simple Service Mode: " + mode + " {0}");
  14. //Set the Default Time.
  15. //DateTime d = DateTime.Today;
  16. //TimeSpan t = new TimeSpan(12, 40, 00);
  17. //DateTime scheduledTime = d.Date + t;
  18. DateTime scheduledTime = DateTime.Now.AddSeconds(30);
  19. if (DateTime.Now > scheduledTime)
  20. {
  21. //If Scheduled Time is passed set Schedule for the next day.
  22. // scheduledTime = scheduledTime.AddDays(1);
  23. scheduledTime = scheduledTime.AddDays(1);
  24. }
  25. TimeSpan timeSpan = scheduledTime.Subtract(DateTime.Now);
  26. string schedule = string.Format("{0} day(s) {1} hour(s) {2} minute(s) {3} seconds(s)", timeSpan.Days, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
  27. this.WriteToFile("Simple Service scheduled to run after: " + schedule + " {0}");
  28. //Get the difference in Minutes between the Scheduled and Current Time.
  29. int dueTime = Convert.ToInt32(timeSpan.TotalMilliseconds);
  30.  
  31. //Change the Timer's Due Time.
  32. Schedular.Change(dueTime, Timeout.Infinite);
  33. }
  34. catch (Exception ex)
  35. {
  36. WriteToFile("Simple Service Error on: {0} " + ex.Message + ex.StackTrace);
  37. }
  38. }
  39.  
  40. string body = ViewRenderer.RenderPartialView("~/Views/Shared/Email.cshtml", LEM);
  41.  
  42. public class ViewRenderer:ETHOS.Controllers.HomeController
  43. {/// <summary>
  44. /// Required Controller Context
  45. /// </summary>
  46. protected ControllerContext Context { get; set; }
  47.  
  48. /// <summary>
  49. /// Initializes the ViewRenderer with a Context.
  50. /// </summary>
  51. /// <param name="controllerContext">
  52. /// If you are running within the context of an ASP.NET MVC request pass in
  53. /// the controller's context.
  54. /// Only leave out the context if no context is otherwise available.
  55. /// </param>
  56. public ViewRenderer(ControllerContext controllerContext = null)
  57. {
  58. System.Web.HttpContext ctx = (System.Web.HttpContext) Session["ctx"];
  59. // Create a known controller from HttpContext if no context is passed
  60. if (controllerContext == null)
  61. {
  62. if (System.Web.HttpContext.Current != null)
  63. controllerContext = CreateController<EmptyController>().ControllerContext;
  64. else
  65. throw new InvalidOperationException(
  66. "ViewRenderer must run in the context of an ASP.NET " +
  67. "Application and requires HttpContext.Current to be present.");
  68. }
  69. Context = controllerContext;
  70. }
  71.  
  72. /// <summary>
  73. /// Renders a full MVC view to a string. Will render with the full MVC
  74. /// View engine including running _ViewStart and merging into _Layout
  75. /// </summary>
  76. /// <param name="viewPath">
  77. /// The path to the view to render. Either in same controller, shared by
  78. /// name or as fully qualified ~/ path including extension
  79. /// </param>
  80. /// <param name="model">The model to render the view with</param>
  81. /// <returns>String of the rendered view or null on error</returns>
  82. public string RenderViewToString(string viewPath, object model = null)
  83. {
  84. return RenderViewToStringInternal(viewPath, model, false);
  85. }
  86.  
  87. /// <summary>
  88. /// Renders a full MVC view to a writer. Will render with the full MVC
  89. /// View engine including running _ViewStart and merging into _Layout
  90. /// </summary>
  91. /// <param name="viewPath">
  92. /// The path to the view to render. Either in same controller, shared by
  93. /// name or as fully qualified ~/ path including extension
  94. /// </param>
  95. /// <param name="model">The model to render the view with</param>
  96. /// <returns>String of the rendered view or null on error</returns>
  97. public void RenderView(string viewPath, object model, TextWriter writer)
  98. {
  99. RenderViewToWriterInternal(viewPath, writer, model, false);
  100. }
  101.  
  102.  
  103. /// <summary>
  104. /// Renders a partial MVC view to string. Use this method to render
  105. /// a partial view that doesn't merge with _Layout and doesn't fire
  106. /// _ViewStart.
  107. /// </summary>
  108. /// <param name="viewPath">
  109. /// The path to the view to render. Either in same controller, shared by
  110. /// name or as fully qualified ~/ path including extension
  111. /// </param>
  112. /// <param name="model">The model to pass to the viewRenderer</param>
  113. /// <returns>String of the rendered view or null on error</returns>
  114. public string RenderPartialViewToString(string viewPath, object model = null)
  115. {
  116. return RenderViewToStringInternal(viewPath, model, true);
  117. }
  118.  
  119. /// <summary>
  120. /// Renders a partial MVC view to given Writer. Use this method to render
  121. /// a partial view that doesn't merge with _Layout and doesn't fire
  122. /// _ViewStart.
  123. /// </summary>
  124. /// <param name="viewPath">
  125. /// The path to the view to render. Either in same controller, shared by
  126. /// name or as fully qualified ~/ path including extension
  127. /// </param>
  128. /// <param name="model">The model to pass to the viewRenderer</param>
  129. /// <param name="writer">Writer to render the view to</param>
  130. public void RenderPartialView(string viewPath, object model, TextWriter writer)
  131. {
  132. RenderViewToWriterInternal(viewPath, writer, model, true);
  133. }
  134.  
  135. /// <summary>
  136. /// Renders a partial MVC view to string. Use this method to render
  137. /// a partial view that doesn't merge with _Layout and doesn't fire
  138. /// _ViewStart.
  139. /// </summary>
  140. /// <param name="viewPath">
  141. /// The path to the view to render. Either in same controller, shared by
  142. /// name or as fully qualified ~/ path including extension
  143. /// </param>
  144. /// <param name="model">The model to pass to the viewRenderer</param>
  145. /// <param name="controllerContext">Active Controller context</param>
  146. /// <returns>String of the rendered view or null on error</returns>
  147. public static string RenderView(string viewPath, object model = null,
  148. ControllerContext controllerContext = null)
  149. {
  150. ViewRenderer renderer = new ViewRenderer(controllerContext);
  151. return renderer.RenderViewToString(viewPath, model);
  152. }
  153.  
  154. /// <summary>
  155. /// Renders a partial MVC view to the given writer. Use this method to render
  156. /// a partial view that doesn't merge with _Layout and doesn't fire
  157. /// _ViewStart.
  158. /// </summary>
  159. /// <param name="viewPath">
  160. /// The path to the view to render. Either in same controller, shared by
  161. /// name or as fully qualified ~/ path including extension
  162. /// </param>
  163. /// <param name="model">The model to pass to the viewRenderer</param>
  164. /// <param name="writer">Writer to render the view to</param>
  165. /// <param name="controllerContext">Active Controller context</param>
  166. /// <returns>String of the rendered view or null on error</returns>
  167. public static void RenderView(string viewPath, TextWriter writer, object model,
  168. ControllerContext controllerContext)
  169. {
  170. ViewRenderer renderer = new ViewRenderer(controllerContext);
  171. renderer.RenderView(viewPath, model, writer);
  172. }
  173.  
  174. /// <summary>
  175. /// Renders a partial MVC view to string. Use this method to render
  176. /// a partial view that doesn't merge with _Layout and doesn't fire
  177. /// _ViewStart.
  178. /// </summary>
  179. /// <param name="viewPath">
  180. /// The path to the view to render. Either in same controller, shared by
  181. /// name or as fully qualified ~/ path including extension
  182. /// </param>
  183. /// <param name="model">The model to pass to the viewRenderer</param>
  184. /// <param name="controllerContext">Active Controller context</param>
  185. /// <param name="errorMessage">optional out parameter that captures an error message instead of throwing</param>
  186. /// <returns>String of the rendered view or null on error</returns>
  187. public static string RenderView(string viewPath, object model,
  188. ControllerContext controllerContext,
  189. out string errorMessage)
  190. {
  191. errorMessage = null;
  192. try
  193. {
  194. ViewRenderer renderer = new ViewRenderer(controllerContext);
  195. return renderer.RenderViewToString(viewPath, model);
  196. }
  197. catch (Exception ex)
  198. {
  199. errorMessage = ex.GetBaseException().Message;
  200. }
  201. return null;
  202. }
  203.  
  204. /// <summary>
  205. /// Renders a partial MVC view to the given writer. Use this method to render
  206. /// a partial view that doesn't merge with _Layout and doesn't fire
  207. /// _ViewStart.
  208. /// </summary>
  209. /// <param name="viewPath">
  210. /// The path to the view to render. Either in same controller, shared by
  211. /// name or as fully qualified ~/ path including extension
  212. /// </param>
  213. /// <param name="model">The model to pass to the viewRenderer</param>
  214. /// <param name="controllerContext">Active Controller context</param>
  215. /// <param name="writer">Writer to render the view to</param>
  216. /// <param name="errorMessage">optional out parameter that captures an error message instead of throwing</param>
  217. /// <returns>String of the rendered view or null on error</returns>
  218. public static void RenderView(string viewPath, object model, TextWriter writer,
  219. ControllerContext controllerContext,
  220. out string errorMessage)
  221. {
  222. errorMessage = null;
  223. try
  224. {
  225. ViewRenderer renderer = new ViewRenderer(controllerContext);
  226. renderer.RenderView(viewPath, model, writer);
  227. }
  228. catch (Exception ex)
  229. {
  230. errorMessage = ex.GetBaseException().Message;
  231. }
  232. }
  233.  
  234.  
  235. /// <summary>
  236. /// Renders a partial MVC view to string. Use this method to render
  237. /// a partial view that doesn't merge with _Layout and doesn't fire
  238. /// _ViewStart.
  239. /// </summary>
  240. /// <param name="viewPath">
  241. /// The path to the view to render. Either in same controller, shared by
  242. /// name or as fully qualified ~/ path including extension
  243. /// </param>
  244. /// <param name="model">The model to pass to the viewRenderer</param>
  245. /// <param name="controllerContext">Active controller context</param>
  246. /// <returns>String of the rendered view or null on error</returns>
  247. public static string RenderPartialView(string viewPath, object model = null,
  248. ControllerContext controllerContext = null)
  249. {
  250. ViewRenderer renderer = new ViewRenderer(controllerContext);
  251. return renderer.RenderPartialViewToString(viewPath, model);
  252. }
  253.  
  254. /// <summary>
  255. /// Renders a partial MVC view to string. Use this method to render
  256. /// a partial view that doesn't merge with _Layout and doesn't fire
  257. /// _ViewStart.
  258. /// </summary>
  259. /// <param name="viewPath">
  260. /// The path to the view to render. Either in same controller, shared by
  261. /// name or as fully qualified ~/ path including extension
  262. /// </param>
  263. /// <param name="model">The model to pass to the viewRenderer</param>
  264. /// <param name="controllerContext">Active controller context</param>
  265. /// <param name="writer">Text writer to render view to</param>
  266. /// <param name="errorMessage">optional output parameter to receive an error message on failure</param>
  267. public static void RenderPartialView(string viewPath, TextWriter writer, object model = null,
  268. ControllerContext controllerContext = null)
  269. {
  270. ViewRenderer renderer = new ViewRenderer(controllerContext);
  271. renderer.RenderPartialView(viewPath, model, writer);
  272. }
  273.  
  274.  
  275. /// <summary>
  276. /// Internal method that handles rendering of either partial or
  277. /// or full views.
  278. /// </summary>
  279. /// <param name="viewPath">
  280. /// The path to the view to render. Either in same controller, shared by
  281. /// name or as fully qualified ~/ path including extension
  282. /// </param>
  283. /// <param name="model">Model to render the view with</param>
  284. /// <param name="partial">Determines whether to render a full or partial view</param>
  285. /// <param name="writer">Text writer to render view to</param>
  286. protected void RenderViewToWriterInternal(string viewPath, TextWriter writer, object model = null, bool partial = false)
  287. {
  288. // first find the ViewEngine for this view
  289. ViewEngineResult viewEngineResult = null;
  290. if (partial)
  291. viewEngineResult = ViewEngines.Engines.FindPartialView(Context, viewPath);
  292. else
  293. viewEngineResult = ViewEngines.Engines.FindView(Context, viewPath, null);
  294.  
  295. if (viewEngineResult == null)
  296. throw new FileNotFoundException();
  297.  
  298. // get the view and attach the model to view data
  299. var view = viewEngineResult.View;
  300. Context.Controller.ViewData.Model = model;
  301.  
  302. var ctx = new ViewContext(Context, view,
  303. Context.Controller.ViewData,
  304. Context.Controller.TempData,
  305. writer);
  306. view.Render(ctx, writer);
  307. }
  308.  
  309. /// <summary>
  310. /// Internal method that handles rendering of either partial or
  311. /// or full views.
  312. /// </summary>
  313. /// <param name="viewPath">
  314. /// The path to the view to render. Either in same controller, shared by
  315. /// name or as fully qualified ~/ path including extension
  316. /// </param>
  317. /// <param name="model">Model to render the view with</param>
  318. /// <param name="partial">Determines whether to render a full or partial view</param>
  319. /// <returns>String of the rendered view</returns>
  320. private string RenderViewToStringInternal(string viewPath, object model,
  321. bool partial = false)
  322. {
  323. // first find the ViewEngine for this view
  324. ViewEngineResult viewEngineResult = null;
  325. if (partial)
  326. viewEngineResult = ViewEngines.Engines.FindPartialView(Context, viewPath);
  327. else
  328. viewEngineResult = ViewEngines.Engines.FindView(Context, viewPath, null);
  329.  
  330. if (viewEngineResult == null || viewEngineResult.View == null)
  331. throw new FileNotFoundException();//Resources.ViewCouldNotBeFound);
  332.  
  333. // get the view and attach the model to view data
  334. var view = viewEngineResult.View;
  335. Context.Controller.ViewData.Model = model;
  336.  
  337. string result = null;
  338.  
  339. using (var sw = new StringWriter())
  340. {
  341. var ctx = new ViewContext(Context, view,
  342. Context.Controller.ViewData,
  343. Context.Controller.TempData,
  344. sw);
  345. view.Render(ctx, sw);
  346. result = sw.ToString();
  347. }
  348.  
  349. return result;
  350. }
  351.  
  352.  
  353. /// <summary>
  354. /// Creates an instance of an MVC controller from scratch
  355. /// when no existing ControllerContext is present
  356. /// </summary>
  357. /// <typeparam name="T">Type of the controller to create</typeparam>
  358. /// <returns>Controller for T</returns>
  359. /// <exception cref="InvalidOperationException">thrown if HttpContext not available</exception>
  360. public static T CreateController<T>(RouteData routeData = null, params object[] parameters)
  361. where T : Controller, new()
  362. {
  363. // create a disconnected controller instance
  364. T controller = (T)Activator.CreateInstance(typeof(T), parameters);
  365.  
  366. // get context wrapper from HttpContext if available
  367. HttpContextBase wrapper = null;
  368. if (System.Web.HttpContext.Current != null)
  369. wrapper = new HttpContextWrapper(System.Web.HttpContext.Current);
  370. else
  371. throw new InvalidOperationException(
  372. "Can't create Controller Context if no active HttpContext instance is available.");
  373.  
  374. if (routeData == null)
  375. routeData = new RouteData();
  376.  
  377. // add the controller routing if not existing
  378. if (!routeData.Values.ContainsKey("controller") && !routeData.Values.ContainsKey("Controller"))
  379. routeData.Values.Add("controller", controller.GetType().Name
  380. .ToLower()
  381. .Replace("controller", ""));
  382.  
  383. controller.ControllerContext = new ControllerContext(wrapper, routeData, controller);
  384. return controller;
  385. }
  386.  
  387. }
  388.  
  389. /// <summary>
  390. /// Empty MVC Controller instance used to
  391. /// instantiate and provide a new ControllerContext
  392. /// for the ViewRenderer
  393. /// </summary>
  394. public class EmptyController : Controller
  395. {
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement