Registering my own IApiContentResponseBuilder for ...
# help-with-umbraco
p
With v12 and the Delivery API - If I want to register my own
IApiContentResponseBuilder
- do I need to remove the default one first or will mine override the default when registered via an
IComposer
?
s
Looks like people are adding their own - https://trnktms.com/2023/09/27/umbraco-content-delivery-api-extending-the-api-response-with-extra-fields/ guess it depends on what you're trying to achieve.
p
Thanks @Sebastiaan , looks similar to what I have done and in this case looks like they are just adding theirs and not removing the core one first 👍
One difference with my implementation is that I am registering the
IApiContentResponseBuilder
via an
IComposer
but in doing so the site will no longer start up - it doesn't error, just doesn't appear to complete loading and hangs.
s
Weird, definitely how you should be able to do it, but I haven't played with this before
p
The final entry in the logs is always:
Copy code
{
  "@t": "2023-12-05T12:27:54.2578701Z",
  "@mt": "Duplicate field definitions found for field name {FieldName} among the index handlers - first one wins.",
  "@l": "Warning",
  "FieldName": "domain",
  "SourceContext": "Umbraco.Cms.Infrastructure.Examine.DeliveryApiContentIndexFieldDefinitionBuilder",
  "ProcessId": 64520,
  "ProcessName": "iisexpress",
  "ThreadId": 1,
  "ApplicationId": "70db158a02c3b1c83279642af486f7e5db1ff92b",
  "MachineName": "PN-MEGATRON",
  "Log4NetLevel": "WARN "
}
Currently leaving it for a while to see if it will ever complete loading but not looking hopeful at the moment.
The error above is a red herring. Whatever the issue is, it's only happening when adding my ResponseBuilder in
IComposer
. If I comment out the line below the warning above is not the last line in the logs and the site loads.
builder.Services.AddSingleton<IApiContentResponseBuilder, MyContentResponseBuilder>();
I ended up taking a different approach and implemented my own
IOutputExpansionStrategy
and have implemented my own version of the method
MapProperties(IPublishedContent content, IEnumerable<IPublishedProperty> properties)
I've needed to return to my own implementation of the
IApiContentResponseBuilder
and have finally narrowed down the issue with Umbraco hanging and never starting up. It is caused by the fact that my implementation takes an additional parameter
IMappingHelper mappingHelper
. That class takes a
IUmbracoMapper mapper
parameter. I am registering my implementations as follows:
Copy code
builder.Services.AddSingleton<IMappingHelper, MappingHelper>();
  builder.Services.AddSingleton<IApiContentResponseBuilder, BaseContentResponseBuilder>();
I am registering
IMappingHelper
as a singleton because it can't be scoped due to being a parameter for the
IApiContentResponseBuilder
singleton. I'm now banging my head against a wall - probably missing something really obvious but it's not very helpful when there is no error to provide some insight into the issue that is occurring with the service registration. Any ideas?
Resolved by removing the parameter from the
IApiContentResponseBuilder
constructor and adding it to the
IOutputExpansionStrategy
and it now all works as intended. However, I am still interested to know how to make it work in the original context and why it was not working previously.
s
Hello I'm trying to inject a custom implementation of
IApiContentResponseBuilder
, but it should be scoped in my case. That is because I need to inject my list of scoped services into it. Can I override it as scoped?
Copy code
umbracoBuilder.Services.AddScoped<IApiContentResponseBuilder, CustomDeliveryApiResponseBuilder>();
When I changed AddSingleton to AddScoped that looked working, but I want to ensure there are no pitfalls of change.
4 Views