6

I wrote a User Defined Function that gets a UtcTimeStamp and a windowstimezoneid as parameters and returns the TimeStamp for the TimeZone.

But if i want to execute it in a simple select 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 the select as sa. I set clr enabled to 1. I use SQL Server 2008 R2 (10.50.1600).

Does anyone know what i have to set to get this working or what i may done wrong?

3 Answers 3

11

CLR assemblies have trust levels.

This one requires UNSAFE permissions because of the rights required to use "MayLeakOnAbort"

Either change the CLR to something safer, or re-add the assembly with UNSAFE rights. The word "UNSAFE" is exactly that of course...

Sign up to request clarification or add additional context in comments.

2 Comments

thanks had to set trustworthy on first and changed the database owner to sa, but now it works with unsafe.
@gbn I have created assembly and also given Unsafe permission there But still getting the error.
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.

1 Comment

Had to found this by myself, but I think this one deserves to be upvoted so people know about this
1

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

MayLeakOnAbort Members and Types

So check if you are using any of those and see if you can change it. Otherwise you will have to mark UNSAFE or something similar

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.