Environment: SQL Server 2008 R2 standard edition and VS2010, .Net Framework 3.5
I've got a table with a varbinary column that contains a serialized Hashtable. I'm writing a CLR stored procedure that, given the contents of such a field from a row in the table, does a lookup on a key in the Hashtable. and returns the element's value.
To get the Hashtable, I need to deserialize it, and it appears that the BinaryFormatter's Deserialize function requires certain permissions that I don't know how to grant.
This SQL Server instance runs on my own development machine, and was installed pretty much with all defaults. I'm the owner.
Any help on solving this? I should point out while I write a fair amount of SQL and a LOT of VB, I'm far from a SQL Server expert, so if you have suggestions I'd appreciate it if you'd spell things out clearly for me.
Thanks in advance for any help,
Tom
Here's a fragment from the VB source:
' the bytes variable is defined as Byte()
Dim ms As MemoryStream = New MemoryStream()
ms.Write(bytes, 0, bytes.Length)
ms.Seek(0, 0)
Dim formatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Dim oHT As Object = formatter.Deserialize(ms) 'hurls here
The CREATE ASSEMBLY statement had WITH PERMISSION_SET = SAFE. I tried it with UNSAFE, and got this error:
CREATE ASSEMBLY for assembly 'ddmi.deep.data.sqlserver' failed because assembly 'ddmi.deep.data.sqlserver' is not authorized for PERMISSION_SET = UNSAFE. The assembly is authorized when either of the following is true: the database owner (DBO) has
UNSAFE 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 UNSAFE ASSEMBLY permission.
Here's an SQL script that runs it:
declare
@id int
, @raw_req_bin varbinary(1500)
, @value nvarchar(max)
set @id = 327
set @raw_req_bin = (select raw_req_bin from dp_payment where transaction_id = @id)
set @value = dbo.dp_fx_hash_table_lookup(@raw_req_bin, 'first_name')
and here's what I get when I run the script:
SecurityExceptionSystem.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
at System.Security.CodeAccessSecurityEngine.SpecialDemand(PermissionType whatPermission, StackCrawlMark& stackMark)
at System.Security.CodeAccessPermission.DemandInternal(PermissionType permissionType)
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
at ddmi.deep.data.sqlserver.UserDefinedFunctions.AsHashTable(Byte[] bytes, String& phase)
The action that failed was:
Demand
The type of the first permission that failed was:
System.Security.Permissions.SecurityPermission
The Zone of the assembly that failed was:
MyComputer