コード例 #1
0
        /// <summary>
        /// This function obtains a live connection, then calls
        /// an assessment operation specified by <paramref name="assessmentFunc"/>
        /// </summary>
        /// <typeparam name="TResult">
        /// SQL Assessment result item type.
        /// </typeparam>
        /// <param name="requestParams">
        /// Request parameters passed from the host.
        /// </param>
        /// <param name="connectParams">
        /// Connection parameters used to identify and access the target.
        /// </param>
        /// <param name="taskUri">
        /// An URI identifying the request task to enable concurrent execution.
        /// </param>
        /// <param name="assessmentFunc">
        /// A function performing assessment operation for given target.
        /// </param>
        /// <returns>
        /// Returns <see cref="AssessmentResult{TResult}"/> for given target.
        /// </returns>
        internal async Task <AssessmentResult <TResult> > CallAssessmentEngine <TResult>(
            AssessmentParams requestParams,
            ConnectParams connectParams,
            string taskUri,
            Func <SqlObjectLocator, Task <List <TResult> > > assessmentFunc)
            where TResult : AssessmentItemInfo

        {
            var result = new AssessmentResult <TResult>
            {
                ApiVersion = ApiVersion
            };

            await ConnectionService.Connect(connectParams);

            var connection = await ConnectionService.Instance.GetOrOpenConnection(taskUri, ConnectionType.Query);

            try
            {
                var serverInfo = ReliableConnectionHelper.GetServerVersion(connection);
                var hostInfo   = ReliableConnectionHelper.GetServerHostInfo(connection);

                var server = new SqlObjectLocator
                {
                    Connection    = connection,
                    EngineEdition = GetEngineEdition(serverInfo.EngineEditionId),
                    Name          = serverInfo.ServerName,
                    ServerName    = serverInfo.ServerName,
                    Type          = SqlObjectType.Server,
                    Urn           = serverInfo.ServerName,
                    Version       = Version.Parse(serverInfo.ServerVersion),
                    Platform      = hostInfo.Platform
                };

                switch (requestParams.TargetType)
                {
                case SqlObjectType.Server:
                    Logger.Write(
                        TraceEventType.Verbose,
                        $"SQL Assessment: running an operation on a server, platform:{server.Platform}, edition:{server.EngineEdition.ToString()}, version:{server.Version}");

                    result.Items.AddRange(await assessmentFunc(server));

                    Logger.Write(
                        TraceEventType.Verbose,
                        $"SQL Assessment: finished an operation on a server, platform:{server.Platform}, edition:{server.EngineEdition.ToString()}, version:{server.Version}");
                    break;

                case SqlObjectType.Database:
                    var db = GetDatabaseLocator(server, connection.Database);
                    Logger.Write(
                        TraceEventType.Verbose,
                        $"SQL Assessment: running an operation on a database, platform:{server.Platform}, edition:{server.EngineEdition.ToString()}, version:{server.Version}");

                    result.Items.AddRange(await assessmentFunc(db));

                    Logger.Write(
                        TraceEventType.Verbose,
                        $"SQL Assessment: finished an operation on a database, platform:{server.Platform}, edition:{server.EngineEdition.ToString()}, version:{server.Version}");
                    break;
                }

                result.Success = true;
            }
            finally
            {
                ActiveRequests.TryRemove(taskUri, out _);
                ConnectionService.Disconnect(new DisconnectParams {
                    OwnerUri = taskUri, Type = null
                });
            }

            return(result);
        }