I'm managing an old ASP.NET MVC application built with .NET 4.6.2 and Entity Framework (yes I know it should be updated). Due to changes in the way it and its database is hosted in Azure, I've been tasked to change the SQL provider from System.Data.SqlClient
to Microsoft.Data.SqlClient
. I have updated and installed a bunch of NuGet packages as well as made configuration changes.
This all seem to work fine when I run it locally. However, when deployed to Azure, it does not work. And what seems strange to me is that I can see in the stack trace of the exception that System.Data.SqlClient
is still used. When I provoke the application to get the same exception locally, I see that Microsoft.Data.SqlClient
is used as expected.
How can that be? That my local environment and Azure is using different packages when the code is the same?
The only thing that I know I different between the two is the connection string.
The exception occurs when connection string is parsed. These are the last lines in the stack trace:
Locally:
[ArgumentException: Invalid value for key 'Authentication'.]
Microsoft.Data.Common.DbConnectionStringBuilderUtil.ConvertToAuthenticationType(String keyword, Object value) +393
Microsoft.Data.SqlClient.SqlConnectionString.ConvertValueToAuthenticationType() +109
Microsoft.Data.SqlClient.SqlConnectionString..ctor(String connectionString) +2303
Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) +38
Microsoft.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) +217
Microsoft.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key) +53
Microsoft.Data.SqlClient.SqlConnection.set_ConnectionString(String value) +374
System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) +98
System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.SetConnectionString(DbConnection connection, DbConnectionPropertyInterceptionContext`1 interceptionContext) +514
On Azure:
[ArgumentException: Invalid value for key 'Authentication'.]
System.Data.Common.DbConnectionStringBuilderUtil.ConvertToAuthenticationType(String keyword, Object value) +412
System.Data.SqlClient.SqlConnectionString.ConvertValueToAuthenticationType() +118
System.Data.SqlClient.SqlConnectionString..ctor(String connectionString) +3042
System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) +38
System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) +217
System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key) +68
System.Data.SqlClient.SqlConnection.set_ConnectionString(String value) +167
System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) +98
System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.SetConnectionString(DbConnection connection, DbConnectionPropertyInterceptionContext`1 interceptionContext) +494
I believe the reason for the exception is that the new provider must be used for the connection string to be working in Azure. But my question is why is the old provider used in Azure?
I'm managing an old ASP.NET MVC application built with .NET 4.6.2 and Entity Framework (yes I know it should be updated). Due to changes in the way it and its database is hosted in Azure, I've been tasked to change the SQL provider from System.Data.SqlClient
to Microsoft.Data.SqlClient
. I have updated and installed a bunch of NuGet packages as well as made configuration changes.
This all seem to work fine when I run it locally. However, when deployed to Azure, it does not work. And what seems strange to me is that I can see in the stack trace of the exception that System.Data.SqlClient
is still used. When I provoke the application to get the same exception locally, I see that Microsoft.Data.SqlClient
is used as expected.
How can that be? That my local environment and Azure is using different packages when the code is the same?
The only thing that I know I different between the two is the connection string.
The exception occurs when connection string is parsed. These are the last lines in the stack trace:
Locally:
[ArgumentException: Invalid value for key 'Authentication'.]
Microsoft.Data.Common.DbConnectionStringBuilderUtil.ConvertToAuthenticationType(String keyword, Object value) +393
Microsoft.Data.SqlClient.SqlConnectionString.ConvertValueToAuthenticationType() +109
Microsoft.Data.SqlClient.SqlConnectionString..ctor(String connectionString) +2303
Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) +38
Microsoft.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) +217
Microsoft.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key) +53
Microsoft.Data.SqlClient.SqlConnection.set_ConnectionString(String value) +374
System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) +98
System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.SetConnectionString(DbConnection connection, DbConnectionPropertyInterceptionContext`1 interceptionContext) +514
On Azure:
[ArgumentException: Invalid value for key 'Authentication'.]
System.Data.Common.DbConnectionStringBuilderUtil.ConvertToAuthenticationType(String keyword, Object value) +412
System.Data.SqlClient.SqlConnectionString.ConvertValueToAuthenticationType() +118
System.Data.SqlClient.SqlConnectionString..ctor(String connectionString) +3042
System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) +38
System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) +217
System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key) +68
System.Data.SqlClient.SqlConnection.set_ConnectionString(String value) +167
System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) +98
System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.SetConnectionString(DbConnection connection, DbConnectionPropertyInterceptionContext`1 interceptionContext) +494
I believe the reason for the exception is that the new provider must be used for the connection string to be working in Azure. But my question is why is the old provider used in Azure?
Share Improve this question edited Jan 29 at 17:50 marc_s 757k184 gold badges1.4k silver badges1.5k bronze badges asked Jan 29 at 14:06 haagelhaagel 2,72810 gold badges38 silver badges56 bronze badges 5 |2 Answers
Reset to default 1I encountered the same issue while using the default connection strings. It turns out that the provider name is overridden by default.
The solution that worked for me was to create a custom DbConfiguration
and register it in Application_Start()
:
DbConfiguration.SetConfiguration(new AppServiceConfiguration());
Then, define the custom configuration class:
public class AppServiceConfiguration : MicrosoftSqlDbConfiguration
{
public AppServiceConfiguration()
{
SetProviderFactory("System.Data.SqlClient", Microsoft.Data.SqlClient.SqlClientFactory.Instance);
SetProviderServices("System.Data.SqlClient", MicrosoftSqlProviderServices.Instance);
SetExecutionStrategy("System.Data.SqlClient", () => new MicrosoftSqlAzureExecutionStrategy());
}
}
For more details, refer to the known issues section:
Microsoft.EntityFramework.SqlServer - Known Issues
This should ensure that the correct provider is used consistently.
When you deploy code to azure, were all existing files/ddl are cleared? it could be that old dlls still on the app service (not cleaned).
Also check the zip or folder of build/published package locally to see if both System.Data and Microsoft.Data dll are actually both there.
Regarding The exception occurs when connection string is parsed
, probably you mean `not parsed' so gives exception above? If local uses Microsoft.Data, the app service should too. check the difference in debug and release folder to see if all dlls are same.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745293108a4620980.html
System.Data.SqlClient
. It shouldn't be usingMicrosoft.Data.SqlClient
at all. How did you addMicrosoft.Data.SqlClient
in the first place and how did you force EF to use it - if it did? The error complains about a bad connection string key. The stack shows an attempt to read and parse that bad string, not actually use the library, IfSystem.Data.Entity.Infrastructure.Interception.InternalDispatcher
1.Dispatch` tries to find the target type by name, it's only chance (or rather, accident) that used MDS – Panagiotis Kanavos Commented Jan 29 at 14:30EntityFramework
which in turn depends onSystem.Data.SqlClient
. If your application (or the Azure framework you use) tries to load eg SqlConnection dynamically, it could end up loading the System.Data.SqlClient class – Panagiotis Kanavos Commented Feb 3 at 8:11System.Data.SqlClient
from publishing. That won't help if the Azure runtime includes it though. – Panagiotis Kanavos Commented Feb 3 at 8:16