2

I am trying to implement the options-pattern and get the following error:

"Cannot create instance of type 'System.String' because it is missing a public parameterless constructor."

My setup is as this:

I have a Controller class like this:

public class MyController : Controller
{

  private readonly HttpClient _httpClient;
  private readonly MyConfig _myConfig;

  public MyController(HttpClient httpClient, IOptions<MyConfig> myConfig){
    _httpClient = httpClient;
    _myConfig = myConfig.Value;  <<<<<<<<<<<< FAILS HERE
  }

  [HttpGet]
  public async Task<IActionResult> Get(){
  ....
  var myHttpRequest = new HttpRequestMessage(HttpMethod.Get, _myConfig.MY_ENDPOINT);
  ...
  ...
  }
}

I have a JSON file - my appsettings that goes something like this:

{
"Tracer": {
"Trace": {
  "Default": "",
  "Smari": ""
  }
},

"MY_DB": "",
"MY_USER": "",
"MY_PW": "",
"MY_ENDPOINT": "https://my-test-site/offering",
"MY_CIGO": ""
}

And in my Startup.cs I've written the following:

services.Configure<MyConfig>(Configuration);

Lastly my MyConfig class that goes like this:

public class MyConfig 
{
    public string MY_ENDPOINT { get; set; }
}

Can anyone see, why it fails? I've checked my JSON file for syntax and it seems correct, so I am a bit lost.

10
  • 1
    looks like your JSON is not valid, there are extra comma at the last item of any list for example "Smari": "", should "Smari": "" and "MY_ENDPOINT": "https://my-test-site/offering", should be "MY_ENDPOINT": "https://my-test-site/offering" Commented Feb 27, 2020 at 8:57
  • @styx - sorry thats my bad (bad copy into SO). Fixed the JSON, but the error is still the same though. Put the "new" JSON inside a validator and it passes so I dont think the error is in the JSON syntax... Commented Feb 27, 2020 at 9:02
  • What is myConfig.Value? Commented Feb 27, 2020 at 9:06
  • It has no value, but just states: myConfig.Value threw and exception of type "System.InvalidOperationException". Commented Feb 27, 2020 at 9:09
  • 1
    Let us continue this discussion in chat. Commented Feb 27, 2020 at 9:45

2 Answers 2

4

Following up on the chat discussion:

The issue was that the controller got instantiated inside of a test project by a helper class. This helper class expected an appsettings file with a given name (appsettings.test.json). But the test project didn't contain an appsettings file with the expected name. So adding an appsettings file with the correct name solved this issue.

Means for everyone else getting this error:
This error most likely occurs if something is wrong with the appsettings.json file. Either being it a format issue or the file not being existent at all. So check for the right filename and format (even if json validators validate it successfully).

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

Comments

-1

I had the same error with a slightly different problem. I was also implementing the option-pattern (as the OP calls it).

My problem was that I had a string type instead of a string[] type defined in the TOption class which had an attribute of [Required(AllowEmptyStrings = false)]. Clearly my coding error.

So, I just replaced the attribute with [Required()] and changed the type to string[].

It appears that this error occurs with either something wrong with the appsettings.json file OR the mapping of it to the TOptions class.

2 Comments

This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review
How is that helpful to anyone who comes here with a similar issue to the OP but unsure if it is identical?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.