Thursday, July 14, 2005

SQLCLR and Events

I'm still trying to figure out all of the ramifications of creating Assemblies in SQL 2005 that are marked Unsafe. Seems like the easiest way to get things done, but that's probably not the best way to think of the "Unsafe" permission set.

One of my libraries that I attempted to import today had some events in the object model, and SQL Server didn't like that (I was trying to mark it as a Safe Assembly) because the add_xxx method that the compiler tacks on for events is attributed as Synchronized, and apparently, explicit synchronization is bad, or otherwise unsafe, as far as SQL Server is concerned.

In my case, the events were simply part of an Observer pattern, and I was looking for a callback mechanism to alert the parent when something particular occurred in the child. I managed to get the code working using straight delegates (i.e., only 1 "event" handler would be allowed), and then SQL Server let me create the Assembly and mark it as Safe.

I'm probably making this more complicated than it needs to be, but I can't tell what's my fault and what is the fault of the beta software.