[英]Unable to cast object of type 'System.Web.Hosting.SimpleWorkerRequest' to type 'System.Web.Hosting.IIS7WorkerRequest'
I am getting an InvalidCastException after I migrated my solution from VS2010 to VS2013. 我将解决方案从VS2010迁移到VS2013之后,我收到了InvalidCastException。
In VS2010 everything works fine, the same code gives the following error in VS2013: 在VS2010中,一切正常,在VS2013中,相同的代码给出以下错误:
An exception of type 'System.InvalidCastException' occurred in System.Web.dll but was not handled in user code System.Web.dll中发生类型'System.InvalidCastException'的异常,但未在用户代码中处理
Additional information: Unable to cast object of type 'System.Web.Hosting.SimpleWorkerRequest' to type 'System.Web.Hosting.IIS7WorkerRequest'. 其他信息:无法将类型为“ System.Web.Hosting.SimpleWorkerRequest”的对象转换为类型为“ System.Web.Hosting.IIS7WorkerRequest”的对象。
I don't know what to do, couldn't find anything related on the internet. 我不知道该怎么办,在互联网上找不到任何相关内容。
The error occurs on line 5 in the following piece of code: 在以下代码段的第5行出现错误:
public static void AuthenticateUser(String UserName, Int32 Minutes = 30, Boolean IsPersistent = true)
{
FormsAuthenticationTicket _ticket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.Now.AddMinutes(Minutes), IsPersistent, String.Empty);
FormsIdentity _id = new FormsIdentity(_ticket);
var _roles = Roles.GetRolesForUser(UserName);
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(_id, _roles);
}
Technically, this is not an answer. 从技术上讲,这不是答案。 But it helped me fix my problem. 但这帮助我解决了问题。 Therefore I am sharing this with you all, hopefully this will prevent some headache for somebody in the near future. 因此,我正在与大家分享这一点,希望这将在不久的将来防止某些人感到头疼。
In Visual Studio 2010 Professional I was running my project using the Visual Studio Development Server using a Specific Port (4302). 在Visual Studio 2010 Professional中,我正在使用Visual Studio Development Server通过特定端口(4302)运行我的项目。
In Visual Studio 2013 Community edition i was running my project on IIS Express. 在Visual Studio 2013社区版中,我在IIS Express上运行我的项目。 For some reason, I still do not know why, this caused my software to cast a 'IIS7WorkerRequest' to a 'SimpleWorkerRequest' object. 由于某些原因,我仍然不知道为什么,这导致我的软件将“ IIS7WorkerRequest”转换为“ SimpleWorkerRequest”对象。
After I switched to the Local IIS Server with a custom virtual directory my error was gone. 在使用自定义虚拟目录切换到本地IIS服务器后,我的错误消失了。 If there is somebody who can explain this, I would love to know why this is happening. 如果有人可以解释这一点,我很想知道为什么会这样。 But for now this fixed the problem above. 但是现在这解决了上面的问题。
You cannot cast from a sibling class to another sibling; 您不能从同级类转换为另一同级。 you can cast from parent to child, or child to parent, but not between siblings. 您可以从父对象投射到孩子,或从孩子投射到父母,但不能在兄弟姐妹之间投射。
https://msdn.microsoft.com/en-us/library/ms173105.aspx https://msdn.microsoft.com/en-us/library/ms173105.aspx
Since both SimpleWorkerRequest and IIS7WorkerRequest inherit from HttpWorkerRequest, you cannot cast between them. 由于SimpleWorkerRequest和IIS7WorkerRequest都继承自HttpWorkerRequest,因此无法在它们之间进行强制转换。 I would first ask yourself why you want to or think you have to perform this conversion, and if you cannot find an alternative then write a conversion method to map between them. 我首先要问自己为什么想要或认为必须执行此转换,如果找不到替代方法,请编写一种转换方法以在它们之间进行映射。 However, this is usually a good sign that you're headed down the wrong path for your code. 但是,这通常是一个好兆头,说明您走错了代码的正确路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.