How to register System.DirectoryServices for use in SQL CLR User Functions?

Saul Dolgin picture Saul Dolgin · Jun 2, 2010 · Viewed 14.2k times · Source

I am porting an old 32-bit COM component that was written in VB6 for the purpose of reading and writing to an Active Directory server. The new solution will be in C# and will use SQL CLR user functions.

The assembly that I am trying to deploy to SQL Server contains a reference to System.DirectoryServices. The project does compile without any errors but I am unable to deploy the assembly to the SQL Server because of the following error:

Error: Assembly 'system.directoryservices, version=2.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a.' was not found in the SQL catalog.

What are the correct steps for registering System.DirectoryServices on SQL Server?

Answer

Saul Dolgin picture Saul Dolgin · Jun 2, 2010

The information provided from other answers led me to the solution. Here are the steps I came up with for future reference:

CREATE ASSEMBLY [System.DirectoryServices]
FROM 'C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.DirectoryServices.dll'
WITH PERMISSION_SET = UNSAFE
GO

The first time I ran the statement above I got the following error:

CREATE ASSEMBLY for assembly 'System.DirectoryServices' failed because assembly 'System.DirectoryServices' is not authorized for PERMISSION_SET = UNSAFE. The assembly is authorized when either of the following is true: the database owner (DBO) has UNSAFE ASSEMBLY permission and the database has the TRUSTWORTHY database property on; or the assembly is signed with a certificate or an asymmetric key that has a corresponding login with UNSAFE ASSEMBLY permission.

In order to get the CREATE ASSEMBLY statement to execute without error I had to first turn TRUSTWORTHY ON as follows:

ALTER DATABASE DatabaseName SET TRUSTWORTHY ON
GO

Once TRUSTWORTHY is turned ON, the command executed without error but it did present this scary sounding warning:

Warning: The Microsoft .NET Framework assembly 'system.directoryservices, version=2.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a, processorarchitecture=msil.' you are registering is not fully tested in the SQL Server hosted environment and is not supported. In the future, if you upgrade or service this assembly or the .NET Framework, your CLR integration routine may stop working. Please refer SQL Server Books Online for more details.

With System.DirectoryServices properly registered in SQL Server I am now able to deploy/register the dependent custom SQL CLR assembly without any problems.