简体   繁体   English

如何读取Service Fabric Azure App Configuration ASP.NET Core Stateless Web API?

[英]How to read the Azure App Configuration in Service Fabric ASP.NET Core Stateless Web API?

I need help to read the App Configuration in Service Fabric ASP.NET Core Stateless Web API. In the Normal ASP.NET Core Web API, we can use the Host CreateDefaultBuilder to read the config and use it in the Startup and other classes.我需要帮助阅读 Service Fabric ASP.NET Core Stateless Web API 中的 App Configuration。在 Normal ASP.NET Core Web API 中,我们可以使用 Host CreateDefaultBuilder 读取配置并在 Startup 中使用它。 If I try to inject in the Service Fabric Web API, it does not work.如果我尝试在 Service Fabric Web API 中注入,它不起作用。 The Program.cs contains only the following. Program.cs 仅包含以下内容。

private static void Main(string[] args)
            {
                try
                {
                    // The ServiceManifest.XML file defines one or more service type names.
                    // Registering a service maps a service type name to a .NET type.
                    // When Service Fabric creates an instance of this service type,
                    // an instance of the class is created in this host process.
    
                    ServiceRuntime.RegisterServiceAsync("EmailAPIType",
                        context => new EmailAPI(context)).GetAwaiter().GetResult();
    
                    ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(EmailAPI).Name);
    
    
                    // Prevents this host process from terminating so services keeps running. 
                    Thread.Sleep(Timeout.Infinite);
                }
                catch (Exception e)
                {
                    ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString());
                    throw;
                }
            }

And the startup.cs contains并且 startup.cs 包含

    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.HttpsPolicy;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Logging;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace EmailAPI
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllers();
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
    
                app.UseHttpsRedirection();
    
                app.UseRouting();
    
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });
            }
        }
    }

I tried to inject Host CreateDefaultBuilder in program.cs我试图在 program.cs 中注入 Host CreateDefaultBuilder

Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                    webBuilder.ConfigureAppConfiguration((hostingContext, config) =>
                        {
                            var settings = config.Build();




                            config.AddAzureAppConfiguration(options =>
                            {
                                options.Connect(ConnectionString)
                                    .Select(ConfigValues).TrimKeyPrefix(qualtricsAppConfigPrefix + ":")
                                    .UseFeatureFlags();
                            });
                        })
                        .UseStartup<Startup>());

I am running out of Ideas how to do.我的想法已经用完了。 In Azure Function App we can do it in Startup, not sure how we can handle in Service Fabric ASP.NET Core Web API. Any examples please.在 Azure Function App 中,我们可以在 Startup 中完成,不确定我们如何在 Service Fabric 中处理 ASP.NET Core Web API。请提供任何示例。

I have uploaded the sample project created in One Drive.我已经上传了在 One Drive 中创建的示例项目。 Here is the link to it.这是它的链接。

https://1drv.ms/u/s!Au2rKbF-hqWY61pykRlWRTI4DB8t?e=vz0c8z

Finally figured it out.终于想通了。 For anyone who is interested here is it.对于任何对这里感兴趣的人来说都是如此。 If you have any better way to do it please let me know如果您有更好的方法,请告诉我

    public class Startup
    {
        private static string prefix = "";

        public Startup(IConfiguration configuration)
        {
            ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

            configurationBuilder
                .AddEnvironmentVariables()
                .AddJsonFile("appsettings.json", false, false)
                .AddEnvironmentVariables();


            var builder = configurationBuilder.Build();

            configurationBuilder.AddAzureAppConfiguration(o => AddApplicationKeysAppConfiguration(o, builder));
            builder = configurationBuilder.Build();

            configuration = builder;

            Configuration = configuration;
        }

        private static void AddApplicationKeysAppConfiguration(AzureAppConfigurationOptions options, IConfigurationRoot configuration)
        {
            var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            environment = string.IsNullOrWhiteSpace(environment) ? Environment.GetEnvironmentVariable("Environment") : environment;


            string connectionString = "";

            options.Connect(connectionString)
                .Select($"{prefix}*", environment).TrimKeyPrefix(prefix + ":")
                .UseFeatureFlags(flagOptions =>
                {
                    flagOptions.Label = environment;
                });
        }



        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure 部署ASP.Net后服务应用不可用 Web 核心应用 - Azure Service App Becomes Unavailable After Deploying ASP.Net Web Core App 如何手动将 ASP.NET 核心应用程序的部署版本复制到 Azure web 应用程序(应用服务) - How can I manually copy a deployment build of ASP.NET Core application to Azure web application (App Service) 从ASP.NET Core访问Azure App Service ConnectionString - Access Azure App Service ConnectionString from ASP.NET Core 命名信号量在 Azure App Service 上托管的 ASP.Net Core 5 Web API 上不起作用 - Named semaphore not working on ASP .Net Core 5 Web API hosted on Azure App Service ASP.net 核心 docker https on Azure 应用服务容器 - ASP.net core docker https on Azure App Service Containers 如何从 .Net Frame Asp.Net 应用程序中的 Azure App Service 配置获取连接字符串? - How do I get a connection string from Azure App Service configuration in a .Net Frame Asp.Net application? Azure Web 应用程序与 Linux 并将 GLIBC 2.29 与我的 ASP.NET Core 7 应用程序捆绑在一起 - Azure Web App with Linux and bundling GLIBC 2.29 with my ASP.NET Core 7 App 带有数据库的 ASP.Net Core Web 应用程序使用 Visual Studio 2019 发布到 Azure - 实体框架迁移错误 - ASP.Net Core Web App with Database publish to Azure with Visual Studio 2019 - Error with Entity Framework Migrations 在 Asp.net web 应用程序部署在 azure 应用程序服务中,获取服务(.svc)端点的 404 未找到错误 - Getting 404 not found error for service(.svc) endpoints in Asp.net web application deployed on azure app service 如何配置.NET 7 Web API部署到Azure应用服务 - How to configure .NET 7 Web API for deploying to Azure App service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM