Saturday, August 13, 2011

CLR Safety Issues After Database Restore to New Server


I was validating everythings functionality in SQL 2005 I found that I couldn't run any of the CLR functions in the Assembly, I kept getting errors like I would if the database was not set TRUSTWORTHY ON. Something like:

Msg 10314, Level 16, State 11, Line 1
An error occurred in the Microsoft .NET Framework while trying to load assembly id 65560. The server may be running out of resources, or the assembly may not be trusted with PERMISSION_SET = EXTERNAL_ACCESS or UNSAFE. Run the query again, or check documentation to see how to solve the assembly trust issues. For more information about this error:
System.IO.FileLoadException: Could not load file or assembly 'sqlserverproject1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An error relating to security occurred. (Exception from HRESULT: 0x8013150A)
System.IO.FileLoadException:
at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.Load(String assemblyString)

At first, I figured no problem, I have the source, I will just redeploy it, but I couldn't do that. I kept getting the standard error message letting me know TRUSTWORTHY was not ON, even though it was:
Server: Msg 10327, Level 14, State 1, Line 1CREATE ASSEMBLY for assembly 'sqlserverproject1' failed because assembly 'sqlserverproject1' is not authorized for PERMISSION_SET = EXTERNAL_ACCESS. The assembly is authorized when either of the following is true: the database owner (DBO) has EXTERNAL ACCESS ASSEMBLY permission and the database has the TRUSTWORTHY database property on; or the assembly is signed with a certificate or an asymmetric key that has a corresponding login with EXTERNAL ACCESS ASSEMBLY permission.
After validating that TRUSTWORTHY was ON, restarting services, checking surface configuration to ensure it was enabled, and spending an hour searching online, I found the following article.
You may receive an error message when you try to run an existing CLR object or create an assembly that has the external_access or unsafe permission set on a database that is attached or restored from a different server

"ON" TRUSTWORTHY property of the database and enable clr before following above link,

ALTER DATABASE SET TRUSTWORTHY ON
go
sp_configure 'clr enabled', 1
go
reconfigure
go

No comments:

Post a Comment