コード例 #1
0
        /// <inheritdoc/>
        public override void Execute(UserSettings settings)
        {
            switch (settings.AppSide)
            {
            case AppSide.Client:
                IContract client = CreateClient <IContract>(settings);

                SysConsole.WriteQuestionLine("Single client instance created!");
                SysConsole.WriteLine();

                new Thread(() =>
                {
                    SysConsole.WriteInfoLine("[ON] Client request-reply method caller");
                    while (true)
                    {
                        client.Method("User1");
                    }
                }).Start();

                new Thread(() =>
                {
                    SysConsole.WriteInfoLine("[ON] Client OneWay method caller");
                    while (true)
                    {
                        client.MethodOneWay("User2");
                        Thread.Sleep(1000);
                    }
                }).Start();

                Console.ReadKey();
                break;

            case AppSide.Server:

                var concurrencyMode = QuestionManager.Choose(
                    new[]
                {
                    ConcurrencyMode.Single,
                    ConcurrencyMode.Multiple
                },
                    s => s.ToString(),
                    "Choose service ConcurrencyMode demonstration:");


                var host = concurrencyMode == ConcurrencyMode.Multiple
                        ? CreateServiceHost <IContract, ConcurrencyMultiple>(settings)
                        : CreateServiceHost <IContract, ConcurrencySingle>(settings);

                host.Open();
                SysConsole.WriteInfoLine($"Each one contract method executing at least {ThreadSleepTimeSeconds} SECONDS");
                QuestionManager.AwaitingClientConnections();
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
コード例 #2
0
        /// <inheritdoc/>
        public override void Execute(UserSettings settings)
        {
            switch (settings.AppSide)
            {
            case AppSide.Client:
                var client = CreateClient <IMyService>(settings);
                SysConsole.WriteLine();

                TrySafeCall(
                    () =>
                {
                    SysConsole.WriteQuestionLine($"Calling >> [{nameof(client.MethodFaultContract)}]...");
                    client.MethodFaultContract();
                });

                SysConsole.WriteLine();

                TrySafeCall(
                    () =>
                {
                    SysConsole.WriteQuestionLine($"Calling >> [{nameof(client.MethodWrongFaultContract)}]...");
                    client.MethodWrongFaultContract();
                });

                SysConsole.WriteLine();

                TrySafeCall(
                    () =>
                {
                    SysConsole.WriteQuestionLine($"Calling >> [{nameof(client.MethodThrownFaultException)}]...");
                    client.MethodThrownFaultException();
                });

                SysConsole.WriteLine();

                TrySafeCall(
                    () =>
                {
                    SysConsole.WriteQuestionLine("Validating client service proxy:");
                    SysConsole.WriteQuestionLine($"Calling >> [{nameof(client.CheckClient)}]...");
                    SysConsole.WriteLine(client.CheckClient());
                });
                Console.ReadKey();
                break;

            case AppSide.Server:
                CreateServiceHost <IMyService, MyService>(settings).Open();
                QuestionManager.AwaitingClientConnections();
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
コード例 #3
0
        /// <inheritdoc/>
        public override void Execute(UserSettings settings)
        {
            switch (settings.AppSide)
            {
            case AppSide.Client:
                IContract client = CreateClient <IContract>(settings);

                int threads = 5;
                SysConsole.WriteQuestionLine("Single client instance created!");
                SysConsole.WriteQuestionLine($"{threads} thread gonna calling this client.");
                SysConsole.WriteQuestionLine($"Expected {threads} concurrent executions.", 1);

                for (int i = 0; i < threads; i++)
                {
                    var localId = i;
                    new Thread(
                        () =>
                    {
                        SysConsole.WriteInfoLine("[ON] Client Request-Reply method caller");
                        while (true)
                        {
                            client.Method($"User{localId + 1}");
                        }
                    }).Start();
                }

                Console.ReadKey();
                break;

            case AppSide.Server:

                var host = CreateServiceHost <IContract, ConcurrencyMultiple>(settings);
                host.Open();

                SysConsole.WriteInfoLine($"Each one contract method process time at least {ThreadSleepTimeSeconds} SECONDS");
                QuestionManager.AwaitingClientConnections();
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
コード例 #4
0
        private void InitClient(object clientObj)
        {
            // Each one client processing service messages on call GetMessages()
            var client = clientObj as IMyService;

            // 1. Login method call.
            var clientId = client.Login(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

            try
            {
                do
                {
                    // 2. Infinity GetMessage requests
                    var callbackMessages = client.GetMessages(clientId, TimeSpan.FromSeconds(10));

                    // 3. Processing messages
                    foreach (var callbackMessage in callbackMessages)
                    {
                        var broadcast = callbackMessage as BroadcastMessage;
                        if (broadcast != null)
                        {
                            ProcessBroadcast(clientId, broadcast);
                            continue;
                        }

                        var personal = callbackMessage as PersonalMessage;
                        if (personal != null)
                        {
                            ProcessPersonal(clientId, personal);
                            continue;
                        }

                        SysConsole.WriteQuestionLine($"Unknown message {callbackMessage.GetType()}");
                    }
                } while (true);
            }
            catch (ClientNotFoundedException ex)
            {
                SysConsole.WriteErrorLine($"Client disconnected {ex.ClientId}. Re-login required...");
            }
        }
コード例 #5
0
        /// <inheritdoc/>
        public override void Execute(UserSettings settings)
        {
            switch (settings.AppSide)
            {
            case AppSide.Client:
                var client = CreateClient <IErrorService>(settings);
                SysConsole.WriteLine();

                TrySafeCall(
                    () =>
                {
                    SysConsole.WriteQuestionLine($"Calling >> [{nameof(client.MethodThrowingException)}]...");
                    client.MethodThrowingException();
                });

                TrySafeCall(
                    () =>
                {
                    SysConsole.WriteQuestionLine($"Calling >> [{nameof(client.CheckClient)}]...");
                    SysConsole.WriteLine(client.CheckClient());
                });

                Console.ReadKey();
                break;

            case AppSide.Server:
                var serviceHost  = new ServiceHost(typeof(MyErrorService));
                var servicePoint = serviceHost.AddServiceEndpoint(typeof(IErrorService), settings.Binding, settings.ServiceUrl);
                servicePoint.Behaviors.Add(new ErrorBehavior());
                serviceHost.Open();
                QuestionManager.AwaitingClientConnections();
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
コード例 #6
0
        /// <inheritdoc/>
        public override void Execute(UserSettings settings)
        {
            var correctBindings = new[]
            {
                typeof(NetTcpBinding),
                typeof(WSHttpBinding),
                typeof(BasicHttpBinding)
            };

            var bindingType = settings.Binding.GetType();

            if (!correctBindings.Contains(bindingType))
            {
                SysConsole.WriteLine($"Binding type should be {string.Join(" or ", correctBindings.Select(binding => binding.Name))}");
                return;
            }

            var reliableSessionFlag = QuestionManager.Choose(
                new[]
            {
                true,
                false
            },
                s => CultureInfo.CurrentCulture.TextInfo.ToTitleCase(s.ToString()),
                "Choose reliableSession mode");

            SysConsole.WriteLine();
            SysConsole.WriteQuestionLine($"ReliableSession mode [{reliableSessionFlag.ToString().ToUpper()}] in use now!", 3);

            if (bindingType == typeof(NetTcpBinding))
            {
                var netTcp = ((NetTcpBinding)settings.Binding);
                netTcp.ReliableSession.Enabled = reliableSessionFlag;
                netTcp.Security.Mode           = SecurityMode.None;
            }
            else if (bindingType == typeof(WSHttpBinding))
            {
                var wsDual = ((WSHttpBinding)settings.Binding);
                wsDual.ReliableSession.Enabled = reliableSessionFlag;
                wsDual.Security.Mode           = SecurityMode.None;
            }
            else if (bindingType == typeof(BasicHttpBinding))
            {
                SysConsole.WriteLine($"[{settings.Binding.Name}] [{bindingType.Name}] not supports ReliableSession");
            }
            else
            {
                throw new NotSupportedException(bindingType.FullName);
            }

            switch (settings.AppSide)
            {
            case AppSide.Client:
                var client = CreateClient <IService>(settings);
                SysConsole.WriteQuestion("Single proxy channel client instance!", 1);

                int maxCatchs = 3;
                while (maxCatchs > 0)
                {
                    try
                    {
                        SysConsole.WriteLine($"Call >> {nameof(IService.Method)}");
                        client.Method();
                        Thread.Sleep(2000);
                    }
                    catch (Exception exception)
                    {
                        SysConsole.WriteErrorLine(exception.Message);
                        Thread.Sleep(2000);
                    }

                    try
                    {
                        SysConsole.WriteLine($"Call >> {nameof(IService.OneWayMethodThrowingException)}");
                        client.OneWayMethodThrowingException();
                        Thread.Sleep(2000);
                    }
                    catch (Exception exception)
                    {
                        SysConsole.WriteErrorLine(exception.Message);
                        Thread.Sleep(2000);
                    }

                    try
                    {
                        SysConsole.WriteLine($"Call >> {nameof(IService.MethodThrowingException)}");
                        client.MethodThrowingException();
                        Thread.Sleep(2000);
                    }
                    catch (Exception exception)
                    {
                        SysConsole.WriteErrorLine(exception.Message);
                        Thread.Sleep(2000);
                    }
                    maxCatchs--;
                }

                Console.ReadKey();
                break;

            case AppSide.Server:
                CreateServiceHost <IService, Service>(settings).Open();
                QuestionManager.AwaitingClientConnections();
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
コード例 #7
0
        /// <inheritdoc/>
        public override void Execute(UserSettings settings)
        {
            if (settings.Binding.GetType() != typeof(NetTcpBinding))
            {
                SysConsole.WriteLine("Binding should be NetTcpBinding");
                return;
            }

            var maxConnections =
                QuestionManager.Choose(
                    new[]
            {
                2,
                3,
                4,
                5
            },
                    s => s.ToString(),
                    "Choose maxConnections number",
                    "NOTE: Please use a same value on client and server sides.");

            var netTcpBinding = (NetTcpBinding)settings.Binding;

            netTcpBinding.MaxConnections = maxConnections;
            SysConsole.WriteQuestionLine($"{netTcpBinding.Name}.MaxConnection = {maxConnections} now");

            switch (settings.AppSide)
            {
            case AppSide.Client:

                int clients = 15;
                SysConsole.WriteLine();
                SysConsole.WriteLine();
                SysConsole.WriteLine($"Starting {clients} clients.");
                SysConsole.WriteLine();

                // Если нет channel клиентов, которые могу работать со службой, тогда все соединения сбрасываются и maxConnections не работает
                var holderClient = CreateClient <IMyService>(settings);

                for (var i = 0; i < clients; i++)
                {
                    var client = CreateClient <IMyService>(settings);
                    int iLocal = i;
                    PeriodicExecutor(client,
                                     (service, j) =>
                    {
                        SysConsole.WriteQuestionLine($"Client{iLocal} >> call[{j}]");
                        service.Method($"Client{iLocal}");
                        CloseClient(service);
                        return(false);
                    }, 0);
                }

                Console.ReadKey();
                break;

            case AppSide.Server:
                CreateServiceHost <IMyService, MyService>(settings).Open();
                SysConsole.WriteQuestionLine("Unlimited method calls with OneWay.");
                QuestionManager.AwaitingClientConnections();
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
コード例 #8
0
        /// <inheritdoc/>
        public override void Execute(UserSettings settings)
        {
            switch (settings.AppSide)
            {
            case AppSide.Client:
                SysConsole.WriteLine();
                SysConsole.WriteQuestionLine("A correct flow when we logged in -> said hello world -> logged out.", 2);

                while (true)
                {
                    var client = CreateClient <IMyService>(settings);
                    try
                    {
                        while (true)
                        {
                            var methodName = QuestionManager.Choose(
                                new[]
                            {
                                nameof(IMyService.Login),
                                nameof(IMyService.SayHello),
                                nameof(IMyService.Logout),
                            },
                                s => s.ToString(),
                                "Choose contract action call");
                            switch (methodName)
                            {
                            case nameof(IMyService.Login):
                                client.Login();
                                break;

                            case nameof(IMyService.Logout):
                                client.Logout();
                                break;

                            case nameof(IMyService.SayHello):
                                client.SayHello();
                                break;
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        SysConsole.WriteErrorLine(exception.Message);
                        var answer = QuestionManager.Choose(
                            new[]
                        {
                            "Yes",
                            "No"
                        },
                            s => s.ToString(),
                            "Create new connection?");

                        if (answer == "No")
                        {
                            break;
                        }
                    }
                }
                break;

            case AppSide.Server:
                var service = CreateServiceHost <IMyService, MyService>(settings);
                service.Open();
                SysConsole.WriteQuestion("Service is working.");
                QuestionManager.AwaitingClientConnections();
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }