Example #1
0
        /// <summary>
        /// This is the main entry point for your service instance.
        /// </summary>
        /// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service instance.</param>
        /// <returns>Async task</returns>
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            var factory = new AdminStoreFactory(this.configuration);

            this.store = factory.GetStore();
            this.cache = new RedisClient(
                this.configuration.TenantCache_DefaultConnectionString,
                this.configuration.TenantCache_DatabaseId);

            var quotaCache = new RedisClient(
                this.configuration.TenantCache_DefaultConnectionString,
                this.configuration.TenantCache_QuotaDatabaseId);

            this.quotaManager = new QuotaManager(
                this.Context,
                this.store,
                quotaCache);

            while (!cancellationToken.IsCancellationRequested)
            {
                var trackingId = Guid.NewGuid().ToString();

                try
                {
                    await this.quotaManager.SynchronizeAsync(trackingId);
                }
                catch (Exception ex)
                {
                    TenantManagementEventSource.Current.TraceException(
                        trackingId,
                        this.Context.NodeContext.NodeName,
                        "Exception raised in synchronize loop",
                        ex);
                }

                await Task.Delay(TimeSpan.FromSeconds(1));
            }
        }
Example #2
0
        public static void ConfigureApp(IAppBuilder appBuilder)
        {
            // Configure Web API for self-host.
            var config = new HttpConfiguration();

            // Configure format
            var jsonFormatter = new JsonMediaTypeFormatter();

            jsonFormatter.SerializerSettings.Formatting       = Formatting.Indented;
            jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

            config.Formatters.Clear();
            config.Formatters.Insert(0, jsonFormatter);

            config.MapHttpAttributeRoutes();

            // SAS Authentication
            var factory = new AdminStoreFactory();
            var store   = factory.GetStore();

            config.Properties[SASAuthorizeAttribute.ConfigurationPropertyKey] = new SASAuthorizeAttribute.GetKeysAsyncFunc(
                async(accountName, keyNames) => await store.GetKeysAsync(accountName, keyNames));

            config.Properties[SASAuthorizeAttribute.AuthenticationFailureHandlerKey] = new SASAuthorizeAttribute.OnAuthenticationFailed(
                async(exception, request) =>
            {
                var account        = RequestHelper.ParseAccount(request);
                var subscriptionId = await RequestHelper.GetSubscriptionId(account);

                MetricManager.Instance.LogRequestFailed4xx(1, account, subscriptionId, string.Empty);
            });

            config.Services.Replace(typeof(IExceptionHandler), new CustomExceptionHandler());
            config.MessageHandlers.Add(new ApiTrackHandler());

            // Certificate Authentication
            config.Properties[CertificateBasedAuthorizeAttribute.ValidClientCertificateKey] = new Func <X509Certificate2, bool>(
                (cert) => CertificateHelper.ValidClientCertificate(cert));

            config.Properties[CertificateBasedAuthorizeAttribute.AuthenticationFailureHandlerKey] = new Action <Exception>(
                (exception) => MetricManager.Instance.LogRequestFailed4xx(1, "Acis", "Acis", string.Empty));

            // Temp for ibiza extension
            var exposeHeaders = new List <string>
            {
                Constants.OperationTrackingIdHeader,
                ContinuationToken.ContinuationTokenKey
            };
            var cors = new EnableCorsAttribute("*", "*", "*", string.Join(",", exposeHeaders));

            config.EnableCors(cors);

            // Publish static content
            var physicalFileSystem = new PhysicalFileSystem(@"./StaticContent");
            var options            = new FileServerOptions
            {
                EnableDefaultFiles = true,
                FileSystem         = physicalFileSystem,
            };

            options.StaticFileOptions.FileSystem            = physicalFileSystem;
            options.StaticFileOptions.ServeUnknownFileTypes = true;
            appBuilder.UseFileServer(options);

            // Enable Swagger UI
            var swaggerEnabledConfig = new SwaggerEnabledConfiguration(config, SwaggerDocsConfig.DefaultRootUrlResolver, new[] { "Swagger/2018-10-01.json" });

            swaggerEnabledConfig.EnableSwaggerUi();

            // TLS1.2+: Enable Connection Logging
            appBuilder.Use <ConnectionLogMiddleware>("Microsoft.Azure.EngagementFabric.RequestListener");

            appBuilder.UseWebApi(config);

            config.EnsureInitialized();
        }