// This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            NodaTimeSerializers.Register();
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
            .AddJsonOptions(o =>
            {
                o.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore;
                o.SerializerSettings.NullValueHandling    = Newtonsoft.Json.NullValueHandling.Ignore;
                o.SerializerSettings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
            });
            JsonConvert.DefaultSettings = () => new JsonSerializerSettings
            {
                ContractResolver  = new CamelCasePropertyNamesContractResolver(),
                Converters        = { new StringEnumConverter() },
                NullValueHandling = NullValueHandling.Ignore,
                Formatting        = Formatting.Indented
            }.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info {
                    Title = "Smart Hotel Mixed Reality API", Version = "v1"
                });
                bool authEnabled = Configuration.GetSection("AuthorizationSettings").GetValue <bool>("AuthEnabled");
                if (authEnabled)
                {
                    c.AddSecurityDefinition("ApiKeyAuth", new ApiKeyScheme()
                    {
                        Name = "X-API-KEY", In = "header", Description = "API Key Authentication"
                    });
                    c.AddSecurityRequirement(new Dictionary <string, IEnumerable <string> >
                    {
                        { "ApiKeyAuth", Enumerable.Empty <string>() },
                    });
                }
                c.ConfigureForNodaTime();
            });


            // Add functionality to inject IOptions<T>
            services.AddOptions();

            // Add our Config object so it can be injected
            services.Configure <AuthorizationSettings>(Configuration.GetSection("AuthorizationSettings"));
            services.Configure <DatabaseSettings>(Configuration.GetSection("DatabaseSettings"));
            services.Configure <DigitalTwinsSettings>(Configuration.GetSection("DigitalTwins"));
            services.Configure <SpatialServicesSettings>(Configuration.GetSection("SpatialServices"));

            services.AddScoped(typeof(IDatabaseHandler <>), typeof(DatabaseHandler <>));
            services.AddScoped <IAnchorSetService, AnchorSetService>();
            services.AddScoped <AuthorizationFilterAttribute>();
            services.AddScoped <ITopologyClient, TopologyClient>();
            services.AddScoped <IDigitalTwinsClient, DigitalTwinsClient>();
            services.AddSingleton <IClock>(SystemClock.Instance);
            services.AddHttpClient();
        }
        public static MongoElsaBuilder AddMongoDbProvider(
            this ElsaBuilder elsaBuilder,
            IConfiguration configuration,
            string databaseName,
            string connectionStringName
            )
        {
            NodaTimeSerializers.Register();
            RegisterEnumAsStringConvention();
            BsonSerializer.RegisterSerializer(new JObjectSerializer());
            BsonSerializer.RegisterSerializer(new WorkflowExecutionScopeSerializer());

            elsaBuilder.Services
            .AddSingleton(sp => CreateDbClient(configuration, connectionStringName))
            .AddSingleton(sp => CreateDatabase(sp, databaseName));

            return(new MongoElsaBuilder(elsaBuilder.Services));
        }
        internal static void Build(MongoConfigurationBuilder builder)
        {
            if (_isConfigured)
            {
                return;
            }

            var allMappedClasses = builder.GlobalDatabases.SelectMany(x => x.Value.PolymorpicClasses.Select(y => new
            {
                DatabaseName        = WashDatabaseName(x.Key),
                IsPerTenantDatabase = false,
                IsPolymorphic       = true,
                Type    = y.Key,
                Mapping = y.Value
            }).Concat(x.Value.SingleClasses.Select(y => new
            {
                DatabaseName        = WashDatabaseName(x.Key),
                IsPerTenantDatabase = false,
                IsPolymorphic       = false,
                Type    = y.Key,
                Mapping = y.Value
            }))).Concat(builder.TenantDatabases.SelectMany(x => x.Value.PolymorpicClasses.Select(y => new
            {
                DatabaseName        = WashDatabaseName(x.Key),
                IsPerTenantDatabase = true,
                IsPolymorphic       = true,
                Type    = y.Key,
                Mapping = y.Value
            }).Concat(x.Value.SingleClasses.Select(y => new
            {
                DatabaseName        = WashDatabaseName(x.Key),
                IsPerTenantDatabase = true,
                IsPolymorphic       = false,
                Type    = y.Key,
                Mapping = y.Value
            })))).ToList();

            _cappedCollections = allMappedClasses.Where(x => x.Mapping.Capped).ToDictionary(x => x.Type, x => x.Mapping.CappedCollectionConfig);

            _collections = allMappedClasses.ToDictionary(x => x.Type, x => new DatabaseCollectionDefinition
            {
                CollectionName = x.Mapping.CollectionName,
                DatabaseName   = x.DatabaseName,
                IsPolymorphic  = x.IsPolymorphic
            });

            if (!_collections.ContainsKey(typeof(DeletedObject)))
            {
                _collections[typeof(DeletedObject)] = new DatabaseCollectionDefinition {
                    CollectionName = "DeletedObjects",
                    DatabaseName   = null           // One per database
                };
            }

            _indices = allMappedClasses
                       .SelectMany(x => x.Mapping.Indices.Select(y => new
            {
                x.Type,
                Index = y
            })
                                   )
                       .ToLookup(x => x.Type, x => x.Index);

            _globalTypes = new HashSet <Type>(allMappedClasses.Where(x => !x.IsPerTenantDatabase).Select(x => x.Type));

            var conventionPack = new ConventionPack
            {
                new IgnoreExtraElementsConvention(true),
                new StringObjectIdConvention()
            };

            ConventionRegistry.Register("Conventions", conventionPack, type => true);


            foreach (var t in allMappedClasses)
            {
                var bsonClassMap = new BsonClassMap(t.Type);

                bsonClassMap.AutoMap();

                if (t.Mapping.IdMember != null)
                {
                    bsonClassMap.MapIdProperty(t.Mapping.IdMember).SetSerializer(new StringSerializer(BsonType.ObjectId)).SetIdGenerator(new StringObjectIdGenerator());
                }

                BsonClassMap.RegisterClassMap(bsonClassMap);

                if (t.IsPolymorphic)
                {
                    var subtypes = t.Type.GetTypeInfo().Assembly.GetTypes().Where(st => st.GetTypeInfo().IsSubclassOf(t.Type));

                    foreach (var st in subtypes)
                    {
                        var bsonSubClassMap = new BsonClassMap(st);
                        bsonSubClassMap.AutoMap();

                        if (!BsonClassMap.IsClassMapRegistered(st))
                        {
                            BsonClassMap.RegisterClassMap(bsonSubClassMap);
                        }
                    }
                }
            }

            BsonSerializer.RegisterSerializer(typeof(decimal), new DecimalToWholeCentsSerializer());
            NodaTimeSerializers.Register();

            MongoDefaults.MaxConnectionIdleTime = TimeSpan.FromMinutes(1);

            _isConfigured = true;
        }
 public static MongoConfigurationBuilder MapNodaTimeDates(this MongoConfigurationBuilder builder)
 {
     builder.AddPlugin(_ => { NodaTimeSerializers.Register(); });
     return(builder);
 }
Exemple #5
0
 public static void Init()
 {
     NodaTimeSerializers.Register();
 }