Exemplo n.º 1
0
        private static async Task Run(string[] args)
        {
            var serviceName = new Uri("fabric:/StatelessCalculatorApp/StatelessCalculatorService");

            var builder = new ClientBuilder();

            builder.UseConfiguration(new ClientConfiguration());

            // Use Service Fabric for managing cluster membership.
            builder.AddServiceFabricMembership(serviceName);

            // Add the application assemblies.
            builder.AddApplicationPart(typeof(ICalculatorGrain).Assembly);

            // Optional: configure logging.
            builder.ConfigureLogging(logging => logging.AddDebug());

            builder.ConfigureServices(
                services =>
            {
                // Some deployments require a custom FabricClient, eg so that cluster endpoints and certificates can be configured.
                // A pre-configured FabricClient can be injected.
                var fabricClient = new FabricClient();
                services.AddSingleton(fabricClient);
            });

            // Creat the client and connect to the cluster.
            var client = builder.Build();
            await client.Connect();

            double result;

            if (args.Length < 1)
            {
                Console.WriteLine(
                    $"Usage: {Assembly.GetExecutingAssembly()} <operation> [operand]\n\tOperations: get, set, add, subtract, multiple, divide");
                return;
            }

            var value = args.Length > 1 ? double.Parse(args[1]) : 0;

            var calculator        = client.GetGrain <ICalculatorGrain>(Guid.Empty);
            var observer          = new CalculatorObserver();
            var observerReference = await client.CreateObjectReference <ICalculatorObserver>(observer);

            var cancellationTokenSource = new CancellationTokenSource();
            var subscriptionTask        = StaySubscribed(calculator, observerReference, cancellationTokenSource.Token);

            switch (args[0].ToLower())
            {
            case "stress":
                result = await StressTest(client);

                break;

            case "add":
            case "+":
                result = await calculator.Add(value);

                break;

            case "subtract":
            case "-":
                result = await calculator.Subtract(value);

                break;

            case "multiply":
            case "*":
                result = await calculator.Multiply(value);

                break;

            case "divide":
            case "/":
                result = await calculator.Divide(value);

                break;

            case "set":
                result = await calculator.Set(value);

                break;

            case "get":
            default:
                result = await calculator.Get();

                break;
            }

            Console.WriteLine(result);
            Console.WriteLine("Listening for updates to calculations. Press any key to exit.");
            Console.ReadKey();
            cancellationTokenSource.Cancel();
            await subscriptionTask;
        }