Search code examples
c#.netsql-servermultithreadingsqlclr

System.Security.HostProtectionException: Attempted to perform an operation that was forbidden by the CLR host


I'm using threading in my SQLCLR Project and I'm getting the following error when I try to run my user defined function. What can I do to enable threading things such as the Parallel library and await/async in my project?

Msg 6522, Level 16, State 1, Line 4
A .NET Framework error occurred during execution of user-defined routine or aggregate "CalculateInfo":
  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: Synchronization, ExternalThreading

    System.Security.HostProtectionException:
        at UserDefinedFunctions.getData()
        at UserDefinedFunctions.CalculateInfo()


Solution

  • This error, as noted in your other question, Deploying SQLCLR project fails when creating assembly in database, requires the following:

    ALTER ASSEMBLY [AssemblyName]
       WITH PERMISSION_SET = UNSAFE;
    

    For more information on working with SQLCLR in general, please visit: SQLCLR Info