SQLCLR - System.IO.FileLoadException: Could not load file or assembly

Today I was working with Caroline (Carol) Lavecchia Pereira on a issue with SQLCLR.

Even the error message being very clear of what was going on, I wasn’t expecting this behavior since it is not usual to mess around with system DLLs registrations.

Symptoms

After a server migration some CLR routines stopped working on the new server showing the following error.

1
2
3
4
5
Msg 6522, Level 16, State 1, Line 1
A .NET Framework error occurred during execution of user-defined routine or aggregate "MyUserRoutine":
System.IO.FileLoadException: Could not load file or assembly 'System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. Assembly in host store has a different signature than assembly in GAC. (Exception from HRESULT: 0x80131050) See Microsoft Knowledge Base article 949080 for more information.
System.IO.FileLoadException:
at MyNamespace.MyUserRoutine(String param0, String param1, String param2, String param3, String param4, String param5, String param6, String param7, String param8, String param9)

Diagnosing

Checking DLL load

At first I was expecting this error had something to do with the user DLL, so I decided to get it loaded outside SQLCLR just to check if it was ok.

1
PS C:\temp> [System.Reflection.Assembly]::LoadFile("C:\temp\MyDLL.dll")

The DLL loaded without any errors, so I was back do SQLCLR.

Checking KB

The error message itself pointed to this KB asking you to use ALTER ASSEMBLY to get it fixed.

I think this KB could have provided some examples of the ALTER ASSEMBLY statement and this KB is also missing some DLLs like System.ServiceModel, SMDiagnostics and System.IdentityModel.

Fix

1
2
3
4
5
6
7
8
9
10
11
ALTER ASSEMBLY [System.ServiceModel]
FROM 'C:\Windows\assembly\GAC_MSIL\System.ServiceModel\3.0.0.0__b77a5c561934e089\System.ServiceModel.dll'
ALTER ASSEMBLY [SMDiagnostics]
FROM 'C:\Windows\assembly\GAC_MSIL\SMDiagnostics\3.0.0.0__b77a5c561934e089\SMDiagnostics.dll'
ALTER ASSEMBLY [System.IdentityModel]
FROM 'C:\Windows\assembly\GAC_MSIL\System.IdentityModel\3.0.0.0__b77a5c561934e089\System.IdentityModel.dll'
ALTER ASSEMBLY [System.Runtime.Serialization]
FROM 'C:\Windows\assembly\GAC_MSIL\System.Runtime.Serialization\3.0.0.0__b77a5c561934e089\System.Runtime.Serialization.dll'