1

I was tasked with writing a CLR function that would mass send emails using Mandrill.Net. After a lot of hurdle with assemblies and other issues I managed to patch most of the function. The only thing left was to add sending those emails (I already had code for that). Unfortunately after I added the code I got this error:

Dynamic operations can only be performed in homogenous AppDomain

As it turns out Mandrill.Net (and RestSharp which is used under the hood) makes extensive use of dynamic types. Obviously I did search for solutions for this problem and most of what I found looked like this:

<NetFx40_LegacySecurityPolicy enabled="true"/>
<trust legacyCasModel="false" level="Full" />

That is adding those two lines to the config file of the project. I applied the solution to the app.config file, but nothing changed and I still get the error (yes, I recreated the assembly and function in Sql Server).

Truth to be told I couldn't find anything about this problem in the Sql Server CLR context, so it occurred to me that that it might actually be impossible to use those types in that context (like SQL Server is in control of the policy that prevents the use of dynamic types and not my own library). Can those dynamic types actually be used in CLR functions in SQL Server?

P.S. In my code I'm also using Dapper. In an earlier stage I also got this error when I explicitly used dynamic type to store results of a query (in fact Dapper returns those as IEnumerable<dynamic>). However after adding a Linq Cast<Dictionary<string,object>>() the error went away (though Dapper was still likely operating on the dynamic type).

Community
  • 1
  • 1
jahu
  • 5,427
  • 3
  • 37
  • 64
  • It's likely that SQL Server ignores your custom configuration file (unlike ASP.NET or the regular .NET loader). For testing purposes, could you add these settings (incidentally, I think you mean `legacyCasModel="true"`) to the `sqlservr.exe.config` file in the same directory as your `sqlservr.exe`? SQL Server restart likely required. Be careful even if this works, as I'm not sure what the full implications of this move are (as obviously this will be global to all assemblies loaded in SQL Server). – Jeroen Mostert Feb 13 '15 at 11:27

1 Answers1

0

Based on the answer I got here, likely I cannot use dynamic types in CLR or at least not to the extent that can allow me to use Mandrill.Net.

My guess is, I can actually use dynamic types, as long as I keep it to a single namespace. Dapper can use dynamic types internally and I can use dapper in my CLR, but I cannot have dapper pass dynamic types back to my CLR function\procedure (unless I cast them to something less dynamic).

On the other hand Mandrill.Net passes dynamic types to RestSharp which is a no-no accoarding to CAS Policies(?).

jahu
  • 5,427
  • 3
  • 37
  • 64