Exemple #1
0
 public static IMongoCollection<TItem> FromCallistoConnectorOptions<TItem>(CallistoConnectorOptions options,
                                                                           ApplicationOptions app,
                                                                           string collectionName,
                                                                           string database)
 {
     Type type = typeof(TItem);
     IMongoClient client = CreateFromOptions(options, app);
     return client.GetDatabase(database ?? "default").GetCollection<TItem>(collectionName ?? type.Name.ToLowerInvariant());
 }
Exemple #2
0
        public static IMessageDataRepository MessageDataRepositoryWithCallisto(string clientName, string database,
                                                                               IConfiguration configuration)
        {
            if (string.IsNullOrEmpty(database))
            {
                throw new CallistoException("Please provide a database to the SagaRepository");
            }
            CallistoConnectorOptions options = Helper.GetCallistoConnectionOptions(clientName, configuration);

            return(new MongoDbMessageDataRepository(options.CreateUrl(Helper.GetAppOptions(configuration)).ToString(), database));
        }
Exemple #3
0
        public static CallistoConnectorOptions GetCallistoConnectionOptions(string clientName, IConfiguration configuration)
        {
            IConfigurationSection section = configuration.GetSection(CallistoConstants.ConnectorAppSettingsSection);

            if (!section.Exists())
            {
                throw new CallistoException("The callisto section does not exists in the AppSettings file.");
            }
            CallistoConnectorOptions options = section.GetOptions <List <CallistoConnectorOptions> >()
                                               .FirstOrDefault(a => a.Name.ToUpperInvariant().Equals(clientName.ToUpperInvariant()));

            if (string.IsNullOrEmpty(options.ConnectionString))
            {
                throw new CallistoException("The current MassTransit integration requires a mongodb connection string");
            }
            return(options);
        }
Exemple #4
0
        internal static ISagaRegistrationConfigurator <TSaga> AddSagaWithCallisto <TSaga>(ISagaRegistrationConfigurator <TSaga> configurator,
                                                                                          string clientName,
                                                                                          string database,
                                                                                          IConfiguration configuration)
            where TSaga : class, ISagaVersion
        {
            if (string.IsNullOrEmpty(database))
            {
                throw new CallistoException("Please provide a database to the SagaRepository");
            }
            CallistoConnectorOptions options = Helper.GetCallistoConnectionOptions(clientName, configuration);

            configurator.MongoDbRepository(options.CreateUrl(Helper.GetAppOptions(configuration)).ToString(),
                                           repositoryConfigurator => { repositoryConfigurator.DatabaseName = database; });

            return(configurator);
        }
Exemple #5
0
        private static IMongoClient CreateFromOptions(CallistoConnectorOptions options, ApplicationOptions app)
        {
            CallistoJaegerEventListener eventListener = null;
            if (options.Trace)
            {
                if (GlobalTracer.Instance != null)
                {
                    eventListener = new CallistoJaegerEventListener(GlobalTracer.Instance, new EventFilter(), Options.Create(new CallistoTracerOptions()));
                }
            }

            MongoUrl url = options.CreateUrl(app);
            MongoClientSettings settings = url.CreateMongoClientSettings();
            if (eventListener != null)
                settings.ConfigureTracing(options.Trace, eventListener);
            return settings.CreateMongoClient();
        }
Exemple #6
0
        public static ISolariBuilder AddCallistoIdentityProvider <TUser, TRole>(this ISolariBuilder builder,
                                                                                string clientName,
                                                                                string database,
                                                                                string usersCollection,
                                                                                string rolesCollection,
                                                                                Action <IdentityOptions> setupIdentity)
            where TUser : MongoUser
            where TRole : MongoRole
        {
            IdentityBuilder identityBuilder = builder.Services.AddIdentity <TUser, TRole>(setupIdentity ?? (x => { }));

            identityBuilder.AddRoleStore <RoleStore <TRole> >()
            .AddUserStore <UserStore <TUser, TRole> >()
            .AddUserManager <UserManager <TUser> >()
            .AddRoleManager <RoleManager <TRole> >()
            .AddDefaultTokenProviders();


            var conventions = new CallistoConventionRegistry();

            conventions.AddDefaultConventions();
            conventions.RegisterConventionPack("CallistoIdentityConventionPack", type =>
            {
                bool user = typeof(TUser).IsAssignableFrom(typeof(MongoUser)) && typeof(TUser) == type;
                bool role = typeof(TRole).IsAssignableFrom(typeof(MongoRole)) && typeof(TRole) == type;
                return(user && role);
            });
            ApplicationOptions       app     = builder.GetAppOptions();
            CallistoConnectorOptions options = builder.Configuration.GetCallistoConnectorOptions(clientName);
            IMongoCollection <TUser> users   = MongoUtil.FromCallistoConnectorOptions <TUser>(options, app, usersCollection, database);
            IMongoCollection <TRole> roles   = MongoUtil.FromCallistoConnectorOptions <TRole>(options, app, rolesCollection, database);

            builder.Services.AddSingleton(x => users);
            builder.Services.AddSingleton(x => roles);

            // Identity Services
            builder.Services.AddTransient <IRoleStore <TRole> >(x => new RoleStore <TRole>(roles));
            builder.Services.AddTransient <IUserStore <TUser> >(x => new UserStore <TUser, TRole>(users, new RoleStore <TRole>(roles), x.GetService <ILookupNormalizer>()));


            return(builder);
        }
Exemple #7
0
        public ICallistoClientConfigurator RegisterClient(string clientName, Action <ICallistoCollectionConfigurator> configureCollections)
        {
            Check.ThrowIfNullOrEmpty(clientName, nameof(clientName), new CallistoException("Please provide the name of the client that should be registered."));
            _builder.AddBuildAction(new BuildAction($"Registering Callisto client with name: {clientName}")
            {
                Action = provider =>
                {
                    CallistoConnectorOptions clientOptions = _clients.GetCallistoConnectorOptions(clientName);

                    if (clientOptions is null)
                    {
                        throw new CallistoException($"The options for client '{clientName}' was not found. Check the AppSettings file.");
                    }

                    var registry                  = provider.GetRequiredService <ICallistoClientRegistry>();
                    var eventListener             = provider.GetRequiredService <ICallistoEventListener>();
                    ApplicationOptions appOptions = _builder.Configuration.GetApplicationOptions();
                    registry.AddClient(clientName, clientOptions.CreateUrl(appOptions)
                                       .CreateMongoClientSettings()
                                       .ConfigureTracing(clientOptions.Trace, eventListener)
                                       .CreateMongoClient()
                                       .CreateCallistoClient());
                }
            });
        public static MongoUrl CreateUrl(this CallistoConnectorOptions options, ApplicationOptions app)
        {
            var builder = new MongoUrlBuilder();

            builder.Parse(options.ConnectionString);
            if (Check.IsNull(app) || app.ApplicationName.IsNullOrEmpty())
            {
                builder.ApplicationName = options.ApplicationName;
            }
            else
            {
                builder.ApplicationName = app.ApplicationName;
            }

            builder.ConnectTimeout = options.ConnectTimeout.IsNullOrEmpty()
                                         ? MongoDefaults.ConnectTimeout
                                         : options.ConnectTimeout.ToTimeSpan();

            builder.MaxConnectionIdleTime = options.MaxConnectionIdleTime.IsNullOrEmpty()
                                                ? MongoDefaults.MaxConnectionIdleTime
                                                : options.MaxConnectionIdleTime.ToTimeSpan();

            builder.MaxConnectionLifeTime = options.MaxConnectionLifeTime.IsNullOrEmpty()
                                                ? MongoDefaults.MaxConnectionLifeTime
                                                : options.MaxConnectionLifeTime.ToTimeSpan();

            builder.MaxConnectionPoolSize =
                options.MaxConnectionPoolSize == 0 ? MongoDefaults.MaxConnectionPoolSize : options.MaxConnectionPoolSize;

            builder.MinConnectionPoolSize =
                options.MinConnectionPoolSize == 0 ? MongoDefaults.MinConnectionPoolSize : options.MinConnectionPoolSize;

            builder.HeartbeatInterval = options.HeartbeatInterval.IsNullOrEmpty()
                                            ? TimeSpan.FromSeconds(60)
                                            : options.HeartbeatInterval.ToTimeSpan();

            builder.HeartbeatTimeout = options.HeartbeatTimeout.IsNullOrEmpty()
                                           ? TimeSpan.FromSeconds(60)
                                           : options.HeartbeatTimeout.ToTimeSpan();

            builder.LocalThreshold = options.LocalThreshold.IsNullOrEmpty()
                                         ? MongoDefaults.LocalThreshold
                                         : options.LocalThreshold.ToTimeSpan();

            builder.RetryWrites = options.RetryWrites;

            builder.ServerSelectionTimeout = options.ServerSelectionTimeout.IsNullOrEmpty()
                                                 ? MongoDefaults.ServerSelectionTimeout
                                                 : options.ServerSelectionTimeout.ToTimeSpan();

            builder.SocketTimeout = options.SocketTimeout.IsNullOrEmpty()
                                        ? MongoDefaults.SocketTimeout
                                        : options.SocketTimeout.ToTimeSpan();


            builder.WaitQueueTimeout = options.WaitQueueTimeout.IsNullOrEmpty()
                                           ? MongoDefaults.WaitQueueTimeout
                                           : options.WaitQueueTimeout.ToTimeSpan();

            builder.IPv6 = options.Ipv6;

            return(builder.ToMongoUrl());
        }
Exemple #9
0
        private static MongoUrl CreateUrl(CallistoConnectorOptions options, ApplicationOptions app)
        {
            MongoUrl url = options.CreateUrl(app);

            return(url);
        }