示例#1
0
 public void ConfigureTest()
 {
     var liteDBPersistenceProvider = new LiteDBPersistenceProvider();
 }
示例#2
0
        public virtual void ConfigureServices(IServiceCollection services)
        {
            // CORS
            services.AddCors(o => o.AddPolicy("AllowAnyPolicy", builder =>
            {
                builder.AllowAnyOrigin()
                .WithExposedHeaders(
                    "Accept-Ranges",
                    "Content-Encoding",
                    "Content-Length",
                    "Content-Range",
                    "X-Pagination",
                    "X-Rate-Limit-Limit",
                    "X-Rate-Limit-Remaining",
                    "X-Rate-Limit-Reset"
                    )
                .AllowAnyMethod()
                .AllowAnyHeader();
            }));

            var apiAssemblyName   = Assembly.GetAssembly(typeof(Startup)).FullName;
            var modelAssemblyName = Assembly.GetAssembly(typeof(ServerInfo)).FullName;

            services.AddMvc()
            .AddJsonOptions(options =>
            {
                options.SerializerSettings.ContractResolver      = new CamelCasePropertyNamesContractResolver();
                options.SerializerSettings.NullValueHandling     = NullValueHandling.Ignore;
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                options.SerializerSettings.Error     += OnJsonError;
                options.SerializerSettings.Converters = new JsonConverter[]
                {
                    new IsoDateTimeConverter
                    {
                        DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffzzz"
                                         //DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss"
                    }
                };
            })
            .AddApplicationPart(Assembly.Load(apiAssemblyName))
            .AddApplicationPart(Assembly.Load(modelAssemblyName));

            var apiDocXmlPath = $@"{ContentRoot}/API.xml";

            if (File.Exists(apiDocXmlPath))
            {
                Logger.Info($"Using API Documentation for Swagger from: {apiDocXmlPath}");
            }
            else
            {
                Logger.Error($"Could not find API Documentation for Swagger under: {apiDocXmlPath}");
            }
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info {
                    Title = "SmartDevicesGatewayAPI", Version = "v1"
                });
                c.IncludeXmlComments(apiDocXmlPath);
                c.OperationFilter <BinaryBodyPayloadFilter>();
                c.OperationFilter <JsonBodyPayloadFilter>();
            });

            //Setup ConfigService
            var confService = new ConfigService(_loggerFactory)
            {
                ContentRoot = ContentRoot, Environment = _hostingEnvironment.EnvironmentName
            };

//            confService.RegisterConfigFile(Path.Combine(ContentRoot, "config/ServiceConfig.json"), typeof(ServiceConfig));
            confService.RegisterConfigFile(Path.Combine(ContentRoot, "config/SmartDeviceConfig.json"), typeof(SmartDeviceConfig));
            confService.RegisterConfigFile(Path.Combine(ContentRoot, "config/UserConfig.json"), typeof(UserConfig));
            confService.RegisterConfigFile(Path.Combine(ContentRoot, "config/AppConfig.json"), typeof(AppConfig));
            confService.RegisterConfigFile(Path.Combine(ContentRoot, "config/UiConfig.json"), typeof(UiConfig));

            services.AddSingleton <IConfigService>(confService);

            //Add other services
            //            var useAmqp = GetAmqpUseOrDefault(true);
            //            if (useAmqp)
            //            {
            //                services.AddSingleton<IAmqpService, AmqpService>();
            //            }
            //            else
            //            {
            //                services.AddSingleton<IAmqpService, NoAmqpService>();
            //            }
            //services.AddVoglerAmqpService(x => Configuration.GetSection(nameof(AmqpServiceConfig)).Bind(x));


            var connectionString = Configuration.GetValue <string>("AmqpServiceConfig:ConnectionString");
            var serviceName      = Configuration.GetValue <string>("ServiceName");

            //Setup web proxy from config
            services.ConfigureWebProxy(options =>
                                       Configuration.GetSection(nameof(ProxyConfig)).Bind(options));

            // Add Vogler Amqp Service
            services.AddVoglerAmqpService(options =>
            {
                options.ClientName       = serviceName;
                options.ConnectionString = connectionString;
            }
                                          );
            services.AddVoglerFcmService(options =>
                                         Configuration.GetSection(nameof(FcmServiceConfig)).Bind(options));

            services.AddSingleton <IAuthService, AuthService>();

            var db = new LiteDBPersistenceProvider(Path.Combine(ContentRoot, "SmartDevicesGateway.db"));

            services.AddSingleton <IPersistenceProvider>(db);

            //Add processing services
            services.AddSingleton <ConfigHandler>();
            services.AddSingleton <MessageHandler>();
            services.AddSingleton <ValueHandler>();
            services.AddTransient <DeviceInfoHandler>();
            services.AddSingleton <FcmMessageHandler>();
            services.AddSingleton <TodoListHandler>();

            //Add models as transient
            services.AddTransient <ConfigChangeModel>();
            services.AddTransient <AuthModel>();
            services.AddTransient <ConfigModel>();
            services.AddTransient <DataModel>();
            services.AddTransient <ActionModel>();
            services.AddTransient <JobModel>();
            services.AddTransient <ResourceModel>();
            services.AddTransient <TodoListModel>();
            services.AddTransient <ApkModel>();

            //Add Background Services
            services.AddSingleton <IHostedService, ConfigChangeListenerService>();
            services.AddSingleton <IHostedService, AmqpListenerService>();
        }