Files
sqltoolsservice/src/Microsoft.SqlTools.ServiceLayer/Hosting/Protocol/RequestContext.cs
Benjamin Russell 54c43f950f Inter-Service API for executing queries (#223)
Adding new methods for executing queries from other services (such as the upcoming edit data service). The code is written to avoid duplicating logic by using lambdas to perform custom logic.
Additionally, the service host protocol has been slightly modified to split the IMessageSender into IEventSender and IRequestSender. This allows us to use either a ServiceHost or any RequestContext<T> to send events. It becomes very convenient to use another service's request context to send the events for query execution.

**Breaking Change**: This removes the messages property for query dispose results and instead elects to use error for any errors encountered during query disposal. A result is only used when something is successful.

* Splitting IMessageSender into IEventSender and IRequestSender

* Adding inter-service method for executing queries

* Adding inter-service method for disposing of a query

* Adding null checking for the success/error handlers
2017-02-02 17:05:10 -08:00

51 lines
1.5 KiB
C#

//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System.Threading.Tasks;
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Contracts;
using Newtonsoft.Json.Linq;
namespace Microsoft.SqlTools.ServiceLayer.Hosting.Protocol
{
public class RequestContext<TResult> : IEventSender
{
private readonly Message requestMessage;
private readonly MessageWriter messageWriter;
public RequestContext(Message requestMessage, MessageWriter messageWriter)
{
this.requestMessage = requestMessage;
this.messageWriter = messageWriter;
}
public RequestContext() { }
public virtual async Task SendResult(TResult resultDetails)
{
await this.messageWriter.WriteResponse(
resultDetails,
requestMessage.Method,
requestMessage.Id);
}
public virtual async Task SendEvent<TParams>(EventType<TParams> eventType, TParams eventParams)
{
await this.messageWriter.WriteEvent(
eventType,
eventParams);
}
public virtual async Task SendError(object errorDetails)
{
await this.messageWriter.WriteMessage(
Message.ResponseError(
requestMessage.Id,
requestMessage.Method,
JToken.FromObject(errorDetails)));
}
}
}