11

我正在移植爲編寫Active Directory服務器而寫入VB6的舊3 2-bit COM組件。新解決方案將在C#中,並將使用SQL CLR用戶功能。如何註冊System.DirectoryServices以用於SQL CLR用戶功能?

我試圖部署到SQL Server的程序集包含對System.DirectoryServices的引用。該項目不編譯沒有任何錯誤,但我無法部署組裝到SQL Server,因爲有下列錯誤:

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

什麼是SQL Server上註冊System.DirectoryServices正確的步驟是什麼?

回答

7

從其他答案提供的信息讓我找到解決方案。以下是我想出了以供將來參考的步驟:

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

我第一次跑了上面的語句,我得到了以下錯誤:

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.

爲了得到CREATE語句來執行沒有錯誤,我不得不先打開TRUSTWORTHY ON如下:

ALTER DATABASE DatabaseName SET TRUSTWORTHY ON 
GO 

一旦守信是打開,命令執行沒有錯誤,但它確實存在這個可怕的冠冕堂皇的警告:

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在SQL Server中正確註冊我現在可以毫無問題地部署/註冊從屬定製SQL CLR程序集。

3

這篇文章在這裏幫忙嗎?

New "SQLCLR-approved" assembly in SP1

People often ask about the set of base class library assemblies that can be safely used in SQLCLR. It usually takes the form "can I use assembly System.XYZ.dll in SQLCLR procedural code" or "why do I get "assembly System.XYZ.dll is not found" when I try and catalog my own assembly that calls this one? The ones is hear mentioned most frequently is System.DirectoryServices.dll (Active Directory support) or System.Management.dll (WMI support) or System.Remoting.dll et al. The only way you use these is to run CREATE ASSEMBLY on them yourself, which involves using PERMISSION_SET = UNSAFE. And cataloging all the dependencies. Not for the faint of heart.

而且 - SQL Server的CLR不支持每imagineable總成 - 在這裏找到列表:從

一注第二個MSDN文章:

Unsupported Libraries

Unsupported libraries can still be called from your managed stored procedures, triggers, user-defined functions, user-defined types, and user-defined aggregates. The unsupported library must first be registered in the SQL Server database, using the CREATE ASSEMBLY statement, before it can be used in your code. Any unsupported library that is registered and run on the server should be reviewed and tested for security and reliability.

For example, the System.DirectoryServices namespace is not supported. You must register the System.DirectoryServices.dll assembly with UNSAFE permissions before you can call it from your code. The UNSAFE permission is necessary because classes in the System.DirectoryServices namespace do not meet the requirements for SAFE or EXTERNAL_ACCESS. For more information, see CLR Integration Programming Model Restrictions and CLR Integration Code Access Security.

0

想補充一點,我花了一天的時間嘗試註冊一個使用非對稱密鑰簽名DLL失敗的程序集。

. . . is not authorized for PERMISSION_SET = UNSAFE. The assembly is authorized when . . .

CREATE ASYMMETRIC KEY AsyKeyName FROM executable FILE = 'C:\Program Files\...' 

後來才發現,SQL服務器不喜歡在兩者之間的空間路徑!