Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NET Core 5.0-IServiceCollection.Configure和ILogger_C#_Asp.net Core_Dependency Injection_.net 5 - Fatal编程技术网

C# .NET Core 5.0-IServiceCollection.Configure和ILogger

C# .NET Core 5.0-IServiceCollection.Configure和ILogger,c#,asp.net-core,dependency-injection,.net-5,C#,Asp.net Core,Dependency Injection,.net 5,我有一个注册其服务的图书馆: public static IServiceCollection AddViewStringRenderer(this IServiceCollection services, string contentRootPath, ILogger logger) { services.Configure<RazorViewEngineOptions>(options => { options.ViewLocationExpanders.Add(n

我有一个注册其服务的图书馆:

public static IServiceCollection AddViewStringRenderer(this IServiceCollection services, string contentRootPath, ILogger logger)
{
    services.Configure<RazorViewEngineOptions>(options => { options.ViewLocationExpanders.Add(new ViewLocationExpander(contentRootPath, logger)); });
    services.AddTransient<IRazorViewToStringRenderer, RazorViewToStringRenderer>();
    ...
    return services;
}
公共静态IServiceCollection AddViewStringRenderer(此IServiceCollection服务、字符串内容根路径、ILogger记录器)
{
配置(选项=>{options.ViewLocationExpanders.Add(新的ViewLocationExpander(contentRootPath,logger));});
services.AddTransient();
...
返回服务;
}
而且,正如您可能看到的,我需要一个用于
服务的记录器。配置(…)
,因此它是此扩展方法的参数


但是由于该方法用于
Startup.ConfigureServices()
中,因此我们无法在.NET 5中实例化
ILogger
。如何才能将记录器传递给
服务。Configure()

好的,非常感谢David Fowler指出此模式-

结果如下:

public class RazorViewEngineConfigureOptions : IConfigureOptions<RazorViewEngineOptions>
{
    private readonly ILogger<ViewLocationExpander> _logger;
    private readonly string _contentRootPath;

    public RazorViewEngineConfigureOptions(IHostEnvironment env, ILogger<ViewLocationExpander> logger)
    {
        _contentRootPath = env.ContentRootPath;
        _logger = logger;
    }

    public void Configure(RazorViewEngineOptions options)
    {
       options.ViewLocationExpanders.Add(new ViewLocationExpander(_contentRootPath, _logger));
    }
}

public static class RegisterServices
{
    public static IServiceCollection AddEmailerService(this IServiceCollection services)
    {
        services.AddSingleton<IConfigureOptions<RazorViewEngineOptions>, RazorViewEngineConfigureOptions>();
        services.AddTransient<IRazorViewToStringRenderer, RazorViewToStringRenderer>();

        ...
        return services;
    }
}
公共类RazorViewEngineConfigureOptions:IConfigureOptions
{
专用只读ILogger\u记录器;
私有只读字符串\u contentRootPath;
公共RazorViewEngineConfigurationOptions(IHostEnvironment环境、ILogger记录器)
{
_contentRootPath=env.contentRootPath;
_记录器=记录器;
}
公共void配置(RazorViewEngineOptions选项)
{
options.ViewLocationExpanders.Add(新的ViewLocationExpander(_contentRootPath,_logger));
}
}
公共静态类注册服务
{
公共静态IServiceCollection AddEmailerService(此IServiceCollection服务)
{
services.AddSingleton();
services.AddTransient();
...
返回服务;
}
}