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!

2
  • Put the values in appsettings.json and map them with Configuration: stackoverflow.com/questions/69390676/… Commented Jul 28, 2022 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. Commented Jul 28, 2022 at 19:21

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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
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.
Do you mean in the ClientIdValues class?
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
0

The way I have found to do this is with the implementation factory

In your case, the code would look like this:

services.AddScoped<IClientIdValues, ClientIdValues>((sp) => {
    var clientIdValues = new ClientIdValues();
    clientIdValues.clientIds = stringArray;
    return clientIdValues;
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.