本文介绍了如何设置IHostingEnvironment.ContentRootPath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my Azure Service Fabric Web API project, I am able to add my appsettings.json files to my config using this code in my 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();
                }))
        };
    }

env.ContentRootPath包含以下值:

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

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

In this folder I can see the appsettings.json files.

但是,在我的Azure Service Fabric无状态服务项目中,我的Service.cs类中包含以下代码:

However, in my Azure Service Fabric Stateless Service project, I have this code in my 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();
    }

此处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

However because the appsettings.json files are located in:C:\SfDevCluster\Data\_App\_Node_0\Abc.Integration.OpticalType_App198\Abc.Integration.Optical.ServicePkg.Code.1.0.0,

我被迫构造您可以在上面看到的packageCodeFolderPath变量.

I'm forced to construct the packageCodeFolderPath variable you can see above.

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

This doesn't seem like a very elegant solution, and I'm worried the version number in Pkg.Code.1.0.0 might change (if that is even a version number?).

如何将env.ContentRootPath设置为包含appsettings.json文件的文件夹的路径?还是有更好的方法来访问这些文件?

How can I set env.ContentRootPath to be the path to the folder containing the appsettings.json files? Or is there a better way to go about accessing these files?

推荐答案

在服务清单中,应将WorkingFolder设置为CodePackage,以将ContentRootPath设置为代码Abc.Integration.Optical.ServicePkg.Code.1.0.0而不是work.

In your service manifest, you should set the WorkingFolder to CodePackage to have the ContentRootPath set to the code Abc.Integration.Optical.ServicePkg.Code.1.0.0 instead of work.

即:

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

检查文档此处

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

Other option is get the information from the environment variables set by SF. Take a look in here: https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-environment-variables-reference

或者,使用Loek在其答案中建议的context.CodePackageActivationContext.WorkDirectory.

Or, use the context.CodePackageActivationContext.WorkDirectory as suggested by Loek in his answer.

这篇关于如何设置IHostingEnvironment.ContentRootPath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 07:52