Repository navigation
Inject Application Name to SQL Server connection string when not set #36548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Concurrent; | ||
| using System.Runtime.InteropServices; | ||
|
roji marked this conversation as resolved.
Resolved
|
||
| using Microsoft.Data.SqlClient; | ||
|
|
||
| namespace Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal; | ||
|
|
@@ -19,6 +20,15 @@ public class SqlServerConnection : RelationalConnection, ISqlServerConnection | |
|
|
||
| private static readonly ConcurrentDictionary<string, bool> MultipleActiveResultSetsEnabledMap = new(); | ||
|
|
||
| /// <summary> | ||
| /// Maps a user-provided connection string to a possibly transformed connection string that will actually be used | ||
| /// to connect. By default, if the user-provided connection string doesn't set Application Name, we set it to contain | ||
| /// information about EF Core and the version being used. | ||
| /// </summary> | ||
| private static readonly ConcurrentDictionary<string, string> ConnectionStringMap = new(); | ||
|
|
||
| private static string? _defaultApplicationName; | ||
|
|
||
| /// <summary> | ||
| /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
| /// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
|
|
@@ -28,6 +38,29 @@ public class SqlServerConnection : RelationalConnection, ISqlServerConnection | |
| public SqlServerConnection(RelationalConnectionDependencies dependencies) | ||
| : base(dependencies) | ||
| { | ||
| // If EF was configured with a connection string that doesn't contain Application Name, rewrite it to inject | ||
| // Application Name with EF and versioning info. | ||
| // Note that we don't do anything if EF was configured with a SqlConnection, as that could reset the | ||
| // password because of Persist Security Info=true | ||
| var relationalOptions = RelationalOptionsExtension.Extract(dependencies.ContextOptions); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't love that we need to extract the options here again (or the smelly assignment from ConnectionString to ConnectionString), but RelationalConnection works hard to prevent implementations from ever knowing if it was configured with a connection string or connection and generally to implement this kind of feature. I'd propose some refactorings here, but this is temporary throwaway code, since SqlClient will be including a first-class user-agent-like feature anyway - so as a stop-gap this seems ok... |
||
|
|
||
| if (!string.IsNullOrWhiteSpace(relationalOptions.ConnectionString)) | ||
| { | ||
| Check.DebugAssert(relationalOptions.Connection is null); | ||
|
|
||
| // In the base constructor, the connection string is set by assigning directly to _connectionString | ||
| // (possibly first going through ConnectionStringResolver). | ||
| // Here we re-assign the same via the ConnectionString property, which does the rewriting. | ||
| var userProvidedConnectionString = ConnectionString; | ||
| ConnectionString = userProvidedConnectionString; | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public override string? ConnectionString | ||
| { | ||
| get => base.ConnectionString; | ||
| set => base.ConnectionString = value is null ? null : ConnectionStringMap.GetOrAdd(value, ModifyConnectionString); | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -120,4 +153,45 @@ public virtual bool IsMultipleActiveResultSetsEnabled | |
| /// </summary> | ||
| protected override bool SupportsAmbientTransactions | ||
| => true; | ||
|
|
||
| /// <summary> | ||
| /// Modifies the user-provided connection string. By default, adds an Application Name to the connection | ||
| /// string containing EF Core and versioning information. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
| /// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
| /// any release. You should only use it directly in your code with extreme caution and knowing that | ||
| /// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
| /// </remarks> | ||
| [EntityFrameworkInternal] | ||
| protected virtual string ModifyConnectionString(string userProvidedConnectionString) | ||
| { | ||
| try | ||
| { | ||
| var connectionStringBuilder = new SqlConnectionStringBuilder(userProvidedConnectionString); | ||
|
|
||
| // SqlClient sends "Core Microsoft SqlClient Data Provider" as the default Application Name when unset; | ||
| // detect that and overwrite. | ||
| if (connectionStringBuilder.ApplicationName is "Core Microsoft SqlClient Data Provider" or "" or null) | ||
| { | ||
| var efVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion; | ||
|
AndriySvyryd marked this conversation as resolved.
Resolved
|
||
|
|
||
|
roji marked this conversation as resolved.
Resolved
|
||
| _defaultApplicationName ??= | ||
| $"EFCore/{efVersion} ({RuntimeInformation.OSDescription} {RuntimeInformation.OSArchitecture})"; | ||
|
|
||
| connectionStringBuilder.ApplicationName = _defaultApplicationName; | ||
|
|
||
| return connectionStringBuilder.ToString(); | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| // If anything goes wrong, simply don't modify the connection string. | ||
| // There are some scenarios where an invalid string is provided and an exception isn't expected at this phase | ||
| // (see e.g. test GetContextInfo_does_not_throw_if_DbConnection_cannot_be_created) | ||
| } | ||
|
|
||
| return userProvidedConnectionString; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.