//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.SqlTools.Hosting;
using Microsoft.SqlTools.Hosting.Contracts;
using Microsoft.SqlTools.Hosting.Protocol;
using Microsoft.SqlTools.Hosting.Protocol.Channel;
namespace Microsoft.SqlTools.Utility
{
///
/// SQL Tools Service request handler for any utility services. Provides the entire JSON RPC
/// implementation for sending/receiving JSON requests and dispatching the requests to
/// handlers that are registered prior to startup.
///
public sealed class UtilityServiceHost : ServiceHostBase
{
///
/// This timeout limits the amount of time that shutdown tasks can take to complete
/// prior to the process shutting down.
///
private const int ShutdownTimeoutInSeconds = 120;
#region Singleton Instance Code
///
/// Singleton instance of the service host for internal storage
///
private static readonly Lazy instance = new Lazy(() => new UtilityServiceHost());
///
/// Current instance of the ServiceHost
///
public static UtilityServiceHost Instance
{
get { return instance.Value; }
}
///
/// Constructs new instance of ServiceHost using the host and profile details provided.
/// Access is private to ensure only one instance exists at a time.
///
private UtilityServiceHost() : base(new StdioServerChannel())
{
// Initialize the shutdown activities
shutdownCallbacks = new List();
initializeCallbacks = new List();
}
///
/// Provide initialization that must occur after the service host is started
///
public void InitializeRequestHandlers()
{
// Register the requests that this service host will handle
this.SetRequestHandler(InitializeRequest.Type, this.HandleInitializeRequest);
this.SetRequestHandler(ShutdownRequest.Type, this.HandleShutdownRequest);
this.SetRequestHandler(VersionRequest.Type, HandleVersionRequest);
}
#endregion
#region Member Variables
///
/// Delegate definition for the host shutdown event
///
///
///
public delegate Task ShutdownCallback(object shutdownParams, RequestContext