簡體   English   中英

如何解決:“調用目標引發了異常”C#

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

C#

每次我運行我的porgram我都會得到這個例外: 替代文字

但是當我在調試模式下運行時,沒有異常並且程序運行正常,我該怎么辦?

注意:我不在項目的任何地方使用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?

TargetInvocationException通過告訴你它在“方法調用”期間崩潰,通常通過something.Invoke 掩蓋真正的異常。

您需要做的是查看異常對象( TargetInvocationException對象)的InnerException屬性,這將為您提供拋出的實際異常,並提供更有用的堆棧跟蹤。

您正在使用Protobuf反序列化它不理解的內容。 可能是使用您的程序集的另一個版本序列化的數據或者您未首先序列化的數據。 Google Protocol Buffers可用於將對象的表示形式寫入流。 您可以稍后反序列化流以重新創建對象。 但是,以相同的方式序列化和反序列化對象非常重要。 如果你只是在反序列化中輸入垃圾,你會得到奇怪的異常拋出。

問題發生在MainForm.cs,第97行。

如果您只是在發布模式下運行時遇到錯誤,那么您嘗試反序列化的文件可能位於二進制目錄中,並且發布模式文件已過期,也就是說,它包含舊版本的序列化數據您正在序列化的數據。

在我的情況下,我使用MD5加密技術,因為在服務器上啟用了FIPS 我使用SHA1來計算哈希,它對我有用。

對我來說,在XAML項目的OnLoad()中有一些空引用(並且它引發了OP的模糊錯誤)。 正如LasseVågsætherKarlsen回答的那樣,看着內部異常的堆棧跟蹤讓我看到了行號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM