Thread abort after handling a POST

Gearsopedia.net

New Member
So this application runs seamlessly in Visual Studio, but I have created an installer for the program in which the error is encountered. I think I have pinned down what the issue is. When a POST is received it is handled which kicks off a separate decoupled process which eventually gets aborted from the webpage disposing/closing. The program flow is such, POST received \[code\]context.Request.HttpMethod == "POST"\[/code\], pertinent xml info extracted and written to disk \[code\]csfireEyeHandler.\[/code\]DonJobOnLastIp()\[code\], a monitor running in the background picks up on the file creation event\[/code\]void OnChanged' and starts running services based on the XML doc \[code\]FileAdded\[/code\] --> \[code\]readerRef.ReadInServices(e.FullPath, false)\[/code\]. The problem is after the POST is handled it causes the services to abort with the ThreadAbortException. If a delay is placed after \[code\]handler.ProcessRequest(context)\[/code\] the services finish, I presume because the page still open. I cannot figure out how to properly handle this situation, its terribly difficult to debug because I cannot get the error to occur in VS. \[code\]public partial class fireEye : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { HttpContext context = Context; fireEyeHandler handler = new fireEyeHandler(); handler.ProcessRequest(context); }}public class fireEyeHandler : IHttpHandler{ public void ProcessRequest(HttpContext context) { if (context.Request.HttpMethod == "POST") { var extension = context.Request.Url.AbsolutePath.Split('/')[2].ToLower(); var stream = context.Request.InputStream; var buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); var xml = Encoding.UTF8.GetString(buffer); FileManage.WriteToFile(xml, @"C:\ECC_output\fireEye.xml"); var csfireEyeHandler = new FireEyeService { config = extension + ".config" }; csfireEyeHandler.Load(); csfireEyeHandler.DonJobOnLastIp(); context.Response.StatusCode = 202; } } public bool IsReusable { get { return false; } }}public class Monitor{ bool monitorIsActive; readonly XmlRead readerRef; // Reference to the xml reader readonly FileSystemWatcher watch; public bool monitorRunning; public Monitor(XmlRead reader) { watch = new FileSystemWatcher(); readerRef = reader; try { watch.Path = @"C:\ECC_temp"; //directory to monitor } catch (ArgumentException ex) { Report.LogLine (ex.Message); return; } watch.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; watch.Filter = "*.xml"; monitorIsActive = true; watch.Created += OnChanged; watch.Deleted += OnChanged; watch.Renamed += OnRenamed; watch.EnableRaisingEvents = true; } /// <summary> /// Toggles on/off if a directory is being monitored /// </summary> public void ToggleMonitor() { monitorIsActive = !monitorIsActive; var monitorState = monitorIsActive ? "on" : "false"; Report.LogLine ("Monitor is " + monitorState); } /// <summary> /// File has been added to the directory /// </summary> public bool FileAdded(FileSystemEventArgs e, XmlDocument xmlDocument) { try { var date = string.Format ("<br>\r\n**********************Report {0:yyyy MM-dd hh:mm tt}**********************", DateTime.Now); Report.LogLine(date); readerRef.Validate(e.FullPath, false); readerRef.ReadInServices(e.FullPath, false); Report.CreateReport(); } catch (Exception exception) { Report.LogLine(exception.Message + " id:6"); Report.CreateReport(); return true; } return true; } /// <summary> /// When a file is added, renamed or deleted, OnChanged is called and the appropriate action is taken /// </summary> private void OnChanged(object source, FileSystemEventArgs e) { monitorRunning = true; while (true) { if (e.ChangeType == WatcherChangeTypes.Created || e.ChangeType == WatcherChangeTypes.Renamed) { var xmlDocument = new XmlDocument(); try { xmlDocument.Load(e.FullPath); } catch (IOException) { Thread.Sleep(100); } if (FileAdded(e, xmlDocument)) { break; } } } monitorRunning = false; }}\[/code\]
 
Back
Top