How to fix Error 6239 on Windows Server 2025 / Windows 11

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 .dll file 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 ASSEMBLY before attempting to register the parent assembly.
  • Ensure all dependencies are signed with the same strong-name key if CLR STRICT SECURITY is 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 SecurityPermissionAttribute declarations) from the source code, as these are often incompatible with modern CLR hosting policies.
  • Recompile the .dll using a clean build (dotnet build -c Release or MSBuild) to eliminate any corruption introduced by incremental builds.
  • Re-sign the assembly using sn.exe -R yourassembly.dll yourkey.snk if strong naming is required.

Step 4: Update CLR Security Configuration

  • On the SQL Server instance, verify clr strict security settings via:
  EXEC sp_configure 'clr strict security';
  • If necessary, explicitly mark the assembly as trusted using sp_add_trusted_assembly with the correct SHA-512 hash of the file, rather than disabling strict security globally.
  • Restart the SQL Server service via services.msc or Restart-Service MSSQLSERVER in PowerShell (Admin) for configuration changes to take effect.

Alternative or Verification Steps

  • Verify the Fix: Attempt the CREATE ASSEMBLY or ALTER ASSEMBLY command again in SQL Server Management Studio (SSMS); success is confirmed if the assembly appears in sys.assemblies without 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.