在SQL Server上执行用户定义的函数时出现System.Security.HostProtectionException

时间:2011-07-04 07:55:26

标签: .net sql-server

我编写了一个用户定义函数,它获取UtcTimeStamp和windowstimezoneid作为参数,并返回TimeZone的TimeStamp。

但是如果我想在一个简单的选择中执行它,我会得到以下错误:

A .NET Framework error occurred during execution of user-defined routine or aggregate "ToLocalTime": 
System.Security.HostProtectionException: Attempted to perform an operation that was forbidden by the CLR host.

The protected resources (only available with full trust) were: All
The demanded resources were: MayLeakOnAbort

System.Security.HostProtectionException: 
   bei TimeFunctions.ToLocalTime(DateTime UtcTimestamp, String WindowsTimeZoneId)

我执行select as sa。我将clr设置为1.我使用SQL Server 2008 R2(10.50.1600)。

有谁知道我必须设置什么来使这个工作或我可能做错了什么?

3 个答案:

答案 0 :(得分:11)

CLR程序集有trust levels.

由于使用“MayLeakOnAbort”

所需的权利,因此需要UNSAFE权限

将CLR更改为更安全的内容,或者使用UNSAFE权限重新添加程序集。 “UNSAFE”这个词当然就是那个......

答案 1 :(得分:2)

In my case I found that changing a HashSet to a List in my CLR code made this issue go away. Have no idea why that is. Hopefully that might help someone.

答案 2 :(得分:0)

以下是CLR将拒绝的类型和成员的列表,因为它们是MayLeakOnAbort,请注意,此URL适用于SQL 2017。

MayLeakOnAbort Members and Types

因此,请检查您是否正在使用其中的任何一种,并查看是否可以更改。否则,您将必须标记UNSAFE或类似内容