How to solve: “exception was thrown by the target of invocation” C#

匿名 (未验证) 提交于 2019-12-03 02:16:02

问题:

C#

Every time I run my porgram I get this exception:

But when I run in debug mode, there is no exception and the program works fine, what can I do?

NOTE: I do not use invoke() anywhere in the project

EDIT: Okay, here is the code found in the details: If someone know how to use protoBuff, and know this problem....

    ************** Exception Text ************** System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> ProtoBuf.ProtoException: Incorrect wire-type deserializing TimeSpan    at ProtoBuf.ProtoBcl.ProtoTimeSpan.DeserializeTicks(SerializationContext context) in c:\protobuf-net_fixed\trunk\protobuf-net\ProtoBcl\ProtoTimeSpan.cs:line 80    at ProtoBuf.ProtoBcl.ProtoTimeSpan.DeserializeDateTime(SerializationContext context) in c:\protobuf-net_fixed\trunk\protobuf-net\ProtoBcl\ProtoTimeSpan.cs:line 41    at ProtoBuf.Property.PropertyDateTimeString`1.DeserializeImpl(TSource source, SerializationContext context) in c:\protobuf-net_fixed\trunk\protobuf-net\Property\PropertyDateTimeString.cs:line 32    at ProtoBuf.Property.Property`2.Deserialize(TSource source, SerializationContext context) in c:\protobuf-net_fixed\trunk\protobuf-net\Property\Property.cs:line 150    at ProtoBuf.Serializer`1.Deserialize[TCreation](T& instance, SerializationContext context) in c:\protobuf-net_fixed\trunk\protobuf-net\SerializerT.cs:line 568    at ProtoBuf.Serializer`1.DeserializeChecked[TCreation](T& instance, SerializationContext source) in c:\protobuf-net_fixed\trunk\protobuf-net\SerializerT.cs:line 400    at ProtoBuf.SerializerItemProxy`2.Deserialize(TActualClass& instance, SerializationContext source) in c:\protobuf-net_fixed\trunk\protobuf-net\SerializerProxy.cs:line 86    at ProtoBuf.Serializer.Deserialize[T](SerializationContext source) in c:\protobuf-net_fixed\trunk\protobuf-net\Serializer.cs:line 302    at ProtoBuf.Serializer.Deserialize[T](Stream source) in c:\protobuf-net_fixed\trunk\protobuf-net\Serializer.cs:line 289    --- End of inner exception stack trace ---    at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)    at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)    at ProtoBuf.Serializer.NonGeneric.Deserialize(Type type, Stream source) in c:\protobuf-net_fixed\trunk\protobuf-net\NonGeneric.cs:line 154    at ProtoBuf.Serializer.NonGeneric.TryDeserializeWithLengthPrefix(Stream source, PrefixStyle style, Getter`2 typeReader, Object& item) in c:\protobuf-net_fixed\trunk\protobuf-net\NonGeneric.cs:line 128    at AccessPoint.MainForm.getEventsList() in C:\Users\user\Desktop\accesspoint\AccessPoint\AccessPoint\MainForm.cs:line 97    at AccessPoint.MainForm.Form1_Load(Object sender, EventArgs e) in C:\Users\user\Desktop\accesspoint\AccessPoint\AccessPoint\MainForm.cs:line 18    at System.Windows.Forms.Form.OnLoad(EventArgs e)    at System.Windows.Forms.Form.OnCreateControl()    at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)    at System.Windows.Forms.Control.CreateControl()    at System.Windows.Forms.Control.WmShowWindow(Message& m)    at System.Windows.Forms.Control.WndProc(Message& m)    at System.Windows.Forms.ScrollableControl.WndProc(Message& m)    at System.Windows.Forms.Form.WmShowWindow(Message& m)    at System.Windows.Forms.Form.WndProc(Message& m)    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 

Row 97:

int startIndex = count - 10, index = 0;                 object obj;                 while (Serializer.NonGeneric.TryDeserializeWithLengthPrefix(file, PrefixStyle.Base128, tag =>                 {                     return index++ >= startIndex ? typeof(EventsWireFrame) : null;                 }, out obj))                 {                     EventsWireFrame evt = (EventsWireFrame)obj;                     AddEventToTable(evt.eventDate, evt.eventType, evt.eventKeyNumber, evt.eventKeyName, evt.eventDoor, true);                  } 

I can't get it, what's wrong? Do I need to add another part of code? Maybe the seraliztaion?

回答1:

TargetInvocationException masks the real exception by telling you that it crashed during "a method invocation", usually through something.Invoke.

What you have to do is look at the InnerException property of the exception object (the TargetInvocationException object), this will give you the actual exception that was thrown, with a more useful stack trace.



回答2:

You are using Protobuf to deserialize something it doesn't understand. Probably data serialized using another version of your assembly or data not serialized by you in the first place. Google Protocol Buffers can be used to write a representation of your object to a stream. You can later deserialize the stream to recreate the object. However, it is important that you serialize and deserialize the object in the same way. If you just feed garbage into the deserialization you will get weird exceptions thrown.

The problem occurs at MainForm.cs, line 97.

If you only get the error when you run in release mode then perhaps the file you are trying to deserialize is located in the binary directory and the release mode file is out of date, that is, it contains serialized data of an older version of the data you are serializing.



回答3:

In My case i was using MD5 cryptography where as FIPS was enabled on server. I used SHA1 to calculate hash and its worked for me.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!