Recently, we have troubleshoot a customer issue related to SQL CLR. The problem is that the assembly is configured to use external_access but he kept getting an exception similar to this (this is a sample call stack):
The issue turns out to be that customer has coded in a way that the object has a private constructor. They use third party code to dynamically create the type using Activator.CreateInstance. This requires fully trusted code. http://msdn.microsoft.com/en-us/library/he47tyc4.aspx has documenation on this.
From SQL CLR perspective, the solution is to use unsafe assembly or stop using private constructor.
The following code will reproduce it if you set the assembly to be safe or external_access. Unsafe will work.
using System;using System.Data;using System.Data.SqlClient;using System.Data.SqlTypes;using Microsoft.SqlServer.Server;using System.Reflection;
public partial class StoredProcedures{ [Microsoft.SqlServer.Server.SqlProcedure] public static void sp_test() {
object obj = Activator.CreateInstance(typeof(Test), true);
}
public class Test{
private Test() {
}}
};
-------------------------------------------------------------------------------------------------
Jack Li | Senior Escalation Engineer | Microsoft SQL Server Support