示例#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
        private static ExampleBase GetExample()
        {
            var examples = new ExampleBase[]
            {
                new SessionExample(),
                new OperationsCallSequenceExample(),
                new ConnectionsExample(),
                new ReliableSessionExceptionExample(),
                new FaultExceptionExample(),
                new ErrorHandlerExample(),
                new ConcurrencyModeExample(),
                new ConcurrencyModeNotWorkingExample(),
                new BasicHttpCallbackExample()
            };

            return(QuestionManager.Choose(examples, e => e.Name, "List of samples:"));
        }
        public override void Execute(UserSettings settings)
        {
            if (!(settings.Binding is BasicHttpBinding))
            {
                SysConsole.WriteErrorLine($"Supports {nameof(BasicHttpBinding)} only.");
                return;
            }

            switch (settings.AppSide)
            {
            case AppSide.Client:
                var clients = QuestionManager.Choose(
                    new[]
                {
                    1,
                    2,
                    3
                },
                    (o) => o.ToString(),
                    "Choose clients count");

                // Creates callback processors
                for (int i = 0; i < clients; i++)
                {
                    // proxy client
                    var client = CreateClient <IMyService>(settings);

                    // client thread
                    Task.Factory.StartNew(InitClient, client);
                }

                SysConsole.PressAnyKey();
                SysConsole.WriteLine(null, 2);
                break;

            case AppSide.Server:
                CreateServiceHost <IMyService, MyService>(settings).Open();
                QuestionManager.AwaitingClientConnections();
                break;
            }
        }
示例#4
0
        private static void Main(string[] args)
        {
            var examples = new ExampleBase[]
            {
                new IfElseExample(),
                new StringFormatExample(),
                new CurrentExample(),
                new XPathIteratorMergeExample(),
            };

            while (true)
            {
                var choosenExample = QuestionManager.Choose(
                    examples,
                    example => example.Name,
                    "Please choose an example");
                Console.WriteLine();
                choosenExample.Execute();
                Console.ReadKey();
                Console.WriteLine();
            }
        }
        /// <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();
            }
        }
示例#6
0
        /// <inheritdoc/>
        public override void Execute(UserSettings settings)
        {
            switch (settings.AppSide)
            {
            case AppSide.Client:
                var sessionMode = QuestionManager.Choose(
                    new[]
                {
                    SessionMode.Allowed,
                    SessionMode.NotAllowed,
                    SessionMode.Required
                },
                    s => s.ToString(),
                    "Choose client working SessionMode:");

                var clientsCount = QuestionManager.Choose(
                    new[]
                {
                    1, 2, 3
                },
                    s => s.ToString(),
                    "How many client do we need:");

                for (int i = 0; i < clientsCount; i++)
                {
                    var client = CreateClient(settings, sessionMode);
                    PeriodicExecutor(
                        client,
                        (c, j) =>
                    {
                        c.Hello();
                        if (j == 10)
                        {
                            CloseClient(c);
                            SysConsole.WriteQuestion("Execution completed!");
                            return(false);
                        }

                        return(true);
                    });
                }

                Console.ReadKey();
                break;

            case AppSide.Server:
                var contextMode = QuestionManager.Choose(
                    new[]
                {
                    InstanceContextMode.PerCall,
                    InstanceContextMode.PerSession,
                    InstanceContextMode.Single
                },
                    s => s.ToString(),
                    "Choose service InstanceContextMode:");

                var service = CreateService(settings, contextMode);
                service.Open();
                SysConsole.WriteQuestion("Service is working.");
                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();
            }
        }
        /// <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();
            }
        }