Exemplo n.º 1
0
        public async Task NamedPipeRpcClientRaisesEventOnDisconnectWhenServerShutdown()
        {
            var name = Guid.NewGuid().ToString();

            using (var server = new NamedPipeRpcServerChannel(
                       new BinaryRpcSerializer(),
                       new RpcMessageFactory(),
                       name))
            {
                await server.ListenAsync();

                using (var client = new NamedPipeRpcClientChannel(new BinaryRpcSerializer(),
                                                                  new RpcMessageFactory(), name))
                {
                    await client.ConnectAsync();

                    var wait = new ManualResetEventSlim(false);
                    client.Disconnected += (s, e) =>
                    {
                        Assert.AreSame(client, e.TransportChannel.Channel);
                        wait.Set();
                    };

                    server.Dispose();

                    Assert.IsTrue(wait.Wait(1000));
                }
            }
        }
Exemplo n.º 2
0
        static async Task Main(string[] args)
        {
            /*var server = new TcpRpcServerChannel(
             *  new JsonRpcSerializer(),
             *  new RpcMessageFactory(),
             *  IPAddress.Loopback,
             *  11234);
             * server.ObjectRepository.RegisterSingleton(new TestObject());
             * await server.ListenAsync();*/
            const LogLevel logLevel      = LogLevel.Error;
            var            loggerFactory = LoggerFactory.Create(builder =>
                                                                builder
                                                                .AddFilter("AdvancedRpcLib", logLevel)
                                                                .AddConsole(o => o.LogToStandardErrorThreshold = logLevel));

            // allow all authenticated users to access the pipe
            var ps       = new PipeSecurity();
            var sid      = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null);
            var everyone = sid.Translate(typeof(NTAccount));

            ps.AddAccessRule(new PipeAccessRule(everyone, PipeAccessRights.ReadWrite, AccessControlType.Allow));
            // we need to add the current user so we can open more than one server pipes
            ps.AddAccessRule(new PipeAccessRule(WindowsIdentity.GetCurrent().User, PipeAccessRights.FullControl, AccessControlType.Allow));

            var server = new NamedPipeRpcServerChannel(new BinaryRpcSerializer(), new RpcMessageFactory(), "test", ps,
                                                       loggerFactory: loggerFactory);

            server.ObjectRepository.RegisterSingleton(new TestObject());
            await server.ListenAsync();

            Console.WriteLine("Press key to quit");
            Console.ReadKey();
        }
Exemplo n.º 3
0
        private async Task <IRpcServerChannel> CreateServer <T>(T instance, ChannelType channelType, IRpcSerializer serializer = null,
                                                                TokenImpersonationLevel tokenImpersonationLevel = TokenImpersonationLevel.None,
                                                                IRpcObjectRepository localRepository            = null) where T : class
        {
            if (serializer == null)
            {
                serializer = new BinaryRpcSerializer();
            }
            switch (channelType)
            {
            case ChannelType.Tcp:
            {
                var server = new TcpRpcServerChannel(
                    serializer,
                    new RpcMessageFactory(),
                    IPAddress.Loopback,
                    11234,
                    localRepository);
                if (instance != null)
                {
                    server.ObjectRepository.RegisterSingleton(instance);
                }
                await server.ListenAsync();

                return(server);
            }

            case ChannelType.NamedPipe:
            {
                _pipeName = Guid.NewGuid().ToString();
                var server = new NamedPipeRpcServerChannel(
                    serializer,
                    new RpcMessageFactory(),
                    _pipeName,
                    localRepository: localRepository);
                if (instance != null)
                {
                    server.ObjectRepository.RegisterSingleton(instance);
                }
                await server.ListenAsync();

                return(server);
            }

            default:
                throw new NotSupportedException();
            }
        }