How to upgrade or downgrade an assembly that has a referenced assembly for a CLR object in an instance of SQL Server 2005

Bug #: 490 (SQL Hotfix)
BUG #: 490 (SQL Hotfix)BUG #: 53324 (Content Maintenance)

Summary

When you try to upgrade or downgrade an assembly for a common language runtime (CLR) object in an instance of Microsoft SQL Server 2005, you cannot change the version of the referenced assemblies in the metadata of the assembly. Additionally, if you use a strong-named assembly for a CLR object, you cannot downgrade the assembly by using the ALTER ASSEMBLY statement.

INTRODUCTION

This article describes how to upgrade or downgrade an assembly that has a referenced assembly for a CLR object in an instance of SQL Server 2005.

More Information

How to upgrade or downgrade an assembly

In SQL Server 2005, you can use the ALTER ASSEMBLY statement to upgrade or downgrade a stand-alone assembly that was not created with a strong name. For a strong-named assembly, you can upgrade but cannot downgrade the assembly by using the ALTER ASSEMBLY statement.

You cannot upgrade or downgrade an assembly in the following scenario:
  • Assembly A is created for a CLR object in an instance. Assembly A has a referenced assembly. The referenced assembly is assembly B.
  • Assembly B has no referenced assembly.
  • You recompile assembly A to reference a different version of assembly B.
In this scenario, you can use the ALTER ASSEMBLY statement to upgrade assembly B to a new version. If assembly B is not a strong-named assembly, you can also downgrade assembly B to an old version. However, you cannot use the ALTER ASSEMBLY statement to upgrade or to downgrade assembly A to the recompiled version.

This behavior occurs because SQL Server creates a CLR fusion policy when you create assembly A. This CLR fusion policy automatically maps the version of assembly B in the metadata of assembly A to the actual version of assembly B. SQL Server loads the new version of assembly B at run time when you upgrade or downgrade assembly B to some other version. This behavior occurs even though assembly A refers to the original version of assembly B. However, when you try to upgrade or downgrade assembly A when assembly A contains a new version of assembly B in the metadata, the ALTER ASSEMBLY statement will fail, and you receive error messages that resemble the following:
Error message 1
Msg 6529, Level 16, State 1, Line 1

ALTER ASSEMBLY failed because the identity of referenced assembly 'RefAsm' has changed. Make sure the version, name, and public key have not changed.
Error message 2
Msg 6282, Level 16, State 1, Line 1

ALTER ASSEMBLY failed because the referenced assemblies would change. The referenced assembly list must remain the same.

Upgrade or downgrade the assembly

Notes
  • These steps upgrade or downgrade assembly A in the scenario that was described in the "How to upgrade or downgrade an assembly" section.
  • This example assumes that the versions of the two assemblies are both 1.0.0.0 and the assemblies are both written in C#. When you follow these steps, you try to upgrade assembly A and assembly B to version 2.0.0.0.
To upgrade or downgrade assembly A, follow these steps.
  1. Back up version 1.0.0.0 of assembly B to a folder.
  2. Modify and then recompile assembly B to version 2.0.0.0.
  3. Use the ALTER ASSEMBLY statement to upgrade assembly B in SQL Server 2005.
  4. Modify and then recompile assembly A to version 2.0.0.0. When you do this, reference version 1.0.0.0 of assembly B from the backup that you made in step 1. To do this, use the Csc.exe compiler tool together with the /reference switch. For example, use the following command:
    csc /target:library /out:AssemblyA.dll AssemblyA.cs AssemblyInfo.cs /reference:"BackupFolder\AssemblyB.dll"
    Note To verify the version of assembly B in the metadata of assembly A, open assembly A by using the Ildasm.exe utility. Then, verify the metadata information for the referenced assembly under the MANIFEST section.
  5. Use the ALTER ASSEMBLY statement to upgrade assembly A in SQL Server 2005.

How to downgrade a strong-named assembly

If you use a strong-named assembly for a CLR object, SQL Server 2005 does not support using the ALTER ASSEMBLY statement to downgrade assemblies. If you try to downgrade the version of an assembly by using the ALTER ASSEMBLY statement, you receive an error message that resembles the following:
Msg 6579, Level 16, State 1, Line 1
Alter assembly from 'Asm1, version=1.1.0.0, culture=neutral, publickeytoken=3a52b896cff0513d, processorarchitecture=msil' to 'Asm1, version=1.0.0.0, culture=neutral, publickeytoken=3a52b896cff0513d, processorarchitecture=msil' is not a compatible upgrade.
When you want to downgrade a strong-named assembly, follow these steps:
  1. Drop all objects that depend on the assembly.
  2. Drop the newer version of the assembly.
  3. Create the older version of the assembly.
  4. Create all required objects that depend on the assembly.
Note You can upgrade a strong-named assembly by using the ALTER ASSEMBLY statement. However, if the upgrade involves a referenced assembly, you must follow the same steps that are listed in the "Upgrade or downgrade an assembly" section.

References

For more information about the ALTER ASSEMBLY statement, visit the following Microsoft Developer Network (MSDN) Web site:For more information about the Ildasm.exe utility, visit the following MSDN Web site:
Propriedades

ID do Artigo: 917407 - Última Revisão: 13 de jun de 2008 - Revisão: 1

Comentários