相关疑难解决方法(0)

依赖注入,注入参数

我正在使用DI的vNext实现.如何将参数传递给构造函数?例如,我有课:

public class RedisCacheProvider : ICacheProvider
{
    private readonly string _connectionString;

    public RedisCacheProvider(string connectionString)
    {
        _connectionString = connectionString;
    }
    //interface methods implementation...
}
Run Code Online (Sandbox Code Playgroud)

和服务注册:

services.AddSingleton<ICacheProvider, RedisCacheProvider>();
Run Code Online (Sandbox Code Playgroud)

如何将参数传递给RedisCacheProvider类的构造函数?例如Autofac:

builder.RegisterType<RedisCacheProvider>()
       .As<ICacheProvider>()
       .WithParameter("connectionString", "myPrettyLocalhost:6379");
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection asp.net-core-mvc asp.net-core

31
推荐指数
4
解决办法
3万
查看次数