Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Startup"/> class.
        /// </summary>
        /// <param name="config">The <see cref="IConfiguration"/>.</param>
        /// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
        public Startup(IConfiguration config, ILoggerFactory loggerFactory)
        {
            // Use JSON property names in validation; and determine whether unhandled exception details are to be included in the response.
            ValidationArgs.DefaultUseJsonNames = true;
            WebApiExceptionHandlerMiddleware.IncludeUnhandledExceptionInResponse = config.GetValue <bool>("BeefIncludeExceptionInInternalServerError");

            // Add "page" and "page-size" to the supported paging query string parameters as defined by the CDR specification; and default the page size to 25 from config.
            WebApiQueryString.PagingArgsPageQueryStringNames.Add("page");
            WebApiQueryString.PagingArgsTakeQueryStringNames.Add("page-size");
            PagingArgs.DefaultTake = config.GetValue <int>("BeefDefaultPageSize");

            // Configure the logger.
            _logger = loggerFactory.CreateLogger("Logging");
            Logger.RegisterGlobal((largs) => WebApiStartup.BindLogger(_logger, largs));

            // Configure the cache policies.
            CachePolicyManager.SetFromCachePolicyConfig(config.GetSection("BeefCaching").Get <CachePolicyConfig>());
            CachePolicyManager.StartFlushTimer(CachePolicyManager.TenMinutes, CachePolicyManager.FiveMinutes);

            // Register the DocumentDb/CosmosDb client.
            CosmosDb.Register(() =>
            {
                var cs = config.GetSection("CosmosDb");
                return(new CosmosDb(new Microsoft.Azure.Cosmos.CosmosClient(cs.GetValue <string>("EndPoint"), cs.GetValue <string>("AuthKey")), cs.GetValue <string>("Database")));
            });

            // Register the ReferenceData provider.
            Beef.RefData.ReferenceDataManager.Register(new ReferenceDataProvider());

            // Register the "customised" execution context.
            Beef.ExecutionContext.Register(() => new ExecutionContext());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Startup"/> class.
        /// </summary>
        /// <param name="config">The <see cref="IConfiguration"/>.</param>
        public Startup(IConfiguration config)
        {
            // Use JSON property names in validation.
            ValidationArgs.DefaultUseJsonNames = true;

            // Load the cache policies.
            CachePolicyManager.SetFromCachePolicyConfig(config.GetSection("BeefCaching").Get <CachePolicyConfig>());
            CachePolicyManager.StartFlushTimer(CachePolicyManager.TenMinutes, CachePolicyManager.FiveMinutes);

            // Register the ReferenceData provider.
            Beef.RefData.ReferenceDataManager.Register(new ReferenceDataProvider());

#if (implement_database || implement_entityframework)
            // Register the database.
            Database.Register(() => new Database(WebApiStartup.GetConnectionString(config, "Database")));
#endif
#if (implement_cosmos)
            // Register the DocumentDb/CosmosDb client.
            CosmosDb.Register(() =>
            {
                var cs = config.GetSection("CosmosDb");
                return(new CosmosDb(new Cosmos.CosmosClient(cs.GetValue <string>("EndPoint"), cs.GetValue <string>("AuthKey")), cs.GetValue <string>("Database")));
            });
#endif
            // Default the page size.
            PagingArgs.DefaultTake = config.GetValue <int>("BeefDefaultPageSize");
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Startup"/> class.
        /// </summary>
        /// <param name="config">The <see cref="IConfiguration"/>.</param>
        /// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
        public Startup(IConfiguration config, ILoggerFactory loggerFactory)
        {
            // Use JSON property names in validation; default the page size and determine whether unhandled exception details are to be included in the response.
            ValidationArgs.DefaultUseJsonNames = true;
            PagingArgs.DefaultTake             = config.GetValue <int>("BeefDefaultPageSize");
            WebApiExceptionHandlerMiddleware.IncludeUnhandledExceptionInResponse = config.GetValue <bool>("BeefIncludeExceptionInInternalServerError");

            // Configure the logger.
            _logger = loggerFactory.CreateLogger("Logging");
            Logger.RegisterGlobal((largs) => WebApiStartup.BindLogger(_logger, largs));

            // Configure the cache policies.
            CachePolicyManager.SetFromCachePolicyConfig(config.GetSection("BeefCaching").Get <CachePolicyConfig>());
            CachePolicyManager.StartFlushTimer(CachePolicyManager.TenMinutes, CachePolicyManager.FiveMinutes);

#if (implement_database || implement_entityframework)
            // Register the database.
            AppNameDb.Register(() => new AppNameDb(WebApiStartup.GetConnectionString(config, "Database")));
#endif
#if (implement_cosmos)
            // Register the DocumentDb/CosmosDb client.
            AppNameCosmosDb.Register(() =>
            {
                var cs = config.GetSection("CosmosDb");
                return(new AppNameCosmosDb(new CosmosClient(cs.GetValue <string>("EndPoint"), cs.GetValue <string>("AuthKey")), cs.GetValue <string>("Database")));
            });
#endif
            // Register the ReferenceData provider.
            Beef.RefData.ReferenceDataManager.Register(new ReferenceDataProvider());
        }
Ejemplo n.º 4
0
        public Startup(IConfiguration config)
        {
            // Use JSON property names in validation.
            ValidationArgs.DefaultUseJsonNames = true;

            // Load the cache policies.
            CachePolicyManager.SetFromCachePolicyConfig(config.GetSection("BeefCaching").Get <CachePolicyConfig>());
            CachePolicyManager.StartFlushTimer(CachePolicyManager.TenMinutes, CachePolicyManager.FiveMinutes);

            // Register the ReferenceData provider.
            RefData.ReferenceDataManager.Register(new ReferenceDataProvider());

            // Register the database.
            Database.Register(() => new Database(WebApiStartup.GetConnectionString(config, "BeefDemo")));

            // Register the DocumentDb/CosmosDb client.
            CosmosDb.Register(() =>
            {
                var cs = config.GetSection("CosmosDb");
                return(new CosmosDb(new Cosmos.CosmosClient(cs.GetValue <string>("EndPoint"), cs.GetValue <string>("AuthKey")), cs.GetValue <string>("Database")));
            });

            // Register the test OData service.
            TestOData.Register(() => new TestOData(WebApiStartup.GetConnectionString(config, "TestOData")));

            // Default the page size.
            PagingArgs.DefaultTake = config.GetValue <int>("BeefDefaultPageSize");

            // Configure the Service Agents from the configuration and register.
            var sac = config.GetSection("BeefServiceAgents").Get <WebApiServiceAgentConfig>();

            sac?.RegisterAll();
        }
Ejemplo n.º 5
0
        public Startup(IConfiguration config)
        {
            // Use JSON property names in validation.
            ValidationArgs.DefaultUseJsonNames = true;

            // Load the cache policies.
            CachePolicyManager.SetFromCachePolicyConfig(config.GetSection("BeefCaching").Get <CachePolicyConfig>());
            CachePolicyManager.StartFlushTimer(CachePolicyManager.TenMinutes, CachePolicyManager.FiveMinutes);

            // Register the ReferenceData provider.
            RefData.ReferenceDataManager.Register(new ReferenceDataProvider());

            // Register the database.
            Database.Register(() => new Database(WebApiStartup.GetConnectionString(config, "BeefDemo")));
            Beef.Data.Database.DatabaseInvoker.Default = new Beef.Data.Database.SqlRetryDatabaseInvoker();

            // Register the DocumentDb/CosmosDb client.
            CosmosDb.Register(() =>
            {
                var cs = config.GetSection("CosmosDb");
                return(new CosmosDb(new Cosmos.CosmosClient(cs.GetValue <string>("EndPoint"), cs.GetValue <string>("AuthKey")), cs.GetValue <string>("Database")));
            });

            // Register the test OData services.
            TestOData.Register(() => new TestOData(new Uri(WebApiStartup.GetConnectionString(config, "TestOData"))));
            TripOData.Register(() => new TripOData(new Uri(WebApiStartup.GetConnectionString(config, "TripOData"))));

            // Default the page size.
            PagingArgs.DefaultTake = config.GetValue <int>("BeefDefaultPageSize");

            // Configure the Service Agents from the configuration and register.
            var sac = config.GetSection("BeefServiceAgents").Get <WebApiServiceAgentConfig>();

            sac?.RegisterAll();

            // Set up the event publishing to event hubs.
            if (config.GetValue <bool>("EventHubPublishing"))
            {
                var ehc = EventHubClient.CreateFromConnectionString(config.GetValue <string>("EventHubConnectionString"));
                ehc.RetryPolicy = RetryPolicy.Default;
                var ehp = new EventHubPublisher(ehc);
                Event.Register((events) => ehp.Publish(events));
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Adds a singleton service to instantiate a new <see cref="CachePolicyManager"/> instance with the specified <paramref name="config"/>, <paramref name="flushDueTime"/> and <paramref name="flushPeriod"/>.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/>.</param>
        /// <param name="config">The optional <see cref="CachePolicyConfig"/>.</param>
        /// <param name="flushDueTime">The optional amount of time to delay before <see cref="CachePolicyManager.Flush"/> is invoked for the first time (defaults to <see cref="CachePolicyManager.TenMinutes"/>).</param>
        /// <param name="flushPeriod">The optional time interval between subsequent invocations of <see cref="CachePolicyManager.Flush"/> (defaults to <see cref="CachePolicyManager.FiveMinutes"/>).</param>
        /// <returns>The <see cref="IServiceCollection"/> for fluent-style method-chaining.</returns>
        /// <remarks>The The <see cref="CachePolicyManager"/> enables the centralised management of <see cref="ICachePolicy"/> caches.</remarks>
        public static IServiceCollection AddBeefCachePolicyManager(this IServiceCollection services, CachePolicyConfig?config = null, TimeSpan?flushDueTime = null, TimeSpan?flushPeriod = null)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            return(services.AddSingleton(_ =>
            {
                var cpm = new CachePolicyManager();
                if (config != null)
                {
                    cpm.SetFromCachePolicyConfig(config);
                }

                cpm.StartFlushTimer(flushDueTime ?? CachePolicyManager.TenMinutes, flushPeriod ?? CachePolicyManager.FiveMinutes);
                return cpm;
            }));
        }
Ejemplo n.º 7
0
        public void Configure(IWebJobsBuilder builder)
        {
            var config = builder.GetConfiguration(typeof(Startup).Assembly, environmentVariablesPrefix: "Beef_");

            // Load the cache policies.
            CachePolicyManager.SetFromCachePolicyConfig(config.GetSection("BeefCaching").Get <CachePolicyConfig>());
            CachePolicyManager.StartFlushTimer(CachePolicyManager.TenMinutes, CachePolicyManager.FiveMinutes);

            // Register the ReferenceData provider.
            RefData.ReferenceDataManager.Register(new ReferenceDataProvider());

            // Register the database.
            Database.Register(() => new Database(config.GetConnectionString("BeefDemo")));

            // Register the DocumentDb/CosmosDb client.
            CosmosDb.Register(() =>
            {
                var cs = config.GetSection("CosmosDb");
                return(new CosmosDb(new Microsoft.Azure.Cosmos.CosmosClient(cs.GetValue <string>("EndPoint"), cs.GetValue <string>("AuthKey")), cs.GetValue <string>("Database")));
            });
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Adds a singleton service to instantiate a new <see cref="CachePolicyManager"/> instance with the specified <paramref name="config"/> and starts the corresponding <see cref="CachePolicyManagerServiceHost"/> with the <paramref name="firstInterval"/> and <paramref name="interval"/>.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/>.</param>
        /// <param name="config">The optional <see cref="CachePolicyConfig"/>.</param>
        /// <param name="firstInterval">The optional <see cref="CachePolicyManagerServiceHost"/> <see cref="TimerHostedServiceBase.FirstInterval"/> before <see cref="CachePolicyManager.Flush"/> is invoked for the first time (defaults to <see cref="CachePolicyManager.TenMinutes"/>).</param>
        /// <param name="interval">The optional <see cref="CachePolicyManagerServiceHost"/> <see cref="TimerHostedServiceBase.FirstInterval"/> between subsequent invocations of <see cref="CachePolicyManager.Flush"/> (defaults to <see cref="CachePolicyManager.FiveMinutes"/>).</param>
        /// <param name="useCachePolicyManagerTimer">Indicates whether the <see cref="CachePolicyManager.StartFlushTimer(TimeSpan, TimeSpan, ILogger{CachePolicyManager}?)"/> should be used; versus, being managed via <c>"IServiceCollection.AddHostedService"</c> <see cref="CachePolicyManagerServiceHost"/> (default).</param>
        /// <returns>The <see cref="IServiceCollection"/> for fluent-style method-chaining.</returns>
        /// <remarks>The <see cref="CachePolicyManager"/> enables the centralised management of <see cref="ICachePolicy"/> caches.</remarks>
        public static IServiceCollection AddBeefCachePolicyManager(this IServiceCollection services, CachePolicyConfig?config = null, TimeSpan?firstInterval = null, TimeSpan?interval = null, bool useCachePolicyManagerTimer = false)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            var cpm = new CachePolicyManager();

            if (config != null)
            {
                cpm.SetFromCachePolicyConfig(config);
            }

            firstInterval ??= CachePolicyManager.TenMinutes;
            interval ??= CachePolicyManager.FiveMinutes;

            services.AddSingleton(sp =>
            {
                if (useCachePolicyManagerTimer)
                {
                    cpm.StartFlushTimer(firstInterval.Value, interval.Value, sp.GetService <ILogger <CachePolicyManager> >());
                }

                return(cpm);
            });

            if (!useCachePolicyManagerTimer)
            {
                services.AddHostedService(sp => new CachePolicyManagerServiceHost(cpm, sp, sp.GetRequiredService <ILogger <CachePolicyManager> >())
                {
                    FirstInterval = firstInterval, Interval = interval.Value
                });
            }

            return(services);
        }