简体   繁体   中英

Casting Errors Since Upgrade to Visual Studio 2013

I've been getting a slough of casting errors since an upgrade to Visual studio 2013. When I say 'upgrade', I mean initially the project was built and maintained in Visual Studio 2010. The project was recently upgraded to a Visual Studio 2012 project and now when working on it in VS2013 I've been getting casting errors that I don't understand.

There are two I'm getting, one of which in particular I'm stuck on.

The first, which I'm not so worried about

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 

The second which I am focused on

I'm having the same error issue with another cast but this case is a little more specific. I'm using an in-house class that uses System , System.Collections , System.Collections.Generic , and System.Security.Principal . The class inherits and implements MarshalByRefObject and IIdentity .

When I create an instance of my custom class and try assigning it the value HttpContext.Current.User.Identity I get the cast error which is the same as above. Here is the code.

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

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

Like I explained earlier, I wasn't having any kind of problem like this when I was working with the project in Visual Studio 2010. Once the project was migrated over for us to use in Visual Studio 2013 these errors started popping up. To be quite honest, if I open the same exact project in Visual Studio 2012, I do not get the errors.

I get the errors when I open the project, build the project, right click the file and select view in browser. This is a web project with C# code-behind. I've tried a dozen different solutions inside the web.cofig file which didn't work as well.

The stack trace is also a little foreign to me so if anyone can decipher that and produce a solution that'd be excellent too.

Edit

I think I might have found the solution but I don't know if I'm doing the fix right. What I found is inside the 'Properties Pages' (click the project and hit Shift+F4) under the 'References' tab the references to all of the CustomClass are versioned as 'Auto Update'. If you click one of the Class files and click the 'Update' button it spits out an error to the file referencing it in an old location.

If I remove the references from the Properties Page it deletes the dlls out of the Bin folder in the project. So I removed the References that had 'Auto Update' in their version column. Copied the dlls from an old project location, but not too old, to the projects Bin folder. And then added them back using the correct folder path but that didn't seem to do the trick either. Am I not adding references correctly?

Edit

I now believe that the links to the references are failing. I am now getting errors when I try to view reports that were created in Visual Studio 2013 that use Microsoft.ReportViewer.WebForms . Though the GAC is referenced, it seems as though it's still not found. The error I'm getting is

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.

Which seems awfully similar to

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

Maybe it can't cast it to the CustomClass because it doesn't know what/where it is!

我的自定义类的更新解决了该问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM