private static IJsonRpcServiceHost BuildServiceHost(TextWriter logWriter, IJsonRpcContractResolver contractResolver, bool debugMode) { var loggerFactory = LoggerFactory.Create(builder => builder.AddDebug()); if (debugMode) { loggerFactory.AddFile("logs-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".log"); } var builder = new JsonRpcServiceHostBuilder { ContractResolver = contractResolver, LoggerFactory = loggerFactory }; builder.UseCancellationHandling(); builder.Register(typeof(Program).GetTypeInfo().Assembly); if (debugMode) { // Log all the client-to-server calls. builder.Intercept(async(context, next) => { lock (logWriter) logWriter.WriteLine("{0} > {1}", Utility.GetTimeStamp(), context.Request); await next(); lock (logWriter) logWriter.WriteLine("{0} < {1}", Utility.GetTimeStamp(), context.Response); }); } return(builder.Build()); }
private static IJsonRpcServiceHost BuildServiceHost(TextWriter logWriter, IJsonRpcContractResolver contractResolver, bool debugMode) { var loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(new DebugLoggerProvider(null)); var builder = new JsonRpcServiceHostBuilder { ContractResolver = contractResolver, LoggerFactory = loggerFactory }; builder.UseCancellationHandling(); builder.Register(typeof(Program).GetTypeInfo().Assembly); if (debugMode) { // Log all the client-to-server calls. builder.Intercept(async(context, next) => { lock (logWriter) { logWriter.WriteLine("> {0}", context.Request); } await next(); lock (logWriter) { logWriter.WriteLine("< {0}", context.Response); } }); } return(builder.Build()); }
public static IJsonRpcServiceHost CreateJsonRpcServiceHost(UnitTestBase owner) { var builder = new JsonRpcServiceHostBuilder(); builder.Register(typeof(Utility).GetTypeInfo().Assembly); builder.ContractResolver = DefaultContractResolver; var globalSw = Stopwatch.StartNew(); var session = new SessionFeature(); if (owner.Output != null) { builder.Intercept(async(context, next) => { var sw = Stopwatch.StartNew(); owner.Output.WriteLine("{0}> {1}", globalSw.Elapsed, context.Request); try { context.Features.Set(session); await next(); owner.Output.WriteLine("{0}< {1}", globalSw.Elapsed, context.Response); } finally { owner.Output.WriteLine("Server: Ellapsed time: {0}", sw.Elapsed); } }); } builder.LoggerFactory = owner.LoggerFactory; return(builder.Build()); }
/// <inheritdoc /> public IJsonRpcBuilder Intercept(Func <RequestHandler, RequestHandler> middleware) { if (middleware == null) { throw new ArgumentNullException(nameof(middleware)); } lock (serviceHostBuilder) serviceHostBuilder.Intercept(middleware); return(this); }
public IJsonRpcServiceHost BuildServiceHost(Type service) { var builder = new JsonRpcServiceHostBuilder { ContractResolver = myContractResolver, }; builder.Register(service); builder.Intercept(_handler); return(builder.Build()); }
static void Main(string[] args) { using (Stream cin = Console.OpenStandardInput()) using (BufferedStream bcin = new BufferedStream(cin)) using (Stream cout = Console.OpenStandardOutput()) using (PartwiseStreamMessageReader reader = new PartwiseStreamMessageReader(bcin)) using (PartwiseStreamMessageWriter writer = new PartwiseStreamMessageWriter(cout)) { JsonRpcContractResolver contractResolver = new JsonRpcContractResolver { NamingStrategy = new CamelCaseJsonRpcNamingStrategy(), ParameterValueConverter = new CamelCaseJsonValueConverter(), }; StreamRpcClientHandler clientHandler = new StreamRpcClientHandler(); JsonRpcClient client = new JsonRpcClient(clientHandler); clientHandler.MessageSending += (_, e) => { Console.Error.WriteLine("Sending: " + e.Message); }; clientHandler.MessageReceiving += (_, e) => { Console.Error.WriteLine("Receiving: " + e.Message); }; LanguageServerSession session = new LanguageServerSession(client, contractResolver); JsonRpcServiceHostBuilder builder = new JsonRpcServiceHostBuilder { ContractResolver = contractResolver }; builder.UseCancellationHandling(); builder.Register(typeof(Program).GetTypeInfo().Assembly); builder.Intercept(async(context, next) => { Console.Error.WriteLine("Request: " + context.Request); await next(); Console.Error.WriteLine("Response: " + context.Response); }); IJsonRpcServiceHost host = builder.Build(); StreamRpcServerHandler serverHandler = new StreamRpcServerHandler(host, StreamRpcServerHandlerOptions.ConsistentResponseSequence | StreamRpcServerHandlerOptions.SupportsRequestCancellation); serverHandler.DefaultFeatures.Set(session); using (serverHandler.Attach(reader, writer)) using (clientHandler.Attach(reader, writer)) { // Wait for the "stop" request. session.CancellationToken.WaitHandle.WaitOne(); } } }
private static IJsonRpcServiceHost BuildServiceHost() { var builder = new JsonRpcServiceHostBuilder { ContractResolver = myContractResolver, }; // Register all the services (public classes) found in the assembly builder.Register(typeof(Program).GetTypeInfo().Assembly); // Add a middleware to log the requests and responses builder.Intercept(async(context, next) => { Console.WriteLine("> {0}", context.Request); await next(); Console.WriteLine("< {0}", context.Response); }); return(builder.Build()); }
/// <summary> /// Interprets any <see cref="OperationCanceledException" /> thrown by the service /// as <c>RequestCancelled</c> error per definition in Language Server Protocol. /// </summary> public static void UseCancellationHandling(this JsonRpcServiceHostBuilder builder) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } builder.Intercept(async(context, next) => { try { await next(); } catch (OperationCanceledException ex) { if (ex.CancellationToken == context.CancellationToken || ex.CancellationToken == CancellationToken.None) { context.Response.Error = new ResponseError(Utility.RequestCancelledErrorCode, ex.Message); } } }); }
private static IJsonRpcServiceHost ServiceHostFactory(IComponentContext context) { var builder = new JsonRpcServiceHostBuilder { ContractResolver = myContractResolver, LoggerFactory = context.Resolve <ILoggerFactory>(), ServiceFactory = new AutofacServiceFactory(context.Resolve <ILifetimeScope>()) }; builder.Register(typeof(Program).GetTypeInfo().Assembly); if (context.Resolve <ApplicationConfiguration>().Debug) { var logger = context.Resolve <ILoggerFactory>().CreateLogger("SERVER"); // Log all the client-to-server calls. builder.Intercept(async(ctx, next) => { logger.LogTrace("> {@Request}", ctx.Request); await next(); logger.LogTrace("< {@Response}", ctx.Response); }); } builder.UseCancellationHandling(); return(builder.Build()); }