Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Thread.CurrentPrincipal Casting Problem

Posted on 2011-03-11
5
Medium Priority
?
352 Views
Last Modified: 2012-05-11
The code Thread.CurrentPrincipal as IChatPrinciple) returns null. IChatPrinciple extends IPrincipal, but a conversion error still occurs.  I've been upgrading my application from .net 2.0 to .net 4.0.  The .net 2.0 app did not an issue with the code, and I'm at a loss as to what IIS (version 6) settings could be causing this.  Any help would be much appreciated. Thanks.
0
Comment
Question by:wideman1926
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 35112146
You can't cast between two derived types--you can only cast to a common base type (or interface). You should be able to cast Thread.CurrentPrincipal to IPrincipal.
0
 

Author Comment

by:wideman1926
ID: 35112265
Thread.CurrentPrincipal is IPrinciple, which is the base class of the class that I'm casting to.  You can cast from a base to a derived class.  Plus, the application that this code is a part of works and is used heavily.  The application will not work if that line of code fails.  It worked fine in 2.0, but is failing in .net 4.0.  That's what's perplexing me.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35112372
You're not understanding what I am saying  : )

You are trying to cast from one derived type to another derived type. This cannot be done. You can cast each derived type to IPrinciple, but you cannot cast between two derived types.

Technically speaking, we are talking about casting to an interface, not a base class.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35112401
Think of it in this example.


We have a base class Employee. We extend it in two classes:  SalariedEmployee and HourlyEmployee. I can cast a SalariedEmployee to be an Employee, and I can cast an HourlyEmployee to be an Employee. Does it make sense that I should be able to cast a HourlyEmployee to be a SalariedEmployee?
0
 

Author Comment

by:wideman1926
ID: 35112450
No it doesn't.  I didn't write the code.  I'm just trying to figure out how to make it work in .net 4.0.  Anyway, I'm curious, though,  Why did the code work in the .net 2.0 application?  It gets run constantly, so it's not an issue of it not being reached.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question