c# - 如何从 Azure Service Fabric 无状态服务访问 appsettings.json

标签 c# azure asp.net-core azure-service-fabric

我有一个包含无状态服务项目 (.sfproj) 和相应的控制台应用程序项目的解决方案。

我已将 appsettings.json 添加到控制台应用程序项目中,并尝试使用以下命令设置对其的访问:

protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
    //return this.CreateServiceRemotingInstanceListeners(); //This line works
    return new ServiceInstanceListener[] //This line causes an error in Service Fabric Explorer
    {
        new ServiceInstanceListener(serviceContext =>
            new KestrelCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
            {
                ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting Kestrel on {url}");

                return new WebHostBuilder()
                    .UseKestrel()
                    .ConfigureAppConfiguration((builderContext, config) =>
                    {
                        config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
                    })
                    .ConfigureServices(
                        services => services
                            .AddSingleton<StatelessServiceContext>(serviceContext))
                    .UseContentRoot(Directory.GetCurrentDirectory())
                    //.UseStartup<Startup>() //Startup.cs does not exist
                    .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
                    .UseUrls(url)
                    .Build();
            }))
    };
}

这无法编译,因为我没有 Startup类,因为它不是一个 Web api 项目,它使用的是无状态服务。我在控制台应用程序中拥有的唯一类是 Program.cs , Service.csServiceEventSource.cs

我尝试删除 .UseStartup<Startup>()让它编译,但是当我运行该应用程序时,我在 Service Fabric Explorer 中收到以下错误:

'System.FM' reported Error for property 'State'.
Partition is below target replica or instance count.
fabric:/Integration/Integration.Service 1 1 9810816d-5be9-4b53-9eed-c5be403219b0
  InBuild _Node_1 132003711574580709
  (Showing 1 out of 1 instances. Total available instances: 0)

我可以使用return this.CreateServiceRemotingInstanceListeners();来解决这个问题相反,但是我不知道如何设置对 appsettings.json 的访问。

如何访问appsettings.json来 self 的无状态服务?

编辑:

我设法在 Service.cs 中使用它让它工作:

        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            Microsoft.AspNetCore.WebHost.CreateDefaultBuilder()
                .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();
                })
                .UseStartup<Startup>()
                .Build()
                .Run();
        }

我将其添加到 ServiceManifest.xml确保env.ContentRootPath指向包含 appsettings.json 的代码包文件夹文件(默认似乎是工作文件夹,对我来说是空的):

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

我添加了这个Startup类:

public class Startup
{
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    { }
}

我觉得一定有更好的方法来访问 appsettings.json ,特别是考虑到Startup.Configure()方法什么也不做。

有谁知道更好的访问方式appsettings.json

最佳答案

发生这种情况是因为您必须指定运行应用程序入口点的程序集的名称。 docs解释一下应该如何设置。

当您使用.UseStartup<Startup>()时它从 Startup 获取程序集信息类,因为你没有类,所以它不知道入口点在哪里。

This github issuethis显示类似的问题。

这是我从在 SF 运行的一个应用程序中获得的示例:

        var builder = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .ConfigureServices(services => services.AddSingleton(startup))
            .UseApplicationInsights()
            .UseSetting(WebHostDefaults.ApplicationKey, startup.GetType().Assembly.FullName);

在上面的示例中,startup是实现 Microsoft.AspNetCore.Hosting.IStartup 的类的实例使用以下方法:

    void Configure(IApplicationBuilder app);
    IServiceProvider ConfigureServices(IServiceCollection services);

PS:你不使用.UseStartup<Startup>()在上面的示例中,但如果项目中有一个则可以。

关于c# - 如何从 Azure Service Fabric 无状态服务访问 appsettings.json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55788289/

相关文章:

c# - 在 C# 中实现 DDD 实体类

c# - 将事件绑定(bind)到使用 WPF 中的数据模板创建的控件

git - 使用基于 xproj 的 ASP.NET Core 项目时,Azure 构建停止工作

c# - WEB.API 不会在 POST 方法中反序列化传入的 JSON

asp.net-core - 如何在中间件类中访问 IHostingEnvironment

c# - 定时器比 System.Threading.Timer 更可靠

c# - 如何使用 linq 按集合中的属性排序?

azure - 在没有 azure 门户的情况下启动 azure 虚拟机

azure - .net 6.0 与 nuget 包的兼容性问题

c# - appsettings-configuration 中的 Serilog PushProperty 不起作用