Example #1
0
#pragma warning restore RS0026 // Do not add multiple public overloads with optional parameters

        private IContainer ConfigureContainer(IContainer container)
        {
            container.RegisterInstance(LoggerFactory);
            container.RegisterInstance(Logger);
            container.RegisterInstance(SerilogLogger);
            return(BuildContainer(container.WithDependencyInjectionAdapter()));
        }
Example #2
0
 public AppCore.IOC.IContainer ActiveWebApi(System.Web.Http.HttpConfiguration httpConfig, System.Reflection.Assembly[] webApiAssemblies)
 {
     _container = new DryContainer().WithWebApi(httpConfig, webApiAssemblies);
     _container.RegisterInstance <AppCore.IOC.IContainer>(new Container(_container), reuse: Reuse.Singleton);
     Container = _container.Resolve <AppCore.IOC.IContainer>();
     return(Container);
 }
        public IContainer Build()
        {
            var container = new DryIocContainerAdapter();

            _dryContainer.RegisterInstance <IContainer>(container, IfAlreadyRegistered.Keep);

            _dryContainer = _dryContainer.WithNoMoreRegistrationAllowed(); // lock for new registrations

            container.Initialize(_dryContainer);

            _buildActions.Apply(action => action?.Invoke(container));
            _buildActions.Clear();

            return(container);
        }
        /// <summary>
        /// Register the singleton ISession instance with the container once we can connect to the killrvideo schema.
        /// </summary>
        public async Task RegisterDseOnceAvailable(DryIoc.IContainer container)
        {
            IDseSession session  = null;
            int         attempts = 0;

            Logger.Information("Initializing connection to DSE Cluster...");
            while (session == null)
            {
                try
                {
                    Logger.Information("+ Reading node addresses from ETCD.");
                    IEnumerable <string> hosts = await _serviceDiscovery
                                                 .LookupServiceAsync(ConfigKeys.EtcdCassandraKey)
                                                 .ConfigureAwait(false);

                    // Create cluster builder with contact points
                    var builder = DseCluster.Builder().AddContactPoints(hosts.Select(ToIpEndPoint));

                    // Authentication
                    if (!string.IsNullOrEmpty(_kvConfig[ConfigKeys.DseUsername]) &&
                        !string.IsNullOrEmpty(_kvConfig[ConfigKeys.DsePassword]))
                    {
                        Logger.Information("+ Enable Authentication with user {user}", _kvConfig[ConfigKeys.DseUsername]);
                        builder.WithAuthProvider(
                            new DsePlainTextAuthProvider(_kvConfig[ConfigKeys.DseUsername],
                                                         _kvConfig[ConfigKeys.DsePassword]));
                    }
                    else
                    {
                        Logger.Information("+ No Authentication");
                    }

                    // SSL : To be tested (first try based on documentation)
                    if (Boolean.Parse(_kvConfig[ConfigKeys.DseEnableSsl]))
                    {
                        String certPath     = _kvConfig[ConfigKeys.DseSslCertPath];
                        String certPassword = _kvConfig[ConfigKeys.DseSslCertPassword];
                        if (string.IsNullOrEmpty(certPath))
                        {
                            throw new ArgumentNullException("Cannot read SSL File " + certPath);
                        }
                        if (string.IsNullOrEmpty(certPath))
                        {
                            throw new ArgumentNullException("Cannot read SSL Certificate password " + certPath);
                        }
                        Logger.Information("+ Setup SSL options with {certPath}", certPath);
                        SSLOptions         sslOptions = new SSLOptions();
                        X509Certificate2[] certs      = new X509Certificate2[] { new X509Certificate2(ReadX509Certificate(certPath), certPassword) };
                        sslOptions.SetCertificateCollection(new X509CertificateCollection(certs));
                        sslOptions.SetRemoteCertValidationCallback((a1, a2, a3, a4) => true);
                        //sslOptions.SetHostNameResolver((internalIPAddress) => { return "test_client"; });
                        builder.WithSSL(sslOptions);
                    }
                    else
                    {
                        Logger.Information("+ No SSL");
                    }

                    // Query options
                    var queryOptions = new QueryOptions();
                    queryOptions.SetConsistencyLevel(ConsistencyLevel.LocalQuorum);
                    builder.WithQueryOptions(queryOptions);

                    // Graph Options
                    Logger.Information("+ Graph connection to {graphName}", _kvConfig[ConfigKeys.DseGraphName]);
                    var graphOptions = new GraphOptions();
                    graphOptions.SetName(_kvConfig[ConfigKeys.DseGraphName]);
                    graphOptions.SetReadTimeoutMillis(int.Parse(_kvConfig[ConfigKeys.DseGraphReadTimeout]));
                    graphOptions.SetReadConsistencyLevel(ConsistencyLevel.One);
                    graphOptions.SetWriteConsistencyLevel(ConsistencyLevel.One);
                    builder.WithGraphOptions(graphOptions);

                    // Cassandra
                    session = builder.Build().Connect(_kvConfig[ConfigKeys.DseKeySpace]);
                    Logger.Information("+ Session established to keyspace {keyspace}", _kvConfig[ConfigKeys.DseKeySpace]);
                }
                catch (Exception e)
                {
                    attempts++;
                    session = null;

                    // Don't log exceptions until we've tried 6 times
                    if (attempts >= int.Parse(_kvConfig[ConfigKeys.MaxRetry]))
                    {
                        Logger.Error(e,
                                     "Cannot connection to DSE after {max} attempts, exiting",
                                     _kvConfig[ConfigKeys.MaxRetry]);
                        Environment.Exit(404);
                    }
                }

                if (session != null)
                {
                    continue;
                }

                Logger.Information("+ Attempt #{nb}/{max} failed.. trying in {delay} seconds, waiting Dse to Start",
                                   attempts,
                                   _kvConfig[ConfigKeys.MaxRetry],
                                   _kvConfig[ConfigKeys.RetryDelay]);
                await Task.Delay(int.Parse(_kvConfig[ConfigKeys.RetryDelay])).ConfigureAwait(false);
            }

            // Since session objects should be created once and then reused, register the instance with the container
            // which will register it as a singleton by default
            container.RegisterInstance(session);
        }
Example #5
0
 public void RegisterInstance <T>(T instance)
 => _container.RegisterInstance <T>(instance);
Example #6
0
 protected override void RegisterInstanceWithContainer <T>(T instance)
 {
     _dryIocContainer.RegisterInstance(instance);
 }