I have a .Net assembly (64 bit environment) which has P/Invoke method to load an unmanaged assembly. I want to consume the method available in the .Net assembly in to the SQL stored procedures. purpose of this is for error logging of SQL realated exceptions and errors.
Below is the method in .Net assembly to include in to the SQL. MyLogger.Logger.LogInfo method is defined in the MyLogger assembly which internally has P/Invoke method to load an unmanaged assembly.
public class SQLLogger
{
[Microsoft.SqlServer.Server.SqlFunction()]
public static void LogInfoMessage(SqlString infoMessage)
{
MyLogger.Logger.LogInfo(infoMessage.ToString());
}
I built a .Net assembly and run the below query in SQL Server 2008 R2.
CREATE assembly SQLLogger from 'C:\tmp\SQLLogger.dll' WITH PERMISSION_SET = SAFE
I kept the referenced MyLogger.dll
in the same folder as well, and I get the below exception.
CREATE ASSEMBLY failed because type "MyLogger.Logger" in safe assembly "MyLogger" has a pinvokeimpl method. P/Invoke is not allowed in safe assemblies.
I tried to create assembly in sql manually using the values Unrestricted, External Access for the Permission Set and I failed to create.
I am getting the below error during the manual operation for the External Access Permission Set & Unrestricted Permission Set.
Create failed for SqlAssembly 'SQLLogger'. (Microsoft.SqlServer.Smo) ADDITIONAL INFORMATION:
An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)
Assembly 'mylogger, version=1.0.0.0, culture=neutral, publickeytoken=b8f8bb225641c4a3.' was not found in the SQL catalog. (Microsoft SQL Server, Error: 6503)
There are some relevant posts but I didn't get any solution out of it,
Calling unmanaged C/C++ DLL functions from SQL Server 2008
http://social.msdn.microsoft.com/Forums/en-US/sqlnetfx/thread/4a032c4b-1402-4c53-b34e-8c8bd724f904
Thanks for your help!!!