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# 注射眼压<&燃气轮机;到静态类_C#_Asp.net Core_Dependency Injection_Appsettings - Fatal编程技术网

C# 注射眼压<&燃气轮机;到静态类

C# 注射眼压<&燃气轮机;到静态类,c#,asp.net-core,dependency-injection,appsettings,C#,Asp.net Core,Dependency Injection,Appsettings,我想让这个类保持静态。在不修改访问修饰符的情况下,是否有任何方法可以插入IOptions public static class Encrypter { private static readonly Encoding encoding = Encoding.UTF8; private static readonly EncryptionSettings _encryptionSettings; public static Encrypter(IOptions<

我想让这个类保持静态。在不修改访问修饰符的情况下,是否有任何方法可以插入IOptions

public static class Encrypter
{
    private static readonly Encoding encoding = Encoding.UTF8;

    private static readonly EncryptionSettings _encryptionSettings;

    public static Encrypter(IOptions<EncryptionSettings> encryptionSettings)
    {
        _encryptionSettings = encryptionSettings.Value;
    }

    public static string Encrypt(string plainText)
    {
        (...)
    }

    public static string Decrypt(string plainText)
    {
        (...)
    }

    static byte[] HmacSHA256(String data)
    {
        (...)
    }
}
公共静态类加密程序
{
私有静态只读编码编码=Encoding.UTF8;
私有静态只读加密设置\u加密设置;
公共静态加密机(IOptions encryptionSettings)
{
_encryptionSettings=encryptionSettings.Value;
}
公共静态字符串加密(字符串明文)
{
(...)
}
公共静态字符串解密(字符串明文)
{
(...)
}
静态字节[]HmacSHA256(字符串数据)
{
(...)
}
}
“Encrypter.Encrypter(IOptions)”: 静态构造函数上不允许使用访问修饰符

“Encrypter.Encrypter(IOptions)”: 静态构造函数必须是无参数的


不,这是不可能的。首先,静态构造函数不能有访问修饰符(
public
)或参数。第一次访问静态类时,CLR将调用静态构造函数。()


因为它是由CLR调用的,您永远无法调用它,所以您的DI框架也无法调用它。您应该使用单例并像任何其他实例类一样将其注册为服务。

它违反DI,但如果您的DI在使用
Encrypter
类之前已配置,则您可以使用对DI容器的静态引用:

public static class Encrypter
{
    private static readonly EncryptionSettings _encryptionSettings;

    static Encrypter()
    {
        if( IoC.Instance == null ) throw new InvalidOperationException( "IoC must be initialized before static members of Encrypter are used." ); 
        _encryptionSettings = IoC.Instance.GetService<IOptions<EncryptionSettings>>();
    }
}
公共静态类加密程序
{
私有静态只读加密设置\u加密设置;
静态加密程序()
{
如果(IoC.Instance==null)抛出新的InvalidOperationException(“必须在使用加密程序的静态成员之前初始化IoC”);
_encryptionSettings=IoC.Instance.GetService();
}
}

…但请不要这样做。

糟糕的设计选择使该类成为静态的

您正在经历与尝试将其用于依赖项注入相关的挑战。静电和去离子不能很好地混合,应尽量避免

将所需的功能封装在抽象背后

public interface IEncrypter {
    string Encrypt(string plainText);
    string Decrypt(string plainText);
}
并加以实施

public class Encrypter : IEncrypter {
    private static readonly Encoding encoding = Encoding.UTF8;
    private readonly EncryptionSettings _encryptionSettings;

    public Encrypter(IOptions<EncryptionSettings> encryptionSettings) {
        _encryptionSettings = encryptionSettings.Value;
    }

    public string Encrypt(string plainText) {
        //(...)
    }

    public string Decrypt(string plainText) {
        //(...)
    }

    static byte[] HmacSHA256(String data) {
        //(...)
    }
}

为什么它是一个静态类?你能在你的容器中使用一个单件生命周期吗?IOptions包含我在玩代码时忘记删除“public”访问修饰符的键,然后将代码粘贴到这里。
services.AddSingleton<IEncrypter, Encrypter>();