如何解决:“调用目标引发了异常”C#

时间:2010-10-01 11:11:04

标签: c# .net exception

C#

每次我运行porgram时都会遇到这个异常: alt text

但是当我在调试模式下运行时,没有异常且程序运行正常,我该怎么办?

注意:我不在项目的任何地方使用 invoke()

编辑:好的,这是详细信息中的代码: 如果有人知道如何使用protoBuff,并且知道这个问题....

    ************** 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)

第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);

                }

我无法得到它,出了什么问题?我需要添加另一部分代码吗?也许是seraliztaion?

4 个答案:

答案 0 :(得分:34)

TargetInvocationException通过告诉您在“方法调用”期间崩溃,通常通过something.Invoke来掩盖真正的异常。

您需要做的是查看异常对象的InnerException属性(TargetInvocationException对象),这将为您提供抛出的实际异常,并提供更有用的堆栈跟踪。

答案 1 :(得分:4)

您正在使用Protobuf反序列化它不理解的内容。可能是使用您的程序集的另一个版本序列化的数据或者您未首先序列化的数据。 Google Protocol Buffers可用于将对象的表示形式写入流。您可以稍后反序列化流以重新创建对象。但是,以相同的方式序列化和反序列化对象非常重要。如果你只是在反序列化中输入垃圾,你就会得到奇怪的例外。

问题发生在MainForm.cs第97行。

如果在发布模式下运行时只出现错误,那么您尝试反序列化的文件可能位于二进制目录中,并且发布模式文件已过期,也就是说,它包含较旧的序列化数据您要序列化的数据版本。

答案 2 :(得分:0)

在我的情况下,我使用MD5加密,其中FIPS was enabled on server。 我使用SHA1来计算哈希,它对我有用。

答案 3 :(得分:-1)

对我来说,XAML项目的OnLoad()中有一些空引用(这引发了OP的模糊错误)。就像LasseVågsætherKarlsen回答的那样,查看内部异常的堆栈跟踪使我知道了行号。