Esempio n. 1
0
        public void ClientBuilder_ServiceProviderTest()
        {
            var builder = new ClientBuilder()
                          .ConfigureDefaults()
                          .ConfigureApplicationParts(
                parts => parts
                .AddFromApplicationBaseDirectory()
                .AddFromAppDomain()).ConfigureServices(RemoveConfigValidators)
                          .ConfigureServices(services => services.AddSingleton <IGatewayListProvider, NoOpGatewaylistProvider>());

            Assert.Throws <ArgumentNullException>(() => builder.ConfigureServices(null));

            var registeredFirst = new int[1];

            var one = new MyService {
                Id = 1
            };

            builder.ConfigureServices(
                services =>
            {
                Interlocked.CompareExchange(ref registeredFirst[0], 1, 0);
                services.AddSingleton(one);
            });

            var two = new MyService {
                Id = 2
            };

            builder.ConfigureServices(
                services =>
            {
                Interlocked.CompareExchange(ref registeredFirst[0], 2, 0);
                services.AddSingleton(two);
            });

            using (var client = builder.Build())
            {
                var services = client.ServiceProvider.GetServices <MyService>()?.ToList();
                Assert.NotNull(services);

                // Both services should be registered.
                Assert.Equal(2, services.Count);
                Assert.NotNull(services.FirstOrDefault(svc => svc.Id == 1));
                Assert.NotNull(services.FirstOrDefault(svc => svc.Id == 2));

                // Service 1 should have been registered first - the pipeline order should be preserved.
                Assert.Equal(1, registeredFirst[0]);

                // The last registered service should be provided by default.
                Assert.Equal(2, client.ServiceProvider.GetRequiredService <MyService>().Id);
            }
        }
Esempio n. 2
0
        public static IClusterClient CreateClusterClient(string hostName, IEnumerable <IConfigurationSource> configurationSources)
        {
            var configBuilder = new ConfigurationBuilder();

            foreach (var source in configurationSources)
            {
                configBuilder.Add(source);
            }
            var configuration = configBuilder.Build();

            var builder = new ClientBuilder();

            builder.Properties["Configuration"] = configuration;
            builder.Configure <ClusterOptions>(configuration);

            builder.ConfigureServices(services =>
            {
                TryConfigureTestClusterMembership(configuration, services);
                TryConfigureFileLogging(configuration, services, hostName);
            });

            ConfigureAppServices(configuration, builder);
            builder.GetApplicationPartManager().ConfigureDefaults();
            return(builder.Build());
        }
Esempio n. 3
0
        public MessageSerialization(bool roundtrip, params Type[] serializers)
        {
            Requires.NotNull(serializers, nameof(serializers));

            if (!roundtrip)
            {
                return;
            }

            var builder = new ClientBuilder()
                          .Configure <ClusterOptions>(x =>
            {
                x.ClusterId = "test";
                x.ServiceId = "test-service";
            })
                          .UseStaticClustering(x => x.Gateways.Add(new IPEndPoint(0, 0).ToGatewayUri()))
                          .ConfigureApplicationParts(apm => apm.AddFromAppDomain());

            if (serializers.Length > 0)
            {
                builder.ConfigureServices(services => services.Configure <SerializationProviderOptions>(options =>
                {
                    options.SerializationProviders.AddRange(serializers.Select(x => x.GetTypeInfo()));
                }));
            }

            var client = builder.Build();

            Manager = client.ServiceProvider.GetRequiredService <SerializationManager>();
        }
        private static IClusterClient BuildOrleansClient(System.Uri fabricName)
        {
            IClientBuilder clientBuilder = new ClientBuilder();

            clientBuilder.UseConfiguration(new ClientConfiguration());
            clientBuilder.AddServiceFabric(fabricName);
            clientBuilder.ConfigureServices(collection =>
            {
                // Add in Custom Services here
            });
            return(clientBuilder.Build());
        }
Esempio n. 5
0
        private static async Task Run(string[] args)
        {
            var serviceName = new Uri("fabric:/StatelessCalculatorApp/StatelessCalculatorService");

            var builder = new ClientBuilder();

            builder.Configure <ClusterOptions>(options =>
            {
                options.ServiceId = Guid.Empty;
                options.ClusterId = "dev";
            });

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

            // Add the application assemblies.
            builder.ConfigureApplicationParts(parts => parts.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);
            });

            // Create 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;
        }