Why do I need to check the version myself?

D

D. Yates

Hi,

I got bit by the fact that applications using library files that are NOT
strongly named will use older versions of the library without generating an
error, so........

Is there a way to tell an application "Use the library files you were built
with or NEWER versions" without using the GAC?

I have tried giving my library files strong names, but that locks them to
ONE version of the library file. I know that you can add lines to your
application configuration file to tell the application to use a specific
version of a new file (i.e. if you using version 1.2.0.0- 1.2.3.0 now use
1.2.4.0). However, it doesn't allow me to specifiy "USE ALL newer versions"
(e.g. if you using version 1.2.0.0- 1.2.3.0 now use 1.2.*). I have to target
ONE newer version, which means EVERY time I have an update, I would have to
update every applications configuration file with new routing information.

I can use this code at runtime to fix the problem, but I'm look for other
options:

public static bool IsOkToUseLibraries(string sDirectory, string
sLibStartsWith,
Assembly asm, out string sProblem)
{
string sFileName;
sProblem = string.Empty;

foreach (AssemblyName asname in asm.GetReferencedAssemblies())
{
if (asname.Name.StartsWith(sLibStartsWith))
{
sFileName = sDirectory + "\\" + asname.Name + ".dll";
if (System.IO.File.Exists(sFileName))
{
System.Diagnostics.FileVersionInfo fvi =
System.Diagnostics.FileVersionInfo.GetVersionInfo(sFileName);

// major.minor[.build[.revision]]
if ((fvi.FileMajorPart != asname.Version.Major))
{
sProblem = string.Format("{0}.dll -> System {1} file " +
"found by a system {2} application.\n", asname.Name,
fvi.FileMajorPart, asname.Version.Major);
}
else if ((fvi.FileMinorPart < asname.Version.Minor) ||
(fvi.FileBuildPart < asname.Version.Build) ||
(fvi.FilePrivatePart < asname.Version.Revision))
{
sProblem = string.Format("{0}.dll -> Expecting a " +
"version number greater than or equal to {1}, but found
{2}.\n",
asname.Name, asname.Version.ToString(), fvi.FileVersion);
}
}
}
}

return (sProblem == string.Empty);
}


Thanks for any suggestions,
Dave
 
A

Andy

The behavior you describe is by design. The reason the app will use
the old version of the library is because the newer version's behavior
may not be compatiable with the older version. This is the cause of
many problems in the COM base world.

That said, you can specify using the application configuration file to
use newever versions of assemblies. Look up the bindingRedirect method
of the configuration file, that will give you information to force a
newer version of the library. Of course you would only want to do this
after you've tested your application against the newer version of the
library.

HTH
Andy
 
D

D. Yates

Andy,

Thanks for your response. I have looked at the bindingRedirect, but I would
have to modify the application configuration file of at least four different
application every time that I updated a library file because I can't specify
"use ALL newer files than the one you were compiled with".

Do you know if there is a way to do this in the configuration file without
naming the newer version number directly (i.e. 2.4.*)?

Dave
 
A

Andy

Dave,

I found somewhere that keeping the AssemblyVersion the same, but
changing the AssemblyFileVersion would work. Not sure if that would be
useful to you however.

Does using a wildcard in the newVersion attribute work?

Andy
 
D

D. Yates

Does using a wildcard in the newVersion attribute work?

I've tried it, but it doesn't work. You can specify a range for the old
version, but you have to specifically target the new version. Like:

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="mscorcfg"
publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect
oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="1.0.3300.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>

it would be nice if this worked:
<bindingRedirect oldVersion="1.0.0.0-1.3.2.65535" newVersion="1.3.3.*"/>
or

<bindingRedirect oldVersion="1.0.0.0-1.3.65535.65535" newVersion="1.4.*"/>

Dave
 
D

D. Yates

else if ((fvi.FileMinorPart < asname.Version.Minor) ||
(fvi.FileBuildPart < asname.Version.Build) ||
(fvi.FilePrivatePart < asname.Version.Revision))

Should read:
else if ((fvi.FileMinorPart < asname.Version.Minor) &&
(fvi.FileBuildPart < asname.Version.Build) &&
(fvi.FilePrivatePart < asname.Version.Revision))
 
A

Andy

I think you'll have to go the route of only changing the
AssemblyFileVersion and keeping the value in AssemblyVersion the same.
 

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

Top