diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj index 8099e9ff71..227ef211c8 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj @@ -871,7 +871,6 @@ - diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Common/GreenMethods.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Common/GreenMethods.cs deleted file mode 100644 index 43099a0bff..0000000000 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Common/GreenMethods.cs +++ /dev/null @@ -1,57 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Reflection; -using Microsoft.Data.SqlClient; - -namespace Microsoft.Data.Common -{ - internal static class GreenMethods - { - - private const string ExtensionAssemblyRef = "System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=" + AssemblyRef.EcmaPublicKey; - - // For performance, we should convert these calls to using DynamicMethod with a Delegate, or - // even better, friend assemblies if its possible; so far there's only one of these per - // AppDomain, so we're OK. - - //------------------------------------------------------------------------------ - // Access to the DbProviderServices type - private const string SystemDataCommonDbProviderServices_TypeName = "System.Data.Common.DbProviderServices, " + ExtensionAssemblyRef; - internal static Type SystemDataCommonDbProviderServices_Type = Type.GetType(SystemDataCommonDbProviderServices_TypeName, false); - - //------------------------------------------------------------------------------ - // Access to the SqlProviderServices class singleton instance; - private const string MicrosoftDataSqlClientSqlProviderServices_TypeName = "Microsoft.Data.SqlClient.SQLProviderServices, " + ExtensionAssemblyRef; - private static FieldInfo MicrosoftDataSqlClientSqlProviderServices_Instance_FieldInfo; - - internal static object MicrosoftDataSqlClientSqlProviderServices_Instance() - { - if (MicrosoftDataSqlClientSqlProviderServices_Instance_FieldInfo == null) - { - Type t = Type.GetType(MicrosoftDataSqlClientSqlProviderServices_TypeName, false); - - if (t != null) - { - MicrosoftDataSqlClientSqlProviderServices_Instance_FieldInfo = t.GetField("Instance", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static); - } - } - object result = MicrosoftDataSqlClientSqlProviderServices_Instance_GetValue(); - return result; - } - - [System.Security.Permissions.ReflectionPermission(System.Security.Permissions.SecurityAction.Assert, MemberAccess = true)] - private static object MicrosoftDataSqlClientSqlProviderServices_Instance_GetValue() - { - object result = null; - if (MicrosoftDataSqlClientSqlProviderServices_Instance_FieldInfo != null) - { - result = MicrosoftDataSqlClientSqlProviderServices_Instance_FieldInfo.GetValue(null); - } - return result; - } - - } -} diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientFactory.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientFactory.cs index 4e1030f0d6..ca78a6d5b6 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientFactory.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientFactory.cs @@ -2,28 +2,72 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Data.Common; using Microsoft.Data.Sql; + +#if NETFRAMEWORK using System; -using System.Data.Common; +using System.Reflection; using System.Security.Permissions; using System.Security; -using Microsoft.Data.Common; +#endif namespace Microsoft.Data.SqlClient { /// + #if NETFRAMEWORK + public sealed class SqlClientFactory : DbProviderFactory, IServiceProvider + #else public sealed class SqlClientFactory : DbProviderFactory -#if NETFRAMEWORK - , IServiceProvider -#endif + #endif { + #if NETFRAMEWORK + #region Constants / Member Variables + + private const string ExtensionAssemblyRef = + "System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=" + AssemblyRef.EcmaPublicKey; + + private const string MicrosoftDataSqlClientSqlProviderServicesTypeName = + "Microsoft.Data.SqlClient.SQLProviderServices, " + ExtensionAssemblyRef; + + private const string SystemDataCommonDbProviderServicesTypeName = + "System.Data.Common.DbProviderServices, " + ExtensionAssemblyRef; + private static readonly Lazy MicrosoftDataSqlClientProviderServicesInstance = + new(static () => + { + FieldInfo instanceFieldInfo = MicrosoftDataSqlClientSqlProviderServicesType.Value?.GetField( + "Instance", + BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static); + return instanceFieldInfo?.GetValue(null); + }); + + private static readonly Lazy MicrosoftDataSqlClientSqlProviderServicesType = + new (static () => Type.GetType(MicrosoftDataSqlClientSqlProviderServicesTypeName, throwOnError: false)); + + private static readonly Lazy SystemDataCommonDbProviderServicesType = + new(static () => Type.GetType(SystemDataCommonDbProviderServicesTypeName, throwOnError: false)); + + #endregion + #endif + /// public static readonly SqlClientFactory Instance = new SqlClientFactory(); private SqlClientFactory() { } + + #if NET + /// + public override bool CanCreateBatch => true; + + /// + public override DbBatch CreateBatch() => new SqlBatch(); + + /// + public override DbBatchCommand CreateBatchCommand() => new SqlBatchCommand(); + #endif /// public override bool CanCreateDataSourceEnumerator => true; @@ -64,7 +108,7 @@ public override DbParameter CreateParameter() return new SqlParameter(); } -#if NETFRAMEWORK + #if NETFRAMEWORK /// public override CodeAccessPermission CreatePermission(PermissionState state) { @@ -72,37 +116,20 @@ public override CodeAccessPermission CreatePermission(PermissionState state) } /// - /// Extension mechanism for additional services; currently the only service - /// supported is the DbProviderServices + /// Extension mechanism for additional services; currently the only service supported is + /// the System.Data.Common.DbProviderServices type. /// - /// requested service provider or null. - object IServiceProvider.GetService(Type serviceType) - { - object result = null; - if (serviceType == GreenMethods.SystemDataCommonDbProviderServices_Type) - { - result = GreenMethods.MicrosoftDataSqlClientSqlProviderServices_Instance(); - } - return result; - } -#endif + /// Requested service provider or null. + object IServiceProvider.GetService(Type serviceType) => + serviceType == SystemDataCommonDbProviderServicesType.Value + ? MicrosoftDataSqlClientProviderServicesInstance.Value + : null; + #endif /// public override DbDataSourceEnumerator CreateDataSourceEnumerator() { return SqlDataSourceEnumerator.Instance; } - -#if NET - /// - public override bool CanCreateBatch => true; - - /// - public override DbBatch CreateBatch() => new SqlBatch(); - - /// - public override DbBatchCommand CreateBatchCommand() => new SqlBatchCommand(); -#endif - } }