diff --git a/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/AppRole/AppRoleHandler.cs b/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/AppRole/AppRoleHandler.cs index e7d4f39b..3fc9d3e9 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/AppRole/AppRoleHandler.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/AppRole/AppRoleHandler.cs @@ -12,7 +12,6 @@ using Microsoft.SqlServer.Management.Common; using Microsoft.SqlServer.Management.Sdk.Sfc; using Microsoft.SqlServer.Management.Smo; using Microsoft.SqlTools.ServiceLayer.Connection; -using Microsoft.SqlTools.ServiceLayer.Connection.Contracts; using Microsoft.SqlTools.ServiceLayer.Management; using Microsoft.SqlTools.ServiceLayer.ObjectManagement.Contracts; @@ -46,27 +45,20 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement throw new ArgumentException("Invalid connection URI '{0}'", parameters.ConnectionUri); } string originalDatabaseName = originalConnInfo.ConnectionDetails.DatabaseName; + originalConnInfo.ConnectionDetails.DatabaseName = parameters.Database; + + // create a default app role data context and database object + CDataContainer dataContainer; try { - originalConnInfo.ConnectionDetails.DatabaseName = parameters.Database; - ConnectParams connectParams = new ConnectParams - { - OwnerUri = parameters.ContextId, - Connection = originalConnInfo.ConnectionDetails, - Type = Connection.ConnectionType.Default - }; - await this.ConnectionService.Connect(connectParams); + ServerConnection serverConnection = ConnectionService.OpenServerConnection(originalConnInfo, "DataContainer"); + dataContainer = CreateAppRoleDataContainer(serverConnection, null, ConfigAction.Create, parameters.Database); } finally { originalConnInfo.ConnectionDetails.DatabaseName = originalDatabaseName; } - ConnectionInfo connInfo; - this.ConnectionService.TryFindConnection(parameters.ContextId, out connInfo); - - CDataContainer dataContainer = CreateAppRoleDataContainer(connInfo, null, ConfigAction.Create, parameters.Database); - AppRolePrototype prototype = parameters.IsNewObject ? new AppRolePrototype(dataContainer, parameters.Database) : new AppRolePrototype(dataContainer, parameters.Database, dataContainer.Server.GetSmoObject(parameters.ObjectUrn) as ApplicationRole); @@ -149,14 +141,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement private string DoHandleUpdateAppRoleRequest(AppRoleViewContext context, AppRoleInfo appRoleInfo, RunType runType) { - ConnectionInfo connInfo; - this.ConnectionService.TryFindConnection(context.Parameters.ConnectionUri, out connInfo); - if (connInfo == null) - { - throw new ArgumentException("Invalid ConnectionUri"); - } - - CDataContainer dataContainer = CreateAppRoleDataContainer(connInfo, null, ConfigAction.Create, context.Parameters.Database); + CDataContainer dataContainer = CreateAppRoleDataContainer(context.Connection, null, ConfigAction.Create, context.Parameters.Database); AppRolePrototype prototype = new AppRolePrototype(dataContainer, context.Parameters.Database, dataContainer.Server.Databases[context.Parameters.Database].ApplicationRoles[appRoleInfo.Name]); prototype.ApplyInfoToPrototype(appRoleInfo); return ConfigureAppRole(dataContainer, ConfigAction.Update, runType, prototype); @@ -164,23 +149,14 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement private string DoHandleCreateAppRoleRequest(AppRoleViewContext context, AppRoleInfo appRoleInfo, RunType runType) { - ConnectionInfo connInfo; - this.ConnectionService.TryFindConnection(context.Parameters.ConnectionUri, out connInfo); - - if (connInfo == null) - { - throw new ArgumentException("Invalid ConnectionUri"); - } - - CDataContainer dataContainer = CreateAppRoleDataContainer(connInfo, null, ConfigAction.Create, context.Parameters.Database); + CDataContainer dataContainer = CreateAppRoleDataContainer(context.Connection, null, ConfigAction.Create, context.Parameters.Database); AppRolePrototype prototype = new AppRolePrototype(dataContainer, context.Parameters.Database, appRoleInfo); return ConfigureAppRole(dataContainer, ConfigAction.Create, runType, prototype); } - internal CDataContainer CreateAppRoleDataContainer(ConnectionInfo connInfo, AppRoleInfo role, ConfigAction configAction, string databaseName) + internal CDataContainer CreateAppRoleDataContainer(ServerConnection serverConnection, AppRoleInfo role, ConfigAction configAction, string databaseName) { - var serverConnection = ConnectionService.OpenServerConnection(connInfo, "DataContainer"); var connectionInfoWithConnection = new SqlConnectionInfoWithConnection(); connectionInfoWithConnection.ServerConnection = serverConnection; diff --git a/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/DatabaseRole/DatabaseRoleHandler.cs b/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/DatabaseRole/DatabaseRoleHandler.cs index 0214757a..28519cb9 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/DatabaseRole/DatabaseRoleHandler.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/ObjectManagement/ObjectTypes/DatabaseRole/DatabaseRoleHandler.cs @@ -12,7 +12,6 @@ using Microsoft.SqlServer.Management.Common; using Microsoft.SqlServer.Management.Sdk.Sfc; using Microsoft.SqlServer.Management.Smo; using Microsoft.SqlTools.ServiceLayer.Connection; -using Microsoft.SqlTools.ServiceLayer.Connection.Contracts; using Microsoft.SqlTools.ServiceLayer.Management; using Microsoft.SqlTools.ServiceLayer.ObjectManagement.Contracts; @@ -46,27 +45,20 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement throw new ArgumentException("Invalid connection URI '{0}'", parameters.ConnectionUri); } string originalDatabaseName = originalConnInfo.ConnectionDetails.DatabaseName; + originalConnInfo.ConnectionDetails.DatabaseName = parameters.Database; + + // create a default database role data context and database object + CDataContainer dataContainer; try { - originalConnInfo.ConnectionDetails.DatabaseName = parameters.Database; - ConnectParams connectParams = new ConnectParams - { - OwnerUri = parameters.ContextId, - Connection = originalConnInfo.ConnectionDetails, - Type = Connection.ConnectionType.Default - }; - await this.ConnectionService.Connect(connectParams); + ServerConnection serverConnection = ConnectionService.OpenServerConnection(originalConnInfo, "DataContainer"); + dataContainer = CreateDatabaseRoleDataContainer(serverConnection, null, ConfigAction.Create, parameters.Database); } finally { originalConnInfo.ConnectionDetails.DatabaseName = originalDatabaseName; } - ConnectionInfo connInfo; - this.ConnectionService.TryFindConnection(parameters.ContextId, out connInfo); - - CDataContainer dataContainer = CreateDatabaseRoleDataContainer(connInfo, null, ConfigAction.Create, parameters.Database); - DatabaseRolePrototype prototype = parameters.IsNewObject ? new DatabaseRolePrototype(dataContainer, parameters.Database) : new DatabaseRolePrototype(dataContainer, parameters.Database, dataContainer.Server.GetSmoObject(parameters.ObjectUrn) as DatabaseRole); @@ -148,14 +140,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement private string DoHandleUpdateDatabaseRoleRequest(DatabaseRoleViewContext context, DatabaseRoleInfo DatabaseRoleInfo, RunType runType) { - ConnectionInfo connInfo; - this.ConnectionService.TryFindConnection(context.Parameters.ConnectionUri, out connInfo); - if (connInfo == null) - { - throw new ArgumentException("Invalid ConnectionUri"); - } - - CDataContainer dataContainer = CreateDatabaseRoleDataContainer(connInfo, null, ConfigAction.Create, context.Parameters.Database); + CDataContainer dataContainer = CreateDatabaseRoleDataContainer(context.Connection, null, ConfigAction.Create, context.Parameters.Database); DatabaseRolePrototype prototype = new DatabaseRolePrototype(dataContainer, context.Parameters.Database, dataContainer.Server.Databases[context.Parameters.Database].Roles[DatabaseRoleInfo.Name]); prototype.ApplyInfoToPrototype(DatabaseRoleInfo); return ConfigureDatabaseRole(dataContainer, ConfigAction.Update, runType, prototype); @@ -163,23 +148,14 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectManagement private string DoHandleCreateDatabaseRoleRequest(DatabaseRoleViewContext context, DatabaseRoleInfo DatabaseRoleInfo, RunType runType) { - ConnectionInfo connInfo; - this.ConnectionService.TryFindConnection(context.Parameters.ConnectionUri, out connInfo); - - if (connInfo == null) - { - throw new ArgumentException("Invalid ConnectionUri"); - } - - CDataContainer dataContainer = CreateDatabaseRoleDataContainer(connInfo, null, ConfigAction.Create, context.Parameters.Database); + CDataContainer dataContainer = CreateDatabaseRoleDataContainer(context.Connection, null, ConfigAction.Create, context.Parameters.Database); DatabaseRolePrototype prototype = new DatabaseRolePrototype(dataContainer, context.Parameters.Database, DatabaseRoleInfo); return ConfigureDatabaseRole(dataContainer, ConfigAction.Create, runType, prototype); } - internal CDataContainer CreateDatabaseRoleDataContainer(ConnectionInfo connInfo, DatabaseRoleInfo role, ConfigAction configAction, string databaseName) + internal CDataContainer CreateDatabaseRoleDataContainer(ServerConnection serverConnection, DatabaseRoleInfo role, ConfigAction configAction, string databaseName) { - var serverConnection = ConnectionService.OpenServerConnection(connInfo, "DataContainer"); var connectionInfoWithConnection = new SqlConnectionInfoWithConnection(); connectionInfoWithConnection.ServerConnection = serverConnection;