1

hopefully this should be a simple one.

So I have this class which is just this:

public class ClientIdValues : IClientIdValues
{

   public string[] clientIds {get; set;}
}

And the interface for that class is:

public interface IClientIdValues
{
  string [] ClientIds {get; set;}
}

This class gets injected into another class and is registered in startup.cs. The class is available when injected, but it's null and so it should be, I haven't set values yet.

However, I can't seem to figure out how to

I would assume it's some form of

services.Configure<ClientIdValues>(opt => opt.clientIds = stringArray);

Thanks in advance for any help!

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Put the values in appsettings.json and map them with Configuration: https://stackoverflow.com/questions/69390676/how-to-use-appsettings-json-in-asp-net-core-6-program-cs-file – GH DevOps Jul 28 '22 at 15:01
  • Are you injecting the interface or the implementation (class). Show how you are injecting the dependency and also how it is registered in startup. – Nkosi Jul 28 '22 at 19:21

1 Answers1

0

You probably just need to add a constructor in which one you're instanciating your ClientIds property with new string[numberOfElement].

Also, if you don't know the number of elements that it will handle or if it needs to grow at some point, prefer using a List.

yguerin
  • 196
  • 1
  • 6
  • Exactly so when you will resolve your interface on the other class, it will take the instance of ClassIdValues so you will hit the constructor for sure. You could also write something like `public string[] clientIds {get; set;} = new string[0];` in your ClassIdValues class – yguerin Jul 28 '22 at 14:37
  • But again, it will instanciate an array of 0 element which means that if you need to add elements on it, you'll need to affect a new array to this property. A better way to handle it is to use a `List` and use `.Add` method so the `List` can handle its capacity by itself. – yguerin Jul 28 '22 at 14:39
  • Do you mean in the ClientIdValues class? – obiwanconobi Jul 28 '22 at 14:40
  • Yeah this should be a static list that never changes. Basically for admin client. Is there a way to set it outside of the ClientIdValues class? I don't have access to change that class atm, would be an easier fix for me if I could fix it when I register the class – obiwanconobi Jul 28 '22 at 14:42