I’m struggling to access a service when configuring a factory function in our app setup.
We’re using Asp.Versioning, and I’m trying to inject a service for use in our custom model builder.
Here is the relevant configuration section
builder.Services.AddSingleton(Of EdmTypeReplacerProvider)
builder.Services.AddApiVersioning(Sub(o)
o.DefaultApiVersion = ApiVersions.V441
o.AssumeDefaultVersionWhenUnspecified = True
o.ReportApiVersions = True
o.ApiVersionReader = New HeaderApiVersionReader("Api-Version")
End Sub) _
.AddMvc() _
.AddOData(Sub(o)
o.AddRouteComponents("api",
Sub(c)
c.AddSingleton(Of SkipTokenHandler, CustomSkipTokenHandler)
c.AddSingleton(Of ODataResourceSerializer, CssColourResourceSerializer)
c.AddSingleton(Of ODataResourceDeserializer, CssColourResourceDeserializer)
End Sub)
' This is where I want to inject `EdmTypeReplacerProvider`
o.ModelBuilder.ModelBuilderFactory = Function() New CustomConventionODataModelBuilder
End Sub) _
.AddODataApiExplorer(Sub(o)
o.DefaultApiVersion = ApiVersions.V441
End Sub)
I’m trying to inject the EdmTypeReplacerProvider
into CustomConventionODataModelBuilder
, but I can’t pass a service to the ModelBuilderFactory
Function
params because it is expecting an empty function delegate.
I’m also not sure how/whether I can access the ServiceProvider as it’s still being built.
I’ve currently been forced to create EdmTypeReplacerProvider
as a static class singleton instance and use that, but I’m convinced there must be a way to inject this, or at least access the ServiceProvider
in the factory function.
Feel free to give any answers in C# instead of VB, if you’re more familiar with that.