如何设置 IHostingEnvironment.ContentRootPath?

2024-03-30

在我的 Azure Service Fabric Web API 项目中,我可以使用以下代码将 appsettings.json 文件添加到我的配置中:Api.cs class:

    protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
    {
        return new ServiceInstanceListener[]
        {
            new ServiceInstanceListener(serviceContext =>
                new KestrelCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
                {
                    ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting Kestrel on {url}");

                    return new WebHostBuilder()
                                .UseKestrel()
                                .ConfigureAppConfiguration((builderContext, config) =>
                                {
                                        var env = builderContext.HostingEnvironment;
                                        config.SetBasePath(env.ContentRootPath)
                                            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                                            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                                            .AddEnvironmentVariables();
                                })
                                .ConfigureServices((hostingContext, services) => services
                                .AddSingleton<StatelessServiceContext>(serviceContext)
                                .AddApplicationInsightsTelemetry(hostingContext.Configuration))
                                .UseContentRoot(Directory.GetCurrentDirectory())
                                .UseStartup<Startup>()
                                .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
                                .UseUrls(url)
                                .Build();
                }))
        };
    }

The env.ContentRootPath包含这个值:

C:\SfDevCluster\Data\_App\_Node_0\Abc.IntegrationType_App197\Abc.Integration.ApiPkg.Code.1.0.0

在此文件夹中,我可以看到 appsettings.json 文件。

但是,在我的 Azure Service Fabric 无状态服务项目中,我的代码中有此代码Service.cs class:

    protected override async Task RunAsync(CancellationToken cancellationToken)
    {
        Microsoft.AspNetCore.WebHost.CreateDefaultBuilder()
            .ConfigureAppConfiguration((builderContext, config) =>
            {
                var env = builderContext.HostingEnvironment;

                var appName = AppDomain.CurrentDomain.FriendlyName;
                var parentFolderPath = Directory.GetParent(env.ContentRootPath).FullName;
                var packageCodeFolderPath = Path.Combine(parentFolderPath, appName + "Pkg.Code.1.0.0");

                config.SetBasePath(packageCodeFolderPath)
                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                    .AddEnvironmentVariables();
            })
            .UseStartup<Startup>()
            .Build()
            .Run();
    }

Here env.ContentRootPath包含这个值:

C:\SfDevCluster\Data\_App\_Node_0\Abc.Integration.OpticalType_App198\work

然而由于appsettings.json文件位于:C:\SfDevCluster\Data\_App\_Node_0\Abc.Integration.OpticalType_App198\Abc.Integration.Optical.ServicePkg.Code.1.0.0,

我被迫构建packageCodeFolderPath您可以在上面看到变量。

这似乎不是一个非常优雅的解决方案,我担心版本号Pkg.Code.1.0.0可能会改变(如果那是版本号?)。

我该如何设置env.ContentRootPath是包含该文件的文件夹的路径appsettings.json文件?或者有更好的方法来访问这些文件吗?


在您的服务清单中,您应该设置WorkingFolder to CodePackage将 ContentRootPath 设置为代码Abc.Integration.Optical.ServicePkg.Code.1.0.0代替work.

i.e:

<EntryPoint>
  <ExeHost>
    <Program>myapp.exe</Program>
    <WorkingFolder>CodePackage</WorkingFolder>
  </ExeHost>
</EntryPoint>

检查文档here https://learn.microsoft.com/en-us/azure/service-fabric/service-fabric-deploy-existing-app#use-visual-studio-to-package-and-deploy-an-existing-executable

其他选项是从 SF 设置的环境变量中获取信息。看看这里:https://learn.microsoft.com/en-us/azure/service-fabric/service-fabric-environment-variables-reference https://learn.microsoft.com/en-us/azure/service-fabric/service-fabric-environment-variables-reference

或者,使用context.CodePackageActivationContext.WorkDirectory正如 Loek 在他的回答中所建议的那样。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何设置 IHostingEnvironment.ContentRootPath? 的相关文章

随机推荐