• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

Casting error with class obtained through reflection

Below is a snippet of code that I'm having problems with. When I reach the line containing the foreach loop, I get a casting exception. Here's what's going on... First, I am certain that the objects contained in the commissions ArrayList implement the ICalcuTreeCommission interface. Second, the call creating the ICalcuTreeCalculator uses reflection to obtain a reference to an object implementing this interface. The problem, I think, lies within the following article:

http://www.gotdotnet.com/team/clr/LoadFromIsolation.aspx

However, my question is this: how can I get around this? We are using this code to view commissions produced by different types of commission calculators. Although each commission is different, they all have certain things in common, such as when they were run and their amount. It is these kinds of things that I am looking to display using the interface and the reflection.

Finally, here's one final bizarre twist: the code below runs just fine when I put it into a console application. It only fails when I have it in an ASCX control in an ASP.NET web page. Any thoughts? Any solutions? Thanks!

ICalcuTreeCalculator calc = calculator.GetCalculator();
ArrayList commissions = calc.GetCommissions( distributor.Id );
foreach( ICalcuTreeCommission commission in commissions )
0
athomas4219
Asked:
athomas4219
1 Solution
 
KelmenCommented:
http://www.google.com/groups?hl=en&lr=lang_zh-CN|lang_zh-TW|lang_en&newwindow=1&c2coff=1&th=de7957ca93cc5a93&rnum=10

Check it out, maybe you are experiencing what I been

BTW, are yours dynamic assemblies and the interface strongnamed / GAC ?
0
 
athomas4219Author Commented:
I don't know about strongnamed...I've heard the term, but don't know what it means or how to check. As for the GAC, the assemblies we're trying to load are found in the bin directory of the ASP.NET application. However, and I don't know if this makes a difference, the assemblies are found in the same directory as the executable in the case of the console app. Could this have something to do with it in any way? If so, how/where do I put the assemblies for the web app to remove this error?
0
 
athomas4219Author Commented:
After doing a little digging into strong naming, I'm pretty sure that my assembly isn't strongly named. I would check in the AssemblyInfo.cs file, correct? I used sn.exe to generate a strong name and tried to use that file, but it then started complaining that every assembly I referenced in the project wasn't strongly named. Is this a requirement for a strongly named assembly?

Finally, if anyone is relatively sure that this will solve my problem I will look at getting those assemblies strongly named, but it's a lot of work (I didn't write them so I have to track down their authors for sourcecode) so I'd like to be reasonably sure that it would make a different before continuing.

Finally, I am still confused as to why this code would work with a console application but not with an ASP.NET application. Thoughts?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
NipNFriar_TuckCommented:
I experienced a similiar issue between a windows/console app, a service and a web page.  What I found is that for the windows app you can expect the working directory to be the same as where the executable is... However, when running a service or a web application (page, control, service) the working directory was system32 in the wondows directory...  A quick way to test this is to put your dlls in that directory and see of your code works... if it does this is the issue, otherwise it is something else...

If this is the issue you will need to give a full path to the dlls... note this should not be relative and it can not be gathered from the environment!  The way I solved this was to create a base direcotry for my application and set that in the app/web .config file... I then prepended this to all assemblies that I want to dynamically load.

Hope this helps...
0
 
athomas4219Author Commented:
Currently we have a database column that specifies the name of the assembly to load. When it comes time to load it, we run the following line of code:

Assembly a = Assembly.LoadFile( System.AppDomain.CurrentDomain.BaseDirectory + @"bin/" + assemblyName );

This code doesn't fail, and if I use debugging and stop points on the application I can see that my ArrayList is filled with 15 objects, exactly as I'd expect. This shows me that the assembly is loading correctly and running correctly.

One further strangeness: I used reflection to give me a list of interfaces supported by this object. Using GetType().GetInterfaces() I get only one...the one that I'm trying to cast to.

What I finally did, and this is only a workaround, is to use reflection to get the property that I know I want and display it that way. It works just fine, but isn't elegant and isn't in the spirit of everything. So, for the time being at least, I have it working. However, I STILL like to figure out the problem, so if anyone has any other suggestions or things for me to try, I'm all about finding a solution.

Thanks!
0
 
moduloCommented:
PAQed with points refunded (500)

modulo
Community Support Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now