dotnet / corert

This repo contains CoreRT, an experimental .NET Core runtime optimized for AOT (ahead of time compilation) scenarios, with the accompanying compiler toolchain.
http://dot.net
MIT License
2.91k stars 508 forks source link

Does not work with System.Data.SqlClient #7679

Open genifycom opened 5 years ago

genifycom commented 5 years ago

A CoreRT built console app that uses System.Data.SqlClient does not make a connection to the database.

It reports Unable to load sni.dll

MichalStrehovsky commented 5 years ago

It reports Unable to load sni.dll

This is a similar problem as #5496 (that one hits it for clrcompression.dll). Can you try this workaround:

Publish the app as regular standalone app using dotnet publish -c Release -r win-x64 /p:NativeCompilationDuringPublish=false first, save sni.dll that got published, and then publish it again as native and copy sni.dll saved earlier next to the native .exe.

genifycom commented 5 years ago

Thank you for the response. I followed your instructions and the sni.dll reference was resolved.

However the SQL access now reports "Column requires a valid DataType'. This indicates a different problem.

If I build with /p:NativeCompilationDuringPublish=false first and then take ALL of the dlls and place them in the same directory as the Native exe, I still get the same error. But If I run the exe in the full publish directory if works without the error.

Am I able to unpack the CoreRT generated exe?

Can you suggest any other debugging steps I can take.

Thank you.

MichalStrehovsky commented 5 years ago

Can you suggest any other debugging steps I can take.

Can you try these instructions? Setting a breakpoint on the RhThrowEx method mentioned in the doc might give you more insights about the failure.

genifycom commented 5 years ago

Thanks for the response. I can now see that it is failing on this call

https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.getschema?view=netcore-2.1 Uses Assemblies: System.Data.dll, System.Data.SqlClient.dll

Cut down code is like this:

using (SqlConnection sourceConnection = new SqlConnection(_source_connection)) { sourceConnection.Open(); var source_schema = sourceConnection.GetSchema(); }

It reports "Column requires a valid DataType"

But I cannot seem to find out why.

MichalStrehovsky commented 5 years ago

Can you share the stack trace for the exception? I don't have a SQL server installed, but stack traces are usually as good as a local repro for these things.

genifycom commented 5 years ago

Not very exciting

TestCoreRT.exe!RhThrowEx() Unknown TestCoreRT.exe!RhpThrowEx() Line 191 Unknown [External Code] TestCoreRT.exe!TestCoreRT_TestCoreRT_Program__Main() Line 22 Unknown [External Code] TestCoreRT.exe!wmain(int argc, wchar_t argv) Line 395 C++ [External Code]

This is using DotNet 2.1

csproj is:

`

Exe netcoreapp2.1

` Code is:

` static void Main(string[] args) { Console.WriteLine("Hello World!");

        using (SqlConnection sourceConnection = new SqlConnection(explicit_local_data))
        {
            sourceConnection.Open();
            var source_schema = sourceConnection.GetSchema();
        }
    }

`

MichalStrehovsky commented 5 years ago

In the Debug -> Options menu of Visual Studio, can you remove the checkmark next to "Enable Just My Code" and try again?

That should show something more useful than just [External Code].

genifycom commented 5 years ago

If I just run it from the command line here is what I get

Unhandled Exception: System.ArgumentException: Column requires a valid DataType. at System.Data.DataColumn.set_DataType(Type) + 0xad at System.Data.XSDSchema.SetProperties(Object, XmlAttribute[]) + 0x25b at System.Data.XSDSchema.HandleElementColumn(XmlSchemaElement, DataTable, Boolean) + 0xb50 at System.Data.XSDSchema.HandleParticle(XmlSchemaParticle, DataTable, ArrayList, Boolean) + 0x490 at System.Data.XSDSchema.HandleComplexType(XmlSchemaComplexType, DataTable, ArrayList, Boolean) + 0x961 at System.Data.XSDSchema.InstantiateTable(XmlSchemaElement, XmlSchemaComplexType, Boolean) + 0x690 at System.Data.XSDSchema.HandleTable(XmlSchemaElement) + 0x136 at System.Data.XSDSchema.HandleDataSet(XmlSchemaElement, Boolean) + 0x788 at System.Data.XSDSchema.LoadSchema(XmlSchemaSet, DataSet) + 0xe0b at System.Data.DataSet.ReadXSDSchema(XmlReader, Boolean) + 0x304 at System.Data.DataSet.ReadXml(XmlReader, Boolean) + 0xaf9 at System.Data.DataSet.ReadXml(Stream) + 0x6d at System.Data.ProviderBase.DbMetaDataFactory.LoadDataSetFromXml(Stream) + 0x81 at System.Data.ProviderBase.DbMetaDataFactory..ctor(Stream, String, String) + 0x60 at System.Data.SqlClient.SqlMetaDataFactory..ctor(Stream, String, String) + 0x2f at System.Data.SqlClient.SqlConnectionFactory.CreateMetaDataFactory(DbConnectionInternal, Boolean&) + 0xcb at System.Data.ProviderBase.DbConnectionFactory.GetMetaDataFactory(DbConnectionPoolGroup, DbConnectionInternal) + 0x6c at System.Data.ProviderBase.DbConnectionInternal.GetSchema(DbConnectionFactory, DbConnectionPoolGroup, DbConnection, String, String[]) + 0x3b at System.Data.SqlClient.SqlConnection.GetSchema(String, String[]) + 0x84 at System.Data.SqlClient.SqlConnection.GetSchema() + 0x43 at TestCoreRT.Program.Main(String[]) + 0x93 at TestCoreRT!+0xbe9117 at TestCoreRT!+0xbe919d

genifycom commented 5 years ago

Here is the call stack without enable just my code

TestCoreRT.exe!RhThrowEx() Unknown TestCoreRT.exe!RhpThrowEx() Line 191 Unknown TestCoreRT.exe!System_Data_Common_System_Data_DataColumnset_DataType() Line 491 Unknown TestCoreRT.exe!System_Data_Common_System_Data_XSDSchemaSetProperties() Line 288 Unknown TestCoreRT.exe!System_Data_Common_System_Data_XSDSchemaHandleElementColumn() Line 2450 Unknown TestCoreRT.exe!System_Data_Common_System_Data_XSDSchema__HandleParticle() Line 936 Unknown TestCoreRT.exe!System_Data_Common_System_Data_XSDSchemaHandleComplexType() Line 1118 Unknown TestCoreRT.exe!System_Data_Common_System_Data_XSDSchemaInstantiateTable() Line 1625 Unknown TestCoreRT.exe!System_Data_Common_System_Data_XSDSchema__HandleTable() Line 2836 Unknown TestCoreRT.exe!System_Data_Common_System_Data_XSDSchemaHandleDataSet() Line 2637 Unknown TestCoreRT.exe!System_Data_Common_System_Data_XSDSchemaLoadSchema_0() Line 792 Unknown TestCoreRT.exe!System_Data_Common_System_Data_DataSetReadXSDSchema() Line 1780 Unknown TestCoreRT.exe!System_Data_Common_System_Data_DataSetReadXml_0() Line 2088 Unknown TestCoreRT.exe!System_Data_Common_System_Data_DataSetReadXml_1() Line 2250 Unknown TestCoreRT.exe!System_Data_SqlClient_System_Data_ProviderBase_DbMetaDataFactoryLoadDataSetFromXml() Unknown TestCoreRT.exe!System_Data_SqlClient_System_Data_ProviderBase_DbMetaDataFactory___ctor() Unknown TestCoreRT.exe!System_Data_SqlClient_System_Data_SqlClientSqlMetaDataFactoryctor() Unknown TestCoreRT.exe!System_Data_SqlClient_System_Data_SqlClient_SqlConnectionFactoryCreateMetaDataFactory() Unknown TestCoreRT.exe!System_Data_SqlClient_System_Data_ProviderBase_DbConnectionFactoryGetMetaDataFactory() Unknown TestCoreRT.exe!System_Data_SqlClient_System_Data_ProviderBase_DbConnectionInternalGetSchema() Unknown TestCoreRT.exe!System_Data_SqlClient_System_Data_SqlClient_SqlConnectionGetSchema_1() Unknown TestCoreRT.exe!System_Data_SqlClient_System_Data_SqlClient_SqlConnectionGetSchema() Unknown TestCoreRT.exe!TestCoreRT_TestCoreRT_ProgramMain() Line 23 Unknown TestCoreRT.exe!TestCoreRTModuleMainMethodWrapper() Unknown TestCoreRT.exe!TestCoreRTModuleStartupCodeMain() Unknown TestCoreRT.exe!wmain(int argc, wchar_t argv) Line 395 C++ [Inline Frame] TestCoreRT.exe!invoke_main() Line 90 C++ TestCoreRT.exe!__scrt_common_main_seh() Line 288 C++ kernel32.dll!00007fff69503034() Unknown ntdll.dll!00007fff6a673691() Unknown

MichalStrehovsky commented 5 years ago

Thanks! I see this is trying to do something with an XSD schema file embedded in System.Data.SqlClient. The schame refers to several types that we probably didn't compile.

Can you try adding an RD.XML file to your project with the following content:

<Directives>
    <Application>
        <Assembly Name="System.Data.Common" Dynamic="Required All" />
        <Assembly Name="System.Data.SqlClient" Dynamic="Required All" />
        <Assembly Name="System.Data" />
    </Application>
</Directives>

This is a sample of how to refer to the RD.XML from your project file:

https://github.com/dotnet/corert/blob/96ca051eb1ff58c9cf243497c3181638a38480e6/samples/MonoGame/Platformer2D.csproj#L10-L12

Not 100% sure it will help, but it's worth a shot...

genifycom commented 5 years ago

Yes! That worked for my simple test case (Just GetSchema). Thank you.

In my regular case I get this stack trace

---> System.TypeLoadException: The type 'System.String' cannot be found in assembly 'Microsoft.SqlServer.SqlEnum, Version=15.2.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'. at Internal.Reflection.Core.Execution.ExecutionDomain.<>c__DisplayClass3_0.b__0(Assembly, String) + 0x1c1 at System.Reflection.Runtime.TypeParsing.GetTypeOptions.CoreResolveType(Assembly, String) + 0x2f at Microsoft.SqlServer.Management.Smo.DataProvider.InitSchemaTable(ArrayList) + 0x19c at Microsoft.SqlServer.Management.Smo.ExecuteSql.GetDataProvider(StringCollection, Object, StatementBuilder, DataProvider.RetriveMode) + 0x70 at Microsoft.SqlServer.Management.Smo.SqlObjectBase.FillData(ResultType, StringCollection, Object, StatementBuilder) + 0x45 at Microsoft.SqlServer.Management.Smo.SqlObjectBase.FillDataWithUseFailure(SqlEnumResult, ResultType) + 0x10e at Microsoft.SqlServer.Management.Smo.SqlObjectBase.BuildResult(EnumResult) + 0x2a0 at Microsoft.SqlServer.Management.Smo.DatabaseLevel.GetData(EnumResult) + 0x6d at Microsoft.SqlServer.Management.Sdk.Sfc.Environment.GetData() + 0x74 at Microsoft.SqlServer.Management.Sdk.Sfc.Environment.GetData(Request, Object) + 0x55 at Microsoft.SqlServer.Management.Sdk.Sfc.Enumerator.GetData(Object, Request) + 0x12b at Microsoft.SqlServer.Management.Sdk.Sfc.Enumerator.Process(Object, Request) + 0x49 --- End of inner exception stack trace --- at Microsoft.SqlServer.Management.Sdk.Sfc.Enumerator.Process(Object, Request) + 0xbf at Microsoft.SqlServer.Management.Smo.ExecutionManager.GetEnumeratorData(Request) + 0x7d at Microsoft.SqlServer.Management.Smo.SqlSmoObject.GetCollationRelatedProperties(String, ContainmentType&, CatalogCollationType&) + 0x32a at Microsoft.SqlServer.Management.Smo.SqlSmoObject.GetDbComparer(Boolean) + 0xa7 at Microsoft.SqlServer.Management.Smo.SqlSmoObject.InitializeStringComparer() + 0x4c at Microsoft.SqlServer.Management.Smo.SqlSmoObject.get_StringComparer() + 0x10 at Microsoft.SqlServer.Management.Smo.SimpleObjectCollectionBase.InitInnerCollection() + 0x23 at Microsoft.SqlServer.Management.Smo.SmoCollectionBase.GetObjectByKey(ObjectKeyBase) + 0x1d at Microsoft.SqlServer.Management.Smo.DatabaseCollection.get_Item(String) + 0x44

Any ideas on how to fix that one?

Thank you for your help on this.

MichalStrehovsky commented 5 years ago

I think that error should by fixable by adding <Assembly Name="mscorlib" /> to the RD.XML file.

genifycom commented 5 years ago

Thanks Michal, I tried that but no change. Same stack trace.

ifew commented 5 years ago

A CoreRT built console app that uses System.Data.SqlClient does not make a connection to the database.

It reports Unable to load sni.dll

I try to using .NET Core 3.0.100 with ReadyToRun, it's work

please checkout here https://github.com/ifew/netcore3-readytorun-dapper