Troubleshooting Guide: SQL Server Error 6239 – Invalid Custom Attribute in Assembly
Why This Error Happens
Error 6239 occurs when SQL Server’s CLR integration attempts to register or load a .NET assembly, but one of the types within that assembly contains a custom attribute that SQL Server cannot validate or resolve. This typically happens because the assembly was compiled against a different version of the .NET runtime than what SQL Server 2025 supports, the attribute references a dependent assembly that isn’t registered in the database, or the DLL itself is corrupted or was tampered with after compilation. Additionally, this can surface if the assembly was built with PERMISSION_SET = UNSAFE or EXTERNAL_ACCESS attributes that conflict with the server’s CLR strict security policies.
Step-by-Step Resolution Guide
Step 1: Verify the Assembly’s Target Framework and Metadata
- Open the source project in Visual Studio and confirm the target framework matches a version compatible with SQL Server 2025 CLR hosting (typically .NET Framework 4.8 or the specific CLR version SQL Server 2025 supports).
- Use
ildasm.exe(IL Disassembler) on the.dllfile to inspect the manifest and locate the specific custom attribute flagged in the error message. - Check for orphaned or malformed attributes, such as
[assembly: AllowPartiallyTrustedCallers]combined with incompatible security attributes.
Step 2: Re-register Dependent Assemblies
- Run the following query to check for missing dependencies in
sys.assembly_references:
SELECT * FROM sys.assembly_references WHERE assembly_id = (SELECT assembly_id FROM sys.assemblies WHERE name = 'YourAssemblyName');
- If a dependent assembly is missing, upload it first using
CREATE ASSEMBLYbefore attempting to register the parent assembly. - Ensure all dependencies are signed with the same strong-name key if
CLR STRICT SECURITYis enabled (default in SQL Server 2017+ and enforced in 2025).
Step 3: Rebuild the Assembly with Clean Attributes
- Strip unnecessary or legacy custom attributes (e.g., old
SecurityPermissionAttributedeclarations) from the source code, as these are often incompatible with modern CLR hosting policies. - Recompile the
.dllusing a clean build (dotnet build -c Releaseor MSBuild) to eliminate any corruption introduced by incremental builds. - Re-sign the assembly using
sn.exe -R yourassembly.dll yourkey.snkif strong naming is required.
Step 4: Update CLR Security Configuration
- On the SQL Server instance, verify
clr strict securitysettings via:
EXEC sp_configure 'clr strict security';
- If necessary, explicitly mark the assembly as trusted using
sp_add_trusted_assemblywith the correct SHA-512 hash of the file, rather than disabling strict security globally. - Restart the SQL Server service via
services.mscorRestart-Service MSSQLSERVERin PowerShell (Admin) for configuration changes to take effect.
Alternative or Verification Steps
- Verify the Fix: Attempt the
CREATE ASSEMBLYorALTER ASSEMBLYcommand again in SQL Server Management Studio (SSMS); success is confirmed if the assembly appears insys.assemblieswithout error. - Alternative Solution: If the attribute error persists due to legacy code, consider decompiling the DLL to source using a tool like dotPeek, manually removing the invalid attribute metadata, and recompiling the project from scratch to ensure a clean manifest.