[英]Encountering a CryptographicException when console app is run from Windows Task Scheduler
I'm deploying a console app which gathers data on a user base and sends emails to our support team. 我正在部署一个控制台应用程序,该应用程序可以在用户群上收集数据并将电子邮件发送给我们的支持团队。 There is a spreadsheet to read for which Google Spreadsheet Service is employed, therefore the corresponding class opens with the following lines:
有一个要使用Google Spreadsheet Service的电子表格,因此将打开相应的类,并带有以下几行:
const string serviceAccountEmail = "someemailaddresss";
var cert = new X509Certificate2(@"key.p12", "abigsecret", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail) {
Scopes = new[] { "https://spreadsheets.google.com/feeds" }
}.FromCertificate(cert));
I did not have any problems running it locally, nor were there issues when manually launching the executable on the AWS deployment machine. 我在本地运行它没有任何问题,在AWS部署计算机上手动启动可执行文件时也没有问题。 Now this task is to be automated and therefore was set up using Windows Task Scheduler, where it fails with the stack trace below.
现在,此任务将被自动化,因此是使用Windows Task Scheduler设置的,该任务失败并显示以下堆栈跟踪。
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Security.Cryptography.CryptographicException
at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32)
at System.Security.Cryptography.X509Certificates.X509Utils._QueryCertFileType(System.String)
at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromFile(System.String, System.Object, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags)
at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(System.String, System.String, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags)
at (hidden).GoogleSpreadsheetReader.ReadBrokerList(System.String)
at (hidden).Console.ReportEntryPoint..ctor()
at (hidden).Console.ReportEntryPoint.Main(System.String[])
I have found answers which pertain to similar errors within web applications where the answer is to properly load the user profile for the app pool within IIS to enable the crypto subsystem. 我发现了与Web应用程序中类似错误有关的答案,其中答案是正确加载IIS中应用程序池的用户配置文件以启用加密子系统。 This however is a console app.
但是,这是一个控制台应用程序。 Would could be the solution?
会是解决方案吗?
And here was the actual solution: While configuring the task in Task Scheduler, indicating the exe directory under "start in" in the Actions tab fixed the issue. 这是实际的解决方案:在“任务计划程序”中配置任务时,指示“操作”选项卡中“开始于”下的exe目录解决了该问题。 Even though "start in" is marked as an optional field and the path to the executable was obviously initially provided.
即使将“开始于”标记为可选字段,并且显然最初提供了可执行文件的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.