簡體   English   中英

自升級到Visual Studio 2013以來的投射錯誤

[英]Casting Errors Since Upgrade to Visual Studio 2013

自從升級到Visual Studio 2013以來,我一直在投射一些錯誤。當我說“升級”時,我的意思是說最初該項目是在Visual Studio 2010中構建和維護的。現在在VS2013中進行處理時,出現了我不理解的轉換錯誤。

我得到了兩個,特別是其中一個我堅持。

第一個,我不太擔心

Server Error in '/' Application.

Unable to cast object of type 'System.Security.Principal.WindowsIdentity' to type 'System.Security.Principal.WindowsPrincipal'. 
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

 Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Security.Principal.WindowsIdentity' to type 'System.Security.Principal.WindowsPrincipal'.

Source Error: 
Line 21:     {
Line 22:      // omitted
Line 23:         WindowsPrincipal temp2 = (WindowsPrincipal)WindowsPrincipal.Current.Identity;
Line 24:         //HttpContext temp = (HttpContext)HttpContext.Current.User.Identity;
Line 25:         CustomClass spindle = (CustomClass)HttpContext.Current.User.Identity;

 Source File:  C:\My Projects\New Project\Default.aspx.cs    Line:  23 

Stack Trace: 

[InvalidCastException: Unable to cast object of type 'System.Security.Principal.WindowsIdentity' to type 'System.Security.Principal.WindowsPrincipal'.]
   _Default.Page_Load(Object sender, EventArgs e) in C:\My Projects\New Project\Default.aspx.cs:23
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +92
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34237 

我關注的第二個

我在另一個演員表中遇到相同的錯誤問題,但是這種情況更加具體。 我正在using使用SystemSystem.CollectionsSystem.Collections.GenericSystem.Security.Principal的內部類。 該類繼承並實現了MarshalByRefObjectIIdentity

當我創建自定義類的實例並嘗試為其分配值HttpContext.Current.User.Identity我得到與上面相同的轉換錯誤。 這是代碼。

CustomClass temp = (CustomClass)HttpContext.Current.User.Identity;

string FullName = temp.LastName + ", " + temp.FirstName;
Namelbl.Text = FullName;

就像我之前解釋的那樣,當我在Visual Studio 2010中使用該項目時,我沒有遇到任何此類問題。將項目遷移到供我們在Visual Studio 2013中使用的這些錯誤開始出現。 老實說,如果我在Visual Studio 2012中打開相同的項目,則不會收到錯誤消息。

打開項目,構建項目,右鍵單擊文件並在瀏覽器中選擇視圖時,出現錯誤。 這是一個帶有C#代碼隱藏的Web項目。 我在web.cofig文件中嘗試了十幾種不同的解決方案,這些解決方案也無法正常運行。

堆棧跟蹤對我來說也有點陌生,因此,如果有人可以破譯它並提供一種出色的解決方案。

編輯

我想我可能已經找到了解決方案,但是我不知道我是否在正確地解決問題。 我發現在“引用”選項卡下的“屬性頁”(單擊項目並單擊Shift + F4)內,所有CustomClass的引用都被版本化為“自動更新”。 如果您單擊一個類文件,然后單擊“更新”按鈕,則會在舊位置向引用該文件的文件顯示錯誤。

如果我從“屬性”頁中刪除引用,則它將dll從項目中的Bin文件夾中刪除。 因此,我刪除了在其版本列中具有“自動更新”的參考。 將dll從舊的項目位置(但不是太舊)復制到項目的Bin文件夾中。 然后使用正確的文件夾路徑將它們添加回去,但這似乎也不起作用。 我沒有正確添加參考嗎?

編輯

我現在認為引用的鏈接失敗。 當我嘗試查看在Visual Studio 2013中使用Microsoft.ReportViewer.WebForms創建的報表時,出現了錯誤。 盡管引用了GAC,但似乎仍然找不到它。 我得到的錯誤是

Parser Error Message: Could not load file or assembly 'Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.

這似乎非常類似於

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Security.Principal.WindowsIdentity' to type 'CustomClass'.

也許它不能將其強制轉換為CustomClass,因為它不知道它在哪里/在哪里!

我的自定義類的更新解決了該問題。

暫無
暫無

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

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