views:

101

answers:

1

Hi,

We've come up with a strategy to handle backward compatibility when there is a breaking change between two versions. We load the previous version assemblies in the current AppDomain, deserialize some data with the old version types and then convert these into their equivalent in the new version.

  1. Are there any pitfalls with this approach that I should be aware of?
  2. What would happen if I try to load a type by reflection when its assembly version is omitted, will it always load the latest version of the type if two version of it exists in the current AppDomain?

Edit:

Here is an scenario of question #2,

These two assemblies are loaded in the same AppDomain:

  • MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089
  • MyAssembly, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089

Both assembly define the type MyAssembly.MyType.

Then if some code use this kind of reflection:

Type t = Type.GetType("MyAssembly.MyType, MyAssembly, Culture=neutral, PublicKeyToken=b17a5c561934e089");

Will this call deterministically return MyAssembly.MyType, MyAssembly, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089?

I guess the same scenario would occur if I use BinaryFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple when deserializing types that exists in two loaded version of the same assembly.

+1  A: 

I'll work from the assumption that you use Assembly.LoadFile(). Reflection cannot really get you into trouble, it needs an Assembly reference to get the ball rolling. Your code is in control when it decides what Assembly reference to use. Clearly, you'll need the one you got from a previous call to LoadFile() if you'd want to load a type from the old version.

The LoadFile() call cannot otherwise mess up any other assembly resolves, the assembly is loaded without a loading context.

Hans Passant
I use Assembly.LoadFile to load the previous version assemblies, but my concerns are when Assembly.Load or Type.GetType is used when the assembly version is omitted. I updated the question.
SelflessCoder
I specifically addressed this in my answer: "cannot otherwise mess up".
Hans Passant
Ok, do you have any references or explanations for why "cannot otherwise mess up"?
SelflessCoder
Google "assembly loading context".
Hans Passant
The best load context documentation out there: http://blogs.msdn.com/suzcook/archive/2003/05/29/57143.aspx. Basically, if you use another way than Assembly.Load(), you need to subscribe to AppDomain.AssemblyResolve to avoid trouble.
erikkallen
I've done some reading. I think that partial name assembly loading will work for the current version, but things will get messed up if the previous version loaded with Assembly.LoadFile relied on partial name assembly loading as it would load the current type version instead of the previous one.
SelflessCoder
But it didn't, you use LoadFile().
Hans Passant