Friday, 8 June 2007

Sql Server problem when attaching db with CLR objects

If you detach and then attach again, even if on the same server, a SQLServer 2005 database that has CLR stored procedures and functions you can receive the following message:

Msg 10314, Level 16, State 11, Line 1
An error occurred in the Microsoft .NET Framework while trying to load assembly id 65538. 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 'etere.playout.sqlserver, 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)

MS says that only assemblies with UNSAFE or EXTERNAL_ACCESS are affected by the problem. The following MS knowledge base article explains the situation: http://support.microsoft.com/kb/918040,

I've tried to change the db owner as the article says, but it won't be working for me... maybe beacuse I was using TRUSTWORTHY ON. Don't know. Anyway I've tried to reissue the TRUSTWORTHY ON properties into the DB and... magic ! now works.

so if you are using the TRUSTWORTHY ON remember to include it after reattaching the db:

ALTER DATABASE databasename SET TRUSTWORTHY ON
EXEC sp_changedbowner '?????'

3 comments:

Eric said...

Postings like this really help. It did for me and solved my problem that i had for the past 24 hrs.

Keep up the good work! So glad you have this post.

Cheers

Martin Wills said...

The tip to reissue
ALTER DATABASE xxxx SET TRUSTWORTHY ON

solved my problem as well. Thank you.

It's amazing that the same tip doesn't appear on Microsoft's MSDN site.

In my case, I was just detaching the database file and attaching the same database to the same server. So Microsoft's advice about changing the owner didn't seem relevant and, indeed, didn't fix the problem.

Anonymous said...

ha, I am going to try out my thought, your post get me some good ideas, it's really awesome, thanks.

- Thomas