lauantai 6. lokakuuta 2012

Struggling with System.IO.Packaging when creating a CLR procedure

The problem

I started to create a small CLR procedure into Sql  Server 2012 Express Edition which would utilize System.IO.Packaging namespace. In order to use this namespace, WindowsBase must be referenced. After adding the required reference everything seemed to be fine when compiling the assembly.

Now when trying to register the newly created assembly to Sql Server, I got an error message stating:

Msg 10301, Level 16, State 1, Line 1

Assembly 'Test' references assembly 'system.xaml, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089.', which is not present in the current database. SQL Server attempted to locate and automatically load the referenced assembly from the same location where referring assembly came from, but that operation has failed (reason: 2(The system cannot find the file specified.)). Please load the referenced assembly into the current database and retry your request.

Now, this was surprising, but understandable. So I added System.Xaml to the project references; After all I had no use for the visual interface in my project.

After compiling the project I re-tried to register the assembly. This time I received a different error:

Warning: The Microsoft .NET Framework assembly 'system.xaml, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089.' you are registering is not fully tested in the SQL Server hosted environment and is not supported. In the future, if you upgrade or service this assembly or the .NET Framework, your CLR integration routine may stop working. Please refer SQL Server Books Online for more details.
Msg 6218, Level 16, State 2, Line 1
CREATE ASSEMBLY for assembly 'Test' failed because assembly 'System.Xaml' failed verification. Check if the referenced assemblies are up-to-date and trusted (for external_access or unsafe) to execute in the database. CLR Verifier error messages if any will follow this message
[ : System.Windows.Markup.ValueSerializer::CanConvertToString][mdToken=0x6000002][offset 0x00000000] Code size is zero.
[ : System.Windows.Markup.ValueSerializer::CanConvertFromString][mdToken=0x6000003][offset 0x00000000] Code size is zero.
[ : System.Windows.Markup.ValueSerializer::ConvertToString][mdToken=0x6000004][offset 0x00000000] Code size is zero.
[ : System.Windows.Markup.ValueSerializer::ConvertFromString][mdToken=0x6000005][offset 0x00000000] Code size is zero.
[ : System.Windows.Markup.ValueSerializer::TypeReferences][mdToken=0x6000006][offset 0x00000000] Code size is zero.
[ : System.Windows.Markup.ValueSerializer::.ctor][mdToken=0x6000001][offset 0x00000000] Code size is zero.
[ : System.Windows.Markup.ValueSerializer::.cctor][mdToken=0x600000d][offset 0x00000000] Code size is zero.
[ : System.Windows.Markup.ValueSerializer::GetSerializerFor][mdToken=0x6000007][offset 0x00000000] Code size is zero.
[ : System.Windows.Markup.ValueSerializer::GetSerializerFor][mdToken=0x6000008][offset 0x00000000] Code size is zero.
[ : System.Windows.Markup.ValueSerializer::GetSerializerFor][mdToken=0x6000009][offset 0x00000000] Code size is zero.
[ : System.Windows.Markup.ValueSerializer::GetSerializerFor][mdToken=0x600000a][offset 0x00000000] Code size is zero.
[ : System.Windows.Markup.ValueSerializer::GetConvertToException][mdToken=0x600000b][offset 0x00000000] Code size is zero.
[ : System.Windows.Markup.ValueSerializer::GetConvertFromException][mdToken=0x600000c][offset 0x00000000] Code size is zero.
[ : System.Windows.Markup.MemberDefinition::.ctor][mdToken=0x6000010][offset 0x00000000] Code size is zero.
[ : System.Windows.Markup.PropertyDefinition::get_Name][mdToken=0x6000011][offset 0x00000000] Code size is...

So after all I wasn't able to register the assembly to Sql Server at all.

What about other versions

Well I tried the same with Sql  Server 2008 R2 with no success. The error message was different and I needed to reference accessibility assembly which wasn't verified against Sql Server.

I also tried this with Sql  Server 2005 and it was quite a big surprise that Sql  Server 2005 seems to accept my assembly.

Conclusions

So it looks like newer versions of Sql Server don't accept WindowsBase reference. While I do understand this since lot's of the classes in this assembly are used in creating an user interface I don't quite understand why some basic functionality such as System.IO.Packaging is included in the same assembly.

Another potential problem is the System.Security.RightsManagement namespace which is included in WindowsBase