IJsonRpcBuilder IJsonRpcBuilder.Register(Type serviceType)
 {
     if (serviceType == null)
     {
         throw new ArgumentNullException(nameof(serviceType));
     }
     lock (serviceHostBuilder)
         serviceHostBuilder.Register(serviceType);
     if (injectServices)
     {
         serviceCollection.AddTransient(serviceType);
     }
     return(this);
 }
Beispiel #2
0
 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());
        }
        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());
        }
Beispiel #5
0
        // 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;
        }