"Exception has been thrown by the target of an invocation" error (mscorlib)
Asked Answered
H

19

91

I have a website developed in ASP.Net 2.0 that is throwing the error

"Exception has been thrown by the target of an invocation" 

in the production environment. It was not throwing this error in development.

The source is 'mscorlib', and the stack trace says the error at

System.RuntimeMethodHandle._InvokeMethodFast.

The only thing I've changed since my last upload to production is that I've started using Membership controls (Login, LoginView), and have added a few more stored procedures and tables, etc. The membership depends upon a custom provider I've written.

Anybody have a clue why this could be happening?

Homologate answered 15/3, 2009 at 16:47 Comment(2)
Please post the entire exception, including InnerException's. Post ex.ToString().Shillong
cedenoaugusto commented - "I had got this error when tried to save changes in a field type char(40) but was receiving more characters than capacity supported."Edema
E
90

I'd suggest checking for an inner exception. If there isn't one, check your logs for the exception that occurred immediately prior to this one.

This isn't a web-specific exception, I've also encountered it in desktop-app development. In short, what's happening is that the thread receiving this exception is running some asynchronous code (via Invoke(), e.g.) and that code that's being run asynchronously is exploding with an exception. This target invocation exception is the aftermath of that failure.

If you haven't already, place some sort of exception logging wrapper around the asynchronous callbacks that are being invoked when you trigger this error. Event handlers, for instance. That ought to help you track down the problem.

Good luck!

Encephalon answered 15/3, 2009 at 16:51 Comment(4)
Hi, Thanks for your response! I have, in fact, exception logging, which is reporting the error. The code also checks for an inner exception. Maybe I should check at multiple levels (previous error, further level of exceptions). Thanks for the suggestion!Homologate
You were right! Looking into the full exception history showed me the true exception (Tableadapter has the development connection string)!Homologate
Does the inner exception catch the asynchronous Invoke() code exception? I have a desktop app that loads assembly code via reflection then calls it via MethodInfo.Invoke(), it doesn't log exceptions to file, and with reflection, I'm assuming the invoke() code will still be within same process as the parent calling that invoke().Circuit
@David: Sounds like you have a question! Consider clicking the "Ask Question" button at the top of the page. :)Encephalon
G
28

This can happen when invoking a method that doesn't exist.

Grape answered 4/8, 2010 at 4:6 Comment(2)
This was my problem in a Surface project. My control in the xaml file pointed to event handlers that didn't exist (actually the names didn't match up correctly).Motif
I faced this and got System.NotSupportedExceptionInfundibuliform
P
4

I know its kind of odd but I experienced this error for a c# application and finally I found out the problem is the Icon of the form! when I changed it everything just worked fine.

I should say that I had this error just in XP not in 7 or 8 .

Pernell answered 9/3, 2013 at 7:50 Comment(0)
C
3

Encounter the same error when tried to connect to SQLServer2017 through Management Studio 2014

enter image description here

The reason was backward compatibility

So I just downloaded the Management Studio 2017 and tried to connect to SQLServer2017.

Problem Solve!!

Catharinecatharsis answered 13/9, 2019 at 7:4 Comment(0)
P
2
' Get the your application's application domain.
Dim currentDomain As AppDomain = AppDomain.CurrentDomain 

' Define a handler for unhandled exceptions.
AddHandler currentDomain.UnhandledException, AddressOf MYExHandler 

' Define a handler for unhandled exceptions for threads behind forms.
AddHandler Application.ThreadException, AddressOf MYThreadHandler 

Private Sub MYExnHandler(ByVal sender As Object, _
ByVal e As UnhandledExceptionEventArgs) 
Dim EX As Exception 
EX = e.ExceptionObject 
Console.WriteLine(EX.StackTrace) 
End Sub 

Private Sub MYThreadHandler(ByVal sender As Object, _
ByVal e As Threading.ThreadExceptionEventArgs) 
Console.WriteLine(e.Exception.StackTrace) 
End Sub

' This code will throw an exception and will be caught.  
Dim X as Integer = 5
X = X / 0 'throws exception will be caught by subs below
Pyrometer answered 25/6, 2010 at 5:23 Comment(1)
I think it's a vary good practice to define those two exception handlers in order to avoid umexpected application crashesBillbillabong
C
2

This error occurs to me due to I have not set my Project as StartUp Project

When I set my current project to Set As Start-Up Project then it gone.

Cresida answered 2/4, 2019 at 8:24 Comment(0)
A
1

I just had this issue from a namespace mismatch. My XAML file was getting ported over and it had a different namespace from that in the code behind file.

Archduchess answered 20/1, 2016 at 20:55 Comment(0)
R
1

This is may have 2 reasons

1.I found the connection string error in my web.config file i had changed the connection string and its working.

  1. Connection string is proper then check with the control panel>services> SQL Server Browser > start or not
Roentgen answered 24/1, 2017 at 6:9 Comment(0)
O
1

my problem was that i didnt put the connection string in the right place in the appsettings.json it was inside IIS Express object then i moved it inside profiles object it didnt work as well the i fixed the problem by putting it inside the appsettings.json { here }

Opportunity answered 12/5, 2022 at 7:23 Comment(1)
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewBarling
G
0

Got same error, solved changing target platform from "Mixed Platforms" to "Any CPU"

Gusman answered 6/8, 2018 at 21:19 Comment(0)
G
0

In my case, this happened due to choosing the option to publish as a single file in VS (.NET 5 console app), once I changed it to publish normally the exception was gone.

Global answered 2/3, 2021 at 9:10 Comment(0)
R
0

I got this error after I tried to add a new controller in the Controller folder but the connection string was placed inside the entityFramework tag in Web.config file.

Relegate answered 2/4, 2021 at 23:1 Comment(0)
S
0

For me, it happens when there is an internal call to another server and that server is not answering in a logical time or the configuration to call it is wrong. I fixed the problem of connecting to another server, then I received the correct data from that server and the function that was responsible for calling that server is working properly then I do not receive that error.

Socialize answered 29/4, 2021 at 14:41 Comment(0)
K
0

I edited the validation field, the error was resolved.

builder.Property(x => x.Name)**.UseIdentityColumn()**.HasMaxLength(200);  
builder.Property(x => x.Name)**.IsRequired()**.HasMaxLength(200);
Kaleena answered 13/3, 2022 at 20:11 Comment(0)
C
0

In my case I just update Oracle.ManagedDataAccess.Core package from 2.19.60 to 3.21.61

Castalia answered 13/7, 2022 at 11:28 Comment(0)
B
0

I had the same problem. I was trying to create a report from C# and it wasn't checking if the field is empty or whitespace.

I add Exception.

Breadroot answered 23/12, 2022 at 11:37 Comment(0)
A
0

When debugging and no InnerException found it can be handy to hit F5 (continue) until it hits another Exception. Or if it drops out entirely you can then check the Output window for more details.

Alessi answered 18/1, 2023 at 12:11 Comment(0)
D
0

In my case just this worked:

dotnet ef migrations add MyMigrationName --startup-project {your .csproj} --context {your context}
Dentition answered 10/5, 2023 at 5:55 Comment(0)
E
0

This can have two reasons

  1. Use HasForeignKey when selecting the WithMany method
builder.HasOne(m => m.Model).WithMany(m => m.ParentModel).HasForeignKey(m => m.Number)
  1. Insert IsRequired for number
builder.Property(p => p.Number).IsRequired();

You must delete these two items

Exhume answered 25/5, 2023 at 19:33 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewInclusion

© 2022 - 2024 — McMap. All rights reserved.