C# “Class”的类型初始值设定项引发异常
我正在编写一个控制台应用程序,作为对传入的数字进行加密/解密的一个小工具。以下代码引发此错误:C# “Class”的类型初始值设定项引发异常,c#,encryption,console-application,nullreferenceexception,C#,Encryption,Console Application,Nullreferenceexception,我正在编写一个控制台应用程序,作为对传入的数字进行加密/解密的一个小工具。以下代码引发此错误: private static readonly string _encryptedKey = ConfigurationManager.AppSettings["AffinityEncryptedKey"]; private static readonly string _encryptedInitializationVector = ConfigurationManager.A
private static readonly string _encryptedKey =
ConfigurationManager.AppSettings["AffinityEncryptedKey"];
private static readonly string _encryptedInitializationVector =
ConfigurationManager.AppSettings["AffinityEncryptedInitializationVector"];
private static readonly byte[] _key = Convert.FromBase64String(
ServiceLocator.Current
.GetInstance<IEncryptionService>()
.Decrypt(_encryptedKey));
private static readonly byte[] _initializationVector = Convert.FromBase64String(
ServiceLocator.Current
.GetInstance<IEncryptionService>()
.Decrypt(_encryptedInitializationVector));
static void Main(string[] args)
{ ... }
这一行:private static readonly byte[]\u key=Convert.FromBase64StringServiceLocator.Current.GetInstance.decryptedKey引发了错误,我确信它后面的那个也会爆炸
我正在从app.config文件中检索的值存在,并且已成功检索。我在项目中引用了iEntryOptionService和ServiceLocator的DLL,并通过using语句将其包括在内
我在调试时检查了InnerException,它显示:对象引用未设置为对象的实例,源是Microsoft.Practices.ServiceLocation。问题是ServiceLocator的当前属性为null
我不知道如何解决这个问题。我从另一个项目的其他地方一字不差地复制了代码。有人知道当前属性为null的原因吗?我可以做些什么来纠正这个问题?谢谢您的帮助。+1了解一个合理的NRE问题。ServiceLocator需要初始化吗?采用了完全不同的方法,因为我发现使用ServiceLocator进行我的工作会非常麻烦。必须在定位器中注册,而我的项目中没有设置定位器。然后,将其配置为使用DI容器,然后将该容器配置为具有iEntryOptionService。在本例中,我只是直接实例化了EncryptionService。谢谢你的回复。