SQLCLR - 包装COM调用

时间:2011-06-29 16:50:12

标签: c# sql-server sqlclr

我试图从Sql Server调用一个包含几个COM调用(到第三方dll)的.net程序集。程序集注册正常(我尝试注册不安全和外部访问),但是当我运行该程序时,我收到此错误:

  

在执行用户定义的例程或聚合" ManagedCodeCallTest"期间发生.NET Framework错误:   System.UriFormatException:无效的URI:URI为空。   System.UriFormatException:      在System.Uri.CreateThis(String uri,Boolean dontEscape,UriKind uriKind)      在System.Uri..ctor(String uriString)      在System.ComponentModel.Design.RuntimeLicenseContext.GetLocalPath(String fileName)      at System.ComponentModel.Design.RuntimeLicenseContext.GetSavedLicenseKey(Type type,Assembly resourceAssembly)      在System.ComponentModel.LicenseManager.LicenseInteropHelper.GetCurrentContextInfo(Int32& fDesignTime,IntPtr& bstrKey,RuntimeTypeHandlerth)      在ManagedCode.MyClass.ArielComponentCall()

有什么想法吗?是我试图做的甚至可能吗?我读过有关许可dll的内容,但信息非常模糊。

编辑:CLR代码,以防它有用:

[SqlProcedure]
public static void ArielComponentCall()
{
    Ariel.ApplicationClass application = new Ariel.ApplicationClass();

    object arielDoc = application.OpenDocument(@"P:\Projects\COAT\Ariel1.run");
}

包含此类的项目具有对com对象的引用。

1 个答案:

答案 0 :(得分:2)

SQL Server上的SqlClr实现包含a list“祝福”的.net程序集方法,它们可以在SQL Server中运行。这是通过Host Protection Attributes进行管理的。更确切地说是

  

SQL Server不允许使用具有的类型或成员   HostProtectionAttribute,指定HostProtectionResource值   SharedState,Synchronization,MayLeakOnAbort或   ExternalProcessMgmt。这可以防止程序集调用成员   启用共享状态,执行同步,可能会导致   终止时资源泄漏,或影响SQL的完整性   服务器进程。

根据程序集的“访问”设置,SQL Server将抛出错误(在SAFE时),或对阻止的方法(UNSAFEEXTERNAL ACCESS不执行任何操作)。

不幸的是,System.ComponentModel.LicenseContext类具有SharedState主机保护属性,并且是不允许的代码的一部分。因此,在代码中的某个位置会调用LicenseManager中的方法,该方法将无声地执行任何操作。

无论哪种方式,在SQL Server进程中运行com组件都不是一个好主意,因为com组件中的崩溃会导致整个SQL Server崩溃。