Beispiel #1
0
        static async Task Main(string[] args)
        {
            var client = new ClientBuilder()
                         .ConfigureApplicationParts(parts => parts.AddFromApplicationBaseDirectory())
                         .UseLocalhostClustering()
                         .Build();

            await client.Connect();

            AssemblyLoadContext.Default.Unloading += async context => await client.Close();

            Console.WriteLine("Connected.");

            var grain       = client.GetGrain <IHelloNotifyGrain>(0);
            var observerRef = await client.CreateObjectReference <IHelloNotifyGrainObserver>(new HelloNotifyGrainObserver());

            await grain.SubscribeAsync(observerRef);

            while (true)
            {
                Console.WriteLine("Type a message.");
                var message = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(message))
                {
                    continue;
                }

                await grain.SendMessageAsync(message);
            }
        }
Beispiel #2
0
        static async Task <IClusterClient> RunWatcher()
        {
            try

            {
                // Connect to local silo
                var config = ClientConfiguration.LocalhostSilo();
                var client = new ClientBuilder().UseConfiguration(config).Build();
                await client.Connect();

                // Hardcoded player ID
                Guid         playerId = new Guid("{2349992C-860A-4EDA-9590-000000000006}");
                IPlayerGrain player   = client.GetGrain <IPlayerGrain>(playerId);
                IGameGrain   game     = null;

                while (game == null)
                {
                    Console.WriteLine("Getting current game for player {0}...", playerId);

                    try
                    {
                        game = await player.GetCurrentGame();

                        if (game == null) // Wait until the player joins a game
                        {
                            await Task.Delay(5000);
                        }
                    }
                    catch (Exception exc)
                    {
                        Console.WriteLine("Exception: ", exc.GetBaseException());
                    }
                }

                Console.WriteLine("Subscribing to updates for game {0}...", game.GetPrimaryKey());

                // Subscribe for updates
                var watcher = new GameObserver();
                await game.SubscribeForGameUpdates(
                    await client.CreateObjectReference <IGameObserver>(watcher));

                Console.WriteLine("Subscribed successfully. Press <Enter> to stop.");

                return(client);
            }
            catch (Exception exc)
            {
                Console.WriteLine("Unexpected Error: {0}", exc.GetBaseException());
                throw;
            }
        }
Beispiel #3
0
        private static async Task Run(string[] args)
        {
            var serviceName = new Uri("fabric:/StatelessCalculatorApp/StatelessCalculatorService");

            var client = new ClientBuilder()
                         .UseConfiguration(new ClientConfiguration())
                         .AddServiceFabric(serviceName)
                         .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);
            })
                         .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;
        }