'System.Threading.ThreadAbortException' in ASP.net page - c#

I have the below code in my ASP.net page:
Response.StatusCode = 404
Response.Write(strResult)
Response.End()
The code above throws "An exception of type 'System.Threading.ThreadAbortException' occurred and was caught."
Can anyone tell me the reason why? And do I solve this.
thanks

Calling Response.End() from any child object of the page will most likely cause the 'System.Threading.ThreadAbortException' exception.
Please read this for a full explaination of why this is happening and methods to avoid/deal with the exception.

Related

How can i manage exceptions from Client.PostAsync<T>? [duplicate]

I was hoping somebody could enlighten me a little bit on an issue I am facing in regards to async/await exception handling with HttpClient. I have written some code to illustrate, and it is being excecuted on both a Windows Phone 8 device and the emulator:
private async void SearchButton_Click(object sender, EventArgs e)
{
try
{
HttpClient client = new HttpClient();
System.Diagnostics.Debug.WriteLine("BEGIN FAULTY REQUEST:");
string response = await client.GetStringAsync("http://www.ajshdgasjhdgajdhgasjhdgasjdhgasjdhgas.tk/");
System.Diagnostics.Debug.WriteLine("SUCCESS:");
System.Diagnostics.Debug.WriteLine(response);
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine("CAUGHT EXCEPTION:");
System.Diagnostics.Debug.WriteLine(exception);
}
}
Tapping the button that invokes this function, produces the following output in the debugger console, the most interesting being the ones in bold:
BEGIN FAULTY REQUEST:
An exception of type 'System.Net.WebException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary
An exception of type 'System.Net.WebException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary
A first chance exception of type 'System.Net.Http.HttpRequestException' occurred in mscorlib.ni.dll
An exception of type 'System.Net.Http.HttpRequestException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary
CAUGHT EXCEPTION:
(and here it prints out the HttpRequestException)
Of course I am expecting an error in this case since the URL I am calling is nonsense. What I am not understanding here, is why the debugger reports that the exceptions are not handled, when the output simultaneously reports that the exception is caught. Also, the UI side of the app becomes much less responsive while the output is being printed, indicating that something is probably amiss.
Is this not the way to handle exceptions when working with async and await? I appreciate any input! Thanks.
As you are using HttpClient, try to use response.EnsureSuccessStatusCode();
Now HttpClient will throw exception when response status is not a success code.
try
{
HttpResponseMessage response = await client.GetAsync("http://www.ajshdgasjhdgajdhgasjhdgasjdhgasjdhgas.tk/");
response.EnsureSuccessStatusCode(); // Throw if not a success code.
// ...
}
catch (HttpRequestException e)
{
// Handle exception.
}
ORIGINAL SOURCE OF THE CODE: http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client
This is an artifact of the debugger. It's determining that an exception is "uncaught" because it's not caught yet. In this case this is expected behavior.
You are handling the exceptions correctly.
The debugger is telling you that this exception is first chance. When a debugger is attached to your process it gets notified for every exception that is thrown and then based on how the debugger has been configured it will decide what to do with it. You can go through What is first chance exception? for more details.
On a side note, catch specific exceptions only so that you understand which exceptions you are expecting and why.

Invalid object name 'dbo.EdmMetadata'

I keep getting this error on initial load of my application. I have searched high and low and the only thing I found to do was the code below in my context.
Database.SetInitializer<Context>(null);
I have this set in the OnModelCreating method but it is still throwing the error below:
System.Data.SqlClient.SqlException: Invalid object name
'dbo.EdmMetadata'.
System.Data.Entity.Core.EntityCommandExecutionException: An error
occurred while executing the command definition. See the inner
exception for details.
You should add this code in constructor of your Context class.

Operation completed successfully when using resource

My winforms project is throwing this error:
"Exception thrown: 'System.ComponentModel.Win32Exception' in
System.Drawing.dll
Additional information: The operation completed successfully"
at this line:
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
The line is inside the auto generated code for the designer, so I'm not sure why it's throwing an error. I didn't edit it in any way, please help.
EDIT:
this is not a duplicate, the other question is quite vague on the actual code.
EDIT #2:
The error is now popping up at the designer of resources.resx, with the message
Parameter not valid
it now appears at this auto-generated code:
internal static System.Drawing.Bitmap WoodTex
{
get {
object obj = ResourceManager.GetObject("WoodTex", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
resourceCulture appears to be null, is this a different error entirely?

CEF Sharp causing an Object Disposed Exception

While I understand what an object disposed exception is, I don't quite know why it is occurring immediately after the object is instantiated. Below is my code:
var cookiemanager = Cef.GetGlobalCookieManager();
cookiemanager.SetCookieAsync(Domain, Cookie);
The error is occurring on the second line when I attempt to call the SetCookieAsync function stating:
An exception of type 'System.ObjectDisposedException' occurred in CefSharp.Core.dll but was not handled in user code
Any help on why the object was disposed or what I can do to remedy this error would be greatly appreciated!
After some more tinkering I decided to look at the provided Cef examples where I found the following snippet:
Cef.OnContextInitialized = delegate
{
var cookiemanager = Cef.GetGlobalCookieManager();
cookiemanager.SetCookieAsync(Domain, Cookie);
};
Making this changed seemed to work!

'System.Reflection.TargetInvocationException' when calling Activator.CreateInstance()

I am receiving this error:
A first chance exception of type 'System.Reflection.TargetInvocationException'
occurred in mscorlib.dll
and also:
Exception has been thrown by the target of an invocation.
when running this method, and the error is occurring on the CreateInstance(Type.GetType(instantiationString), parameters) call:
public static Report instantiateReport(ReportInfo reportInfo)
{
string instantiationString;
Report reportToReturn;
ReportInfo[] parameters = new ReportInfo[1];
parameters[0] = reportInfo;
instantiationString = reportInfo.Report.InstantiationPath;
reportToReturn = (Report)Activator.CreateInstance(Type.GetType(instantiationString), parameters);
return reportToReturn;
}
ReportInfo is just a custom class that contains variables that help me build the report that I am trying to instantiate and when I investigate it, everything is completely fine. The instantiationString just helps me understand which report is trying to be created at the call and that way I can use the Type.GetType() method. I really have no idea what kind of error this is or what could be going wrong. I researched it a bit but didn't receive any helpful answer so any help is appreciated.
EDIT After looking at the inner exception I am getting Object reference not set to an instance of an object. message. This is strange because neither the ReportInfo or the GetType() methods have a null reference. Say one of the variables within ReportInfo was null, could that cause an issue? It shouldn't right?
FIXED After delving into the stack trace of the inner exception I found that there was some conflicting code causing issues elsewhere. Problem solved.

Categories

Resources