Windows 2003 SP1 and specified cast is not valid

P

pdabak

Hello,

I had an ASP.NET application running on Windows 2003 machine and it was
all working fine. I upgraded the machine to Windows 2003 SP1 and now my
application is failing with "Specified cast is not valid" exception.
Here is the code snippet where the error comes from.

Object o=System.Activator.CreateInstance(t);
IMyInterface p = (IMyInterface)o;

I create an instance of type t and attempt to cast it to interface
IMyInterface. This was working fine till the moment I upgraded Windows
2003 SP1.

My belief is Window 2003 SP1 would have upgraded .NET framework 1.1 as
well and there must be some behavioral change in .NET framework SP1.

I found the following KB on Microsoft site which showed the similar
symptoms where Biztalk Server 2004 failed due to same reason after
applying .NET framework SP1.

http://support.microsoft.com/default.aspx?scid=KB;EN-US;875540

Although the KB talks about the hotfix for Biztalk server, it doesn't
talk about the underlying reason for this breaking after applying .NET
framework SP1.

Can anybody shade some light on this?

Thanks.
-Prasad
 
K

Karl Seguin

Why not simply debug, add a breakpoint at that 2nd line, and add a watch to
o, it should tell you what type it really is. It's possible that the
upgrade to 1.1 is making CreateInstance load to wrong type/version/assembly
or something like that...


Karl
 
P

pdabak

Hello,

I added some logging to my code

Object o=System.Activator.CreateInstance(t);
IMyInterface p = (IMyInterface)o;
Type t1=typeof(IMyInterface);
Type t2=o.GetType().GetInterface("IMyInterface", true);
log.Info("t1.AssemblyQualifiedName=" + t1.Assembly.CodeBase);
log.Info("t2.AssemblyQualifiedName=" + t2.Assembly.CodeBase);

It prints two different paths for the assembly (one coming from GAC and
one from the directory from where the application is loaded. Hence, its
not allowing for the cast operation.

NOTE: I tried the same application on other system not having Windows
2003 SP1 and the above code prints same paths for the assembly pointing
to location in GAC.

There must be some behavioural change in .NET framework 1.1 and .NET
framework 1.1 with SP1.

Any thoughts?

-Prasad
 
K

Karl Seguin

This thread is a little old in my head, but woudln't the output of your code
largely depend on how you got your variable t ?

I went through:
http://support.microsoft.com/?kbid=867460 which lists the SP1 fixes, and
didn't see anything relevant.

I can't offer much more help. You might want to consider a new post with
the additional information you now have so that someone else might be able
to help..

Karl
 
P

pdabak

Hello,

Type t is created is created as follows.

1. IMyInterface is defined in an assembly say myinterface.dll
2. myinterface.dll is placed in GAC.
3. IMyInterface is implemented in as assembly say myobject.dll
4. My application dynamically loads myobject.dll using
AppDomain.CurrentDomain.Load("myobject");
5. Then it enumerates the types in assembly and finds the one that
implements IMyInterface. This is "t" that I refer to in the code
snippet.

There is also a copy of myinterface.dll kept in application root
directory. If I remove this copy, things work fine. But, I want to know
if there is a way to get it working even if there is a local copy in
application root directory.

NOTE: This was all working fine irrespective of whether there is local
copy in application root directory or not. It broken only after I
upgraded to Windows 2003 SP1 which I presume installs .NET framework
1.1 SP1 also.

Thanks.
-Prasad
 
K

Karl Seguin

Prasad:
I've never used AppDomain.CurrentDomain.Load() to get a type. I normally
use Type.GetType() and I know when you use Type.GetType("xxx") and you want
to reference something in the GAC, you need to use the full assembly name,
including culture, version and publictoken. In other words, if I did

Type t = Type.GetType("myobject"); it probably wouldn't work, but if I
did
Type t = Type.GetType("ClassName, myObject, Version=1.0.00000.0,
Culture=neutral, PublicKeyToken=asdaso098098"); it probably would work.

Perhaps you need to do the same thing?

If you go into C:\WINDOWS\assembly you'll see a list of assemblies in the
GAC and you can get their Version, Culture and PublicKeyToken from the list
:)

I might be on the totally wrong track, but hope this is helpful,

Karl
 
P

pdabak

Hello,

Yes I agree that if I specify full assembly name, it will work.

The thing that concerns me most and I really want to know about is,
what is changed in .NET framework 1.1 SP1 that changes the behaviour.

If you look at
http://support.microsoft.com/default.aspx?scid=KB;EN-US;875540, .NET
framework 1.1 SP1 has clearly broken some backward compatibility for
Biztalk and Microsoft has to release a patch for Biztalk to address
that. Although, KB talks about the patch and says that its happens
after applying .NET framework 1.1 SP1, it doesn't say anything about
what changed in SP1 that lead to releasing a patch for Biztalk.

Thanks.
-Prasad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top