RazorLight icon indicating copy to clipboard operation
RazorLight copied to clipboard

RazorLightDependencyBuilder ExcludeAssemblies

Open mikart143 opened this issue 3 years ago • 2 comments

So this builder is basically broken. When adding ExcludedAssemblies via method ExcludeAssemblies() the DefaultMetadataReferenceManager will not receive those elements, because it was added to services (via services.TryAddSingleton<IMetadataReferenceManager, DefaultMetadataReferenceManager>();) before finishing setting the RazorLightOptions. The constructor in DefaultMetadataReferenceManager with IOptions<RazorLightOptions> will receive outdated data (empty list in it). So my suggestion is to remove AddRazorLight(this IServiceCollection services) and replace this with AddRazorLight(this IServiceCollection services, Action<RazorLightOptions> opts); This way we will be able to init all options before registering all other services.

mikart143 avatar Mar 30 '22 14:03 mikart143

Can you please provide a sample of what is wrong. I dont really look at my configuration stuff very often so a repro is super time saving. For context, in the past, I would guess what the heck people were doing and it would take hours or days to reproduce, until I found a blog post on the Internet and realized everyone was copying some dude's shitty code.

jzabroski avatar Mar 30 '22 20:03 jzabroski

@mikart143 Just following up on this.

If I understand your point, it's that the following 3 methods are not composable:

https://github.com/toddams/RazorLight/blob/master/src/RazorLight/RazorLightDependencyBuilder.cs#L65-L94

And that you think we should address it by creating

public RazorLightDependencyBuilder AddRazorLight(this IServiceCollection services, Action<RazorLightOptions> opts)
{
    _services.Configure<RazorLightOptions>(opts);
    return this;
}

Or perhaps we should be using named options instead: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-6.0#named-options-support-using-iconfigurenamedoptions

jzabroski avatar Nov 03 '22 23:11 jzabroski