Home > Mobile >  service does not load object array
service does not load object array

Time:01-04

I've made a service class with configuration options. I'm trying to load array data from appsettings.json but, it doesn't pass data to my service.

Here my initializer for service

        public static IServiceCollection AddHomePage(
                       this IServiceCollection services,
                       Action<List<HomePageServiceOptions>> setupAction = null)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (setupAction == null)
            {
                throw new ArgumentNullException(nameof(setupAction));
            }
            services.AddTransient<IHomePageService, HomePageService>();
            services.Configure(setupAction);
            return services;
        }

I'm fetching data from appsettings.json in Startup.cs and calling extension above.

//here is made list variable to make sure data is not null here
//and I confirmed that it have 1 record. That is what I expected.
var list = Configuration.GetSection("HomePageTemplates").Get<List<HomePageServiceOptions>>();

//here calling extension above with setupAction parameter (list)
services.AddHomePage(o => o = list);

Here is my service constructor, where the problem begins with data

public class HomePageService : IHomePageService
    {
        private readonly ApplicationDbContext context;
        private readonly ILogger<HomePageService> logger;
        private readonly List<HomePageServiceOptions> options;

        public HomePageService(
            ApplicationDbContext context, 
            ILogger<HomePageService> logger, 
            Microsoft.Extensions.Options.IOptions<List<HomePageServiceOptions>> options)
        {
            if (options is null || options.Value is null)
            {
                logger.LogError("parameter options was null");
                throw new ArgumentNullException(nameof(options));
            }
            this.options = options.Value.ToList();//value count is always 0 here
            this.context = context;
            this.logger = logger;
        }
    }

Here is my HomePageServiceOptions

public class HomePageServiceOptions
    {
        public HomePageServiceOptions()
        {

        }
        
        public DesignType DesignType { get; set; }
        public string ViewPath { get; set; }
        public string PreviewImage { get; set; }
        public List<Section> Sections { get; set; }
        public List<Section> SubSections { get; set; }
    }
    public class Section
    {
        public Section()
        {

        }
        public string InputType { get; set; }
        public string Description { get; set; }
    }
public enum DesignType
    {
        Type1,
        Type2,
        Type3,
//...
    }

And here my data inside of appsettings.json

"HomePageTemplates": [
      {
        "ViewPath": "_team.cshtml",
        "PreviewImage": "_team.png",
        "DesignType": "Type1",
        "Sections": [
          {
            "InputType": "text",
            "Description": "Title"
          },
          {
            "InputType": "text",
            "Description": "Title2"
          },
          {
            "InputType": "text",
            "Description": "Extra text"
          },
          {
            "InputType": "text",
            "Description": "Button text"
          }
        ],
        "SubSections": [
          {
            "InputType": "image",
            "Description": "Person image 164x148"
          },
          {
            "InputType": "text",
            "Description": "Person name"
          },
          {
            "InputType": "text",
            "Description": "Person title"
          }
        ]
      }
    ],

I've tried to change listing type of options as array and as IEnumerable but, still same result. Is there I need something special to pass array or list data into services?

here is screenshots about data Startup.cs enter image description here

HomePageService.cs enter image description here

CodePudding user response:

  1. Add file appsettings.Development.json and save in this file all values from appsettings.json

  2. In Path add field "ASPNETCORE_ENVIRONMENT" with "Development". After - restart computer

  3. In Startup.cs in ConfigureServices... add

    services.AddOptions();

  •  Tags:  
  • Related