private static IJsonRpcServiceHost BuildServiceHost() { var builder = new JsonRpcServiceHostBuilder(); builder.Register <SandboxRpcService>(); 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()); }
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()); }
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()); }
private static IJsonRpcServiceHost BuildServiceHost() { var builder = new JsonRpcServiceHostBuilder(); builder.Register(typeof(Program).Assembly); var loggerFactory = new LoggerFactory(); loggerFactory.AddDebug(LogLevel.Trace); builder.LoggerFactory = loggerFactory; return(builder.Build()); }
public IJsonRpcServiceHost BuildServiceHost(Type service) { var builder = new JsonRpcServiceHostBuilder { ContractResolver = myContractResolver, }; builder.Register(service); builder.Intercept(_handler); return(builder.Build()); }
public JsonRpcBuilder(JsonRpcOptions options, IServiceCollection serviceCollection) { if (options == null) { throw new ArgumentNullException(nameof(options)); } this.serviceCollection = serviceCollection ?? throw new ArgumentNullException(nameof(serviceCollection)); injectServices = options.InjectServiceTypes; serviceHostBuilder = new JsonRpcServiceHostBuilder { ContractResolver = options.ContractResolver, LoggerFactory = options.LoggerFactory, ServiceFactory = options.ServiceFactory, MethodBinder = options.MethodBinder }; }
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()); }
// If initialization failed, the whole instance should just be discarded. internal async Task InitializeAsync() { if (state != SandboxState.Created) { throw new InvalidOperationException(); } var pipeName = "SandyBox." + Guid.NewGuid(); var pipe = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); disposables.Add(pipe); Id = await Owner.HostStub.CreateSandbox(Name, pipeName); var procReader = new ByLineTextMessageReader(pipe) { LeaveReaderOpen = true }; disposables.Add(procReader); var procWriter = new ByLineTextMessageWriter(pipe) { LeaveWriterOpen = true }; disposables.Add(procWriter); // Wait for sandbox to start up. using (var cts = new CancellationTokenSource(5000)) { Message startedMessage = null; try { await pipe.WaitForConnectionAsync(cts.Token); startedMessage = await procReader.ReadAsync(m => m is RequestMessage rm && rm.Method == "NotifyStarted", cts.Token); } catch (OperationCanceledException) { } if (startedMessage == null) { throw new ExecutionHostException(Prompts.CannotStartExecutionHost_MissingNotifyStarted); } } // HOST var hostBuilder = new JsonRpcServiceHostBuilder(); hostBuilder.Register <HostingClientService>(); var host = hostBuilder.Build(); var serverHandler = new StreamRpcServerHandler(host); serverHandler.DefaultFeatures.Set <ISandboxContextFeature>(new SandboxContextFeature(Owner, this)); disposables.Add(serverHandler.Attach(procReader, procWriter)); // CLIENT var clientHandler = new StreamRpcClientHandler(); RpcClient = new JsonRpcClient(clientHandler); disposables.Add(clientHandler.Attach(procReader, procWriter)); SandboxStub = JsonRpcExecutionHost.ProxyBuilder.CreateProxy <ISandboxStub>(RpcClient); disposables.Reverse(); // Dispose in the reversed order. state = SandboxState.Started; }