diff --git a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/ObjectExplorerService.cs b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/ObjectExplorerService.cs index 4b378392..d2e79f81 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/ObjectExplorerService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/ObjectExplorerService.cs @@ -268,6 +268,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer private void RunCreateSessionTask(ConnectionDetails connectionDetails, string uri) { + Logger.Write(LogLevel.Normal, "Creating OE session"); CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); if (connectionDetails != null && !string.IsNullOrEmpty(uri)) { @@ -278,7 +279,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer ObjectExplorerTaskResult result = await RunTaskWithTimeout(task, settings?.CreateSessionTimeout ?? ObjectExplorerSettings.DefaultCreateSessionTimeout); - if (result != null && !result.IsComplete) + if (result != null && !result.IsCompleted) { cancellationTokenSource.Cancel(); SessionCreatedParameters response = new SessionCreatedParameters @@ -359,20 +360,28 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer /// object if successful, null if unsuccessful internal async Task DoCreateSession(ConnectionDetails connectionDetails, string uri) { - ObjectExplorerSession session; - connectionDetails.PersistSecurityInfo = true; - ConnectParams connectParams = new ConnectParams() { OwnerUri = uri, Connection = connectionDetails }; - - ConnectionCompleteParams connectionResult = await Connect(connectParams, uri); - if (connectionResult == null) + try { - // Connection failed and notification is already sent + ObjectExplorerSession session; + connectionDetails.PersistSecurityInfo = true; + ConnectParams connectParams = new ConnectParams() { OwnerUri = uri, Connection = connectionDetails }; + + ConnectionCompleteParams connectionResult = await Connect(connectParams, uri); + if (connectionResult == null) + { + // Connection failed and notification is already sent + return null; + } + + session = ObjectExplorerSession.CreateSession(connectionResult, serviceProvider); + sessionMap.AddOrUpdate(uri, session, (key, oldSession) => session); + return session; + } + catch(Exception ex) + { + await SendSessionFailedNotification(uri, ex.Message); return null; } - - session = ObjectExplorerSession.CreateSession(connectionResult, serviceProvider); - sessionMap.AddOrUpdate(uri, session, (key, oldSession) => session); - return session; } @@ -390,30 +399,30 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer } else { - Logger.Write(LogLevel.Warning, $"Connection Failed for OE. connection error: {connectionErrorMessage}"); - await serviceHost.SendEvent(CreateSessionCompleteNotification.Type, new SessionCreatedParameters - { - ErrorMessage = result.ErrorMessage, - SessionId = uri - }); + await SendSessionFailedNotification(uri, result.ErrorMessage); return null; } } catch (Exception ex) { - Logger.Write(LogLevel.Warning, $"Connection Failed for OE. connection error:{connectionErrorMessage} error: {ex.Message}"); - // Send a connection failed error message in this case. - SessionCreatedParameters result = new SessionCreatedParameters() - { - ErrorMessage = ex.ToString(), - SessionId = uri - }; - await serviceHost.SendEvent(CreateSessionCompleteNotification.Type, result); + await SendSessionFailedNotification(uri, ex.ToString()); return null; } } + private async Task SendSessionFailedNotification(string uri, string errorMessage) + { + Logger.Write(LogLevel.Warning, $"Failed To create OE session: {errorMessage}"); + SessionCreatedParameters result = new SessionCreatedParameters() + { + Success = false, + ErrorMessage = errorMessage, + SessionId = uri + }; + await serviceHost.SendEvent(CreateSessionCompleteNotification.Type, result); + } + private void RunExpandTask(ObjectExplorerSession session, ExpandParams expandParams, bool forceRefresh = false) { CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); @@ -424,7 +433,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer ObjectExplorerTaskResult result = await RunTaskWithTimeout(task, settings?.ExpandTimeout ?? ObjectExplorerSettings.DefaultExpandTimeout); - if (result != null && !result.IsComplete) + if (result != null && !result.IsCompleted) { cancellationTokenSource.Cancel(); ExpandResponse response = CreateExpandResponse(session, expandParams); @@ -440,7 +449,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer ObjectExplorerTaskResult result = new ObjectExplorerTaskResult(); TimeSpan timeout = TimeSpan.FromSeconds(timeoutInSec); await Task.WhenAny(task, Task.Delay(timeout)); - result.IsComplete = task.IsCompleted; + result.IsCompleted = task.IsCompleted; if(task.Exception != null) { result.Exception = task.Exception; @@ -563,7 +572,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer internal class ObjectExplorerTaskResult { - public bool IsComplete { get; set; } + public bool IsCompleted { get; set; } public Exception Exception { get; set; } } @@ -601,7 +610,7 @@ namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer DatabaseTreeNode databaseNode = new DatabaseTreeNode(rootNode, response.ConnectionSummary.DatabaseName); session.Root = databaseNode; } - + return session; } diff --git a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/DatabaseTreeNode.cs b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/DatabaseTreeNode.cs index 2463b7ea..1333204f 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/DatabaseTreeNode.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/DatabaseTreeNode.cs @@ -7,7 +7,6 @@ using System; using System.Globalization; using Microsoft.SqlServer.Management.Smo; -using Microsoft.SqlTools.ServiceLayer.ObjectExplorer.Nodes; using Microsoft.SqlTools.Utility; namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel diff --git a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryContext.cs b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryContext.cs index 5b3e5fb2..f509b943 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryContext.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoQueryContext.cs @@ -4,14 +4,10 @@ // using System; -using System.Collections; -using System.Collections.Generic; using System.Globalization; -using Microsoft.SqlServer.Management.Common; using Microsoft.SqlServer.Management.Smo; using Microsoft.SqlTools.Extensibility; using Microsoft.SqlTools.ServiceLayer.ObjectExplorer.Nodes; -using Microsoft.SqlTools.Utility; namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel { diff --git a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoWrapper.cs b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoWrapper.cs index 33e8ebb6..242eac0b 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoWrapper.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/ObjectExplorer/SmoModel/SmoWrapper.cs @@ -3,20 +3,9 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. // -using System; -using System.Data.Common; using System.Data.SqlClient; -using System.Globalization; -using System.Linq; using Microsoft.SqlServer.Management.Common; using Microsoft.SqlServer.Management.Smo; -using Microsoft.SqlTools.Extensibility; -using Microsoft.SqlTools.ServiceLayer.Connection; -using Microsoft.SqlTools.ServiceLayer.Connection.Contracts; -using Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection; -using Microsoft.SqlTools.ServiceLayer.ObjectExplorer.Nodes; -using Microsoft.SqlTools.ServiceLayer.Utility; -using Microsoft.SqlTools.Utility; namespace Microsoft.SqlTools.ServiceLayer.ObjectExplorer.SmoModel { diff --git a/src/Microsoft.SqlTools.ServiceLayer/SqlContext/ObjectExplorerSettings.cs b/src/Microsoft.SqlTools.ServiceLayer/SqlContext/ObjectExplorerSettings.cs index 8d0adf56..ea22fa51 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/SqlContext/ObjectExplorerSettings.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/SqlContext/ObjectExplorerSettings.cs @@ -10,8 +10,8 @@ namespace Microsoft.SqlTools.ServiceLayer.SqlContext /// public class ObjectExplorerSettings { - public static int DefaultCreateSessionTimeout = 300; - public static int DefaultExpandTimeout = 300; + public static int DefaultCreateSessionTimeout = 30; + public static int DefaultExpandTimeout = 30; public ObjectExplorerSettings() {