Search code examples
piranha-cms

Inject IOptionsSnapshot in Block constructor


I created a new custom Block and wanted to inject an IOptionsSnapshot to read my appsettings.json values. The problem is that I get an error saying there is no parameterless constructor for my custom block.

Is there a way to somehow do this injection or is this a limitation in Piranha and custom blocks.

enter image description here


Solution

  • At the moment neither Fields nor Blocks supports parameter injection into the constructor, however Fields have two initialization methods that both support parameter injection, Init() and InitManager(). Given how models are constructed the easiest solution would probably be to add the corresponding init methods to Blocks as well.

    Feel free to open an issue/feature request at the GitHub repo and we can take the discussion from there!