This project has moved. For the latest updates, please go here.
2

Closed

Problem with CreateEngine and SQL?

description

Hi, I'm trying to instantiate a python engine in a SQL stored procedure i'm coding through VS2008. However, I continue to receive this error message when the code reaches the asterisked line of the code below:
 
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
/Other imports here..../
/*
  • Python specific
  • */
    using IronPython;
    using IronPython.Hosting;
    using Microsoft.Scripting;
    using Microsoft.Scripting.Hosting;
     
     
    public partial class SqlProcedures
    {
    [Microsoft.SqlServer.Server.SqlProcedure()]
    public static void BrillTagger(SqlString Phrase, out SqlString outPhrase)
    {
    PROBLEM ScriptEngine pyEngine=pyEngine = Python.CreateEngine(runtime.);
    }
     
    The error message I receive is below. Any ideas? Thanks =D
    Type 'IronPython.Runtime.PythonContext' doesn't provide a suitable public constructor or its implementation is faulty: The type initializer for 'IronPython.Runtime.SysModule' threw an exception.
    at Microsoft.Scripting.Utils.ReflectionUtils.CreateInstance[T](Type actualType, Object[] args)
    at Microsoft.Scripting.Runtime.LanguageConfiguration.LoadLanguageContext(ScriptDomainManager domainManager, Boolean& alreadyLoaded)
    at Microsoft.Scripting.Runtime.DlrConfiguration.LoadLanguageContext(ScriptDomainManager manager, LanguageConfiguration config)
    at Microsoft.Scripting.Runtime.DlrConfiguration.TryLoadLanguage(ScriptDomainManager manager, String str, Boolean isExtension, LanguageContext& language)
    at Microsoft.Scripting.Runtime.ScriptDomainManager.TryGetLanguage(String languageName, LanguageContext& language)
    at Microsoft.Scripting.Hosting.ScriptRuntime.TryGetEngine(String languageName, ScriptEngine& engine)
    at Microsoft.Scripting.Hosting.ScriptRuntime.GetEngine(String languageName)
    at SqlProcedures.BrillTagger(SqlString Phrase, SqlString& outPhrase)
Closed Feb 7, 2011 at 9:13 AM by jdhardy
SQL Server does not support dynamic assembly loading (http://blogs.msdn.com/b/sqlclr/archive/2006/10/18/dynamic-assembly-loading.aspx), so this is unfixable from our end.

comments

ants1591 wrote Mar 23, 2009 at 3:41 AM

Sorry the problem line should read:
PROBLEM ScriptEngine pyEngine=pyEngine = Python.CreateEngine();
I was a little click-happy with my cut and paste :)

dinov wrote Mar 24, 2009 at 2:05 AM

Can you attach the debugger to SQL and see what exception is being thrown right before the module about SysModule's type init?

Likely we are having problems loading an assembly or referring to a type but it's hard to know exactly what's wrong.

ants1591 wrote Mar 24, 2009 at 2:29 PM

The full message from attaching SQL reads as follows. Thanks guys!

Microsoft.Scripting.InvalidImplementationException: Type 'IronPython.Runtime.PythonContext' doesn't provide a suitable public constructor or its implementation is faulty: The type initializer for 'IronPython.Runtime.SysModule' threw an exception. ---> System.TypeInitializationException: The type initializer for 'IronPython.Runtime.SysModule' threw an exception. ---> System.ArgumentException: The path is not of a legal form.
System.ArgumentException:
at System.IO.Path.NormalizePathFast(String path, Boolean fullCheck)
at System.IO.Path.NormalizePath(String path, Boolean fullCheck)
at System.IO.Path.GetDirectoryName(String path)
at IronPython.Runtime.SysModule..cctor()
System.TypeInitializationException:
at IronPython.Runtime.SysModule.PerformModuleReload(PythonContext context, IAttributesCollection dict)
at IronPython.Runtime.PythonContext.InitializeSystemState()
at IronPython.Runtime.PythonContext..ctor(ScriptDomainManager manager, IDictionary`2 options)
Microsoft.Scripting.InvalidImplementationException:
at Microsoft.Scripting.Utils.ReflectionUtils.CreateInstance[T](Type actualType, Object[] args)
at Microsoft.Scripting.Runtime.LanguageConfiguration.LoadLanguageContext(ScriptDomainManager domainManager, Boolean& alreadyLoaded)
at Microsoft.Scripting.Runtime.DlrConfiguration.LoadLanguageContext(ScriptDomainManager manager, LanguageConfiguration config)
at Microsoft.Scripting.Runtime.DlrConfiguration.TryLoadLanguage(ScriptDomainManager manager, AssemblyQualifiedTypeName providerName, LanguageContext& language)
at Microsoft.Scripting.Runtime.ScriptDomainManager.GetLanguageByTypeName(String providerAssemblyQualifiedTypeName)
at Microsoft.Scripting.Hosting.ScriptRuntime.GetEngineByTypeName(String assemblyQualifiedTypeName)
at IronPython.Hosting.Python.GetEngine(ScriptRuntime runtime)
at IronPython.Hosting.Python.CreateEngine()

wrote Apr 13, 2009 at 7:50 PM

wrote Apr 19, 2009 at 12:47 PM

rodrigobarnes wrote Apr 19, 2009 at 12:48 PM

I get the same issue when deploying a CLR function in c# that needs to run some ironpython code.

the exception I get in SQL server is:

A .NET Framework error occurred during execution of user defined routine or aggregate 'TestReport':
Microsoft.Scripting.InvalidImplementationException: Type 'IronPython.Runtime.PythonContext' doesn't provide a suitable public constructor or its implementation is faulty: The type initializer for 'IronPython.Runtime.SysModule' threw an exception. ---> System.TypeInitializationException: The type initializer for 'IronPython.Runtime.SysModule' threw an exception. ---> System.ArgumentException: The path is not of a legal form.
System.ArgumentException:
at System.IO.Path.NormalizePathFast(String path, Boolean fullCheck)
at System.IO.Path.NormalizePath(String path, Boolean fullCheck)
at System.IO.Path.GetDirectoryName(String path)
at IronPython.Runtime.SysModule..cctor()
System.TypeInitializationException:
at IronPython.Runtime.SysModule.PerformModuleReload(PythonContext context, IAttributesCollection dict)
at IronPython.Runtime.PythonContext.InitializeSystemState()
at IronPython.Runtime.PythonContext..ctor(ScriptDomain
...
Microsoft.Scripting.InvalidImplementationException:
at Microsoft.Scripting.Utils.ReflectionUtils.CreateInstance[T](Type actualType, Object[] args)
at Microsoft.Scripting.Runtime.LanguageConfiguration.LoadLanguageContext(ScriptDomainManager domainManager, Boolean& alreadyLoaded)
at Microsoft.Scripting.Runtime.DlrConfiguration.LoadLanguageContext(ScriptDomainManager manager, LanguageConfiguration config)
at Microsoft.Scripting.Runtime.DlrConfiguration.TryLoadLanguage(ScriptDomainManager manager, AssemblyQualifiedTypeName providerName, LanguageContext& language)
at Microsoft.Scripting.Runtime.ScriptDomainManager.GetLanguageByTypeName(String providerAssemblyQualifiedTypeName)
at Microsoft.Scripting.Hosting.ScriptRuntime.GetEngineByTypeName(String assemblyQualifiedTypeName)
at IronPython.Hosting.Python.GetEngine(ScriptRuntime runtime)
at IronPython.Hosting.Python.CreateEngine()
at RttPlugin.ScriptingHelper`1.CreateEngineWithRequ...

rodrigobarnes wrote Apr 20, 2009 at 11:22 AM

I got this to work by analogy with Silverlight. In the source, there's an IFDEF
    static SysModule() {

if SILVERLIGHT

        prefix = String.Empty;

else

        try {
            prefix = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        } catch (SecurityException) {
            prefix = String.Empty;
        }

endif

    }
This appears to be the source of the problem. An exception other than SecurityException is thrown as assemblies deployed in SQL Server don't have a 'path'. Arguably running in SQL Server can be a bit like the the Silverlight sandbox since the DLLs are stored in the database and you don't have normal access to the file system.

If you replace the section with prefix = String.Empty and recompile IronPython, then the CLR function can instantiate the PythonEngine and the script runs (for me!). Of course, I may now run into problems with using the sys module.

ants1591 wrote Jul 4, 2009 at 5:58 AM

Thanks Rodrigo,
I moved away from this project for a little while, but will want to try your solution.

wrote Sep 24, 2009 at 6:29 PM

wrote Mar 23, 2010 at 7:27 PM

wrote Feb 7, 2011 at 9:13 AM

wrote Feb 22, 2013 at 2:13 AM

wrote May 16, 2013 at 1:05 PM