Several solutions for CLR20R3 program termination

Source: Internet
Author: User
Loading editor...
[Switch] several solutions for CLR20R3 program termination
This is because. NET Framework 1.0 and 1.1 provide support for many unprocessed exceptions (for example, unprocessed exceptions in thread pool threads), while in Framework 2.0, the Common Language Runtime Library allows the majority of threads to continue without handling exceptions. In most cases, this means that unhandled exceptions will cause application termination.

I. C/S solution (any of the following methods)
1. Add the following content to the application configuration file:

2. Add the following content to the application configuration file:

3. Use the Application. ThreadException event to intercept exceptions before the program exits due to exceptions. Example:
[SecurityPermission (SecurityAction. Demand, Flags = SecurityPermissionFlag. ControlAppDomain)]
Public static void Main (string [] args)
{
Application. ThreadException + = new ThreadExceptionEventHandler (ErrorHandlerForm. Form1_UIThreadException );
Application. SetUnhandledExceptionMode (UnhandledExceptionMode. CatchException );
AppDomain. CurrentDomain. UnhandledException + = new UnhandledExceptionEventHandler (CurrentDomain_UnhandledException );

Application. Run (new ErrorHandlerForm ());
}

// Exceptions occur in the main thread
Private void button#click (object sender, System. EventArgs e)
{
Throw new ArgumentException ("The parameter was invalid ");
}

// Create a thread that generates an exception
Private void button2_Click (object sender, System. EventArgs e)
{
ThreadStart newThreadStart = new ThreadStart (newThread_Execute );
NewThread = new Thread (newThreadStart );
NewThread. Start ();
}

// Method for generating exceptions
Void newThread_Execute ()
{
Throw new Exception ("The method or operation is not implemented .");
}

Private static void Form1_UIThreadException (object sender, ThreadExceptionEventArgs t)
{
DialogResult result = DialogResult. Cancel;
Try
{
Result = ShowThreadExceptionDialog ("Windows Forms Error", t. Exception );
}
Catch
{
Try
{
MessageBox. Show ("Fatal Windows Forms Error ",
"Fatal Windows Forms Error", MessageBoxButtons. AbortRetryIgnore, MessageBoxIcon. Stop );
}
Finally
{
Application. Exit ();
}
}

If (result = DialogResult. Abort)
Application. Exit ();
}

// Because UnhandledException cannot prevent application termination, this example only records errors in the application event log before termination.
Private static void CurrentDomain_UnhandledException (object sender, UnhandledExceptionEventArgs e)
{
Try
{
Exception ex = (Exception) e. ExceptionObject;
String errorMsg = "An application error occurred. Please contact the adminstrator" +
"With the following information:/n ";

If (! EventLog. SourceExists ("ThreadException "))
{
EventLog. CreateEventSource ("ThreadException", "Application ");
}

EventLog myLog = new EventLog ();
MyLog. Source = "ThreadException ";
MyLog. WriteEntry (errorMsg + ex. Message + "/n/nStack Trace:/n" + ex. StackTrace );
}
Catch (Exception exc)
{
Try
{
MessageBox. Show ("Fatal Non-UI Error ",
"Fatal Non-UI Error. cocould not write the error to the event log. Reason :"
+ Exc. Message, MessageBoxButtons. OK, MessageBoxIcon. Stop );
}
Finally
{
Application. Exit ();
}
}
}

Private static DialogResult ShowThreadExceptionDialog (string title, Exception e)
{
String errorMsg = "An application error occurred. Please contact the adminstrator" +
"With the following information:/n ";
ErrorMsg = errorMsg + e. Message + "/n/nStack Trace:/n" + e. StackTrace;
Return MessageBox. Show (errorMsg, title, MessageBoxButtons. AbortRetryIgnore,
MessageBoxIcon. Stop );
}

II. B/S solution (any of the following methods)
1. Create the iexplore.exe. config file in the IE directory (C:/Program Files/Internet assumer.pdf. The content is as follows:

2. It is not recommended to use this method, which will cause programs later than framework 1.1 to report an error in IE.
Create the same configuration file as above, but the content is as follows:

3. This is complex and involves three steps:
(1) Save the following code as a file named UnhandledExceptionModule. cs. The path is C:/Program Files/Microsoft Visual Studio 8/VC/
Using System;
Using System. Diagnostics;
Using System. Globalization;
Using System. IO;
Using System. Runtime. InteropServices;
Using System. Text;
Using System. Threading;
Using System. Web;

Namespace WebMonitor {
Public class UnhandledExceptionModule: IHttpModule {

Static int _ unhandledExceptionCount = 0;

Static string _ sourceName = null;
Static object _ initLock = new object ();
Static bool _ initialized = false;

Public void Init (HttpApplication app ){

// Do this one time for each AppDomain.
If (! _ Initialized ){
Lock (_ initLock ){
If (! _ Initialized ){
String webenginePath = Path. Combine (RuntimeEnvironment. GetRuntimeDirectory (), "webengine. dll ");

If (! File. Exists (webenginePath )){
Throw new Exception (String. Format (CultureInfo. InvariantCulture,
"Failed to locate webengine. dll at '{0}'. This module requires. NET Framework 2.0 .",
WebenginePath ));
}

FileVersionInfo ver = FileVersionInfo. GetVersionInfo (webenginePath );
_ SourceName = string. Format (CultureInfo. InvariantCulture, "ASP. NET {0}. {1}. {2}. 0 ",
Ver. FileMajorPart, ver. FileMinorPart, ver. FileBuildPart );

If (! EventLog. SourceExists (_ sourceName )){
Throw new Exception (String. Format (CultureInfo. InvariantCulture,
"There is no EventLog source named '{0}'. This module requires. NET Framework 2.0 .",
_ SourceName ));
}

AppDomain. CurrentDomain. UnhandledException + = new UnhandledExceptionEventHandler (OnUnhandledException );

_ Initialized = true;
}
}
}
}

Public void Dispose (){
}

Void OnUnhandledException (object o, UnhandledExceptionEventArgs e ){
// Let this occur one time for each AppDomain.
If (Interlocked. Exchange (ref _ unhandledExceptionCount, 1 )! = 0)
Return;

StringBuilder message = new StringBuilder ("/r/n/r/nUnhandledException logged by UnhandledExceptionModule. dll:/r/n/r/nappId = ");

String appId = (string) AppDomain. CurrentDomain. GetData (". appId ");
If (appId! = Null ){
Message. Append (appId );
}

Exception currentException = null;
For (currentException = (Exception) e. ExceptionObject; currentException! = Null; currentException = currentException. InnerException ){
Message. appendFormat ("/r/n/r/ntype = {0}/r/n/r/nmessage = {1}/r/n/r/nstack =/r/n {2}/r/n ",
CurrentException. GetType (). FullName,
CurrentException. Message,
CurrentException. StackTrace );
}

EventLog Log = new EventLog ();
Log. Source = _ sourceName;
Log. WriteEntry (message. ToString (), EventLogEntryType. Error );
}
}
}

(2) Open the command prompt line window of Visual Studio 2005
Enter Type sn.exe-k key. snk and press Enter.
Enter Type csc/t: library/r: system. web. dll, system. dll/keyfile: key. snk UnhandledExceptionModule. cs, and press Enter.
Enter gacutil.exe/if UnhandledExceptionModule. dll and press Enter.
Enter ngen install UnhandledExceptionModule. dll and press Enter.
Enter gacutil/l UnhandledExceptionModule, press enter, and copy the displayed "Strong name" information.

(3) Open the Web. config file of the ASP.net application and add the following XML to it. Note: Not including "[]", ① may be added .

Iii. Solutions not recommended by Microsoft
Open the Aspnet. config file in the directory % WINDIR %/Microsoft. NET/Framework/v2.0.50727 and set the enabled attribute legacyUnhandledExceptionPolicy to true.

4. jump out of the three boundaries-ActiveX
ActiveX makes it impossible to change the settings of each client. The 3rd methods in the B/S solution do not work, I think it may be because of exceptions caused by ActiveX subcontrols.

It was intercepted by CLR and was not passed to the ActiveX control on the outermost layer. This is just a personal guess. If you have a clear friend, I hope to correct it.

In the end, I did not find a solution to ActiveX, but this is what I need most. In desperation, I re-checked the code and found the problem: A control is created in the Child thread and added to the UI of the main thread.
In the past, the system would report an error. This time, the system was able to bypass the attack. What I do not understand most is that there was no error in the C/S structure of framework 2.0, but in IE (ActiveX). Alas, it is the fault of the host.

Http://blog.csdn.net/fxfeixue/article/details/4466899

Original article:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.