System.Security.HostProtectionException when executing a user-defined function on SQL Server

I wrote a custom function that takes the value of UtcTimeStamp and windowstimezoneid as parameters and returns a TimeStamp for TimeZone.

But if I want to execute it with a simple parting, I get the following error:

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) 

I execute select as sa. I set clr enabled to 1. I am using SQL Server 2008 R2 (10.50.1600).

Does anyone know what I need to configure in order to make this work or what can I do wrong?

+6
source share
3 answers

CLR nodes have trust levels.

This requires UNSAFE permission due to the rights required to use MayLeakOnAbort

Either change the CLR to something more secure, or re-add the assembly with UNSAFE rights. The word "UNSAFE" is exactly what ...

+11
source

In my case, I found that changing the HashSet to a list in my CLR code made the problem go away. I donโ€™t know why this is so. Hope this can help someone.

+2
source

Here is a list of types and members that the CLR will reject, as they may MayLeakOnAbort. Please note that this URL is for SQL 2017.

Members and types of MayLeakOnAbort

So check if you are using any of them and see if you can change it. Otherwise, you will have to mark WRONG or something like that

0
source

Source: https://habr.com/ru/post/891938/


All Articles