作为 Windows 服务运行时的 PCSC.InvalidContextException

2024-04-20

我一直在使用 pcsc-sharp 库开发一个小型智能卡扫描仪应用程序。该应用程序作为控制台应用程序运行时工作正常,代码如下:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Drawing.Printing;
using System.Net;
using System.Net.Sockets;
using System.Data.SqlClient;
using System.Threading;
using System.IO.Ports;
using System.Text.RegularExpressions;
using System.Speech.Synthesis;
using System.Diagnostics;
using PCSC;
using System.Media;

namespace MeterTestingApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            // Retrieve the names of all installed readers.
            string[] readerNames;
            using (var context = new SCardContext())
            {
                context.Establish(SCardScope.System);
                readerNames = context.GetReaders();
                context.Release();
            }

            if (readerNames == null || readerNames.Length == 0)
            {
                Console.WriteLine("There are currently no readers installed.");
                return;
            }

            // Create a monitor object with its own PC/SC context. 
            // The context will be released after monitor.Dispose()
            using (var monitor = new SCardMonitor(new SCardContext(), SCardScope.System))
            {
                // Point the callback function(s) to the anonymous & static defined methods below.
                monitor.CardInserted += (sender, args0) => DisplayEvent("CardInserted", args0);
                //monitor.CardRemoved += (sender, args0) => DisplayEvent("CardRemoved", args0);
                //monitor.Initialized += (sender, args0) => DisplayEvent("Initialized", args0);
                //monitor.StatusChanged += StatusChanged;
                monitor.MonitorException += MonitorException;

                monitor.Start(readerNames);

                // Keep the window open
                Console.ReadLine();
            }
        }

        private static void DisplayEvent(string eventName, CardStatusEventArgs unknown)
        {
            Console.WriteLine(">> {0} Event for reader: {1}", eventName, unknown.ReaderName);
            Console.WriteLine("ATR: {0}", BitConverter.ToString(unknown.Atr ?? new byte[0]));
            Console.WriteLine("State: {0}\n", unknown.State);


            //Works fine here
        }

        private static void StatusChanged(object sender, StatusChangeEventArgs args)
        {
            //Console.WriteLine(">> StatusChanged Event for reader: {0}", args.ReaderName);
            //Console.WriteLine("ATR: {0}", BitConverter.ToString(args.Atr ?? new byte[0]));
            //Console.WriteLine("Last state: {0}\nNew state: {1}\n", args.LastState, args.NewState);
        }

        private static void MonitorException(object sender, PCSCException ex)
        {
            Console.WriteLine("Monitor exited due an error:");
            Console.WriteLine(SCardHelper.StringifyError(ex.SCardError));
        }
    }
}

但是,当尝试将其作为 Windows 服务运行时(以允许扫描卡以更新数据库),该事件永远不会触发,并且在服务启动时似乎会崩溃。事件日志显示 PCSC.InvalidContextException 错误。该服务的代码几乎相同,但这里是示例。

    using System;
using System.Diagnostics;
using System.ServiceProcess;
using System.Timers;
using System.Configuration;
using System.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Drawing.Printing;
using System.Threading;
using System.Speech.Synthesis;
using System.IO.Ports;
using System.Text.RegularExpressions;
using PCSC;
using System.Media;
using log4net;


namespace CardScannerService
{
    public partial class CardScanner : ServiceBase
    {
        private ILog logger;

        private SCardContext context;
        private string[] readerNames;
        private SCardMonitor monitor;

        private static System.Timers.Timer aTimer;

        public CardScanner()
        {
            InitializeComponent();
        }

        static void Main()
        {
            ServiceBase.Run(new CardScanner());
        }


        //
        // Start Service
        //

        protected override void OnStart(string[] args)
        {
            aTimer = new System.Timers.Timer();
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = (1000 * 60 * 60 * 24); // Once a day; 1000ms * 60s * 60m * 24h
            aTimer.Enabled = true;

            //// Entry point
            using (context = new SCardContext())
            {
                context.Establish(SCardScope.System);
                readerNames = context.GetReaders();
                context.Release();
            }

            if (readerNames == null || readerNames.Length == 0)
            {
                EventLog.WriteEntry("CardReaderService", "There are currently no readers installed.");
                return;
            }

            // Create a monitor object with its own PC/SC context. 
            // The context will be released after monitor.Dispose()
            using (monitor = new SCardMonitor(new SCardContext(), SCardScope.System))
            {
                // Point the callback function(s) to the anonymous & static defined methods below.
                monitor.CardInserted += (sender, args0) => DisplayEvent("CardInserted", args0);
                //monitor.CardRemoved += (sender, args0) => DisplayEvent("CardRemoved", args0);
                //monitor.Initialized += (sender, args0) => DisplayEvent("Initialized", args0);
                //monitor.StatusChanged += StatusChanged;
                monitor.MonitorException += MonitorException;

                monitor.Start(readerNames);

                // Keep the window open
                //Console.ReadLine();
            }

            logger.InfoFormat("CardScannerService Started at {0}", DateTime.Now.ToLongTimeString());
        }


        //
        // Stop Service
        //

        protected override void OnStop()
        {
            logger.InfoFormat("CardScannerService Stopped at {0}", DateTime.Now.ToLongTimeString());
        }


        //
        // Execute timed event every hour and half hour
        //

        void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            // Card scanner service entry point for timed event - any cleanup code can go here

        }

        private static void DisplayEvent(string eventName, CardStatusEventArgs unknown)
        {
            EventLog.WriteEntry("CardReaderService", ">> " + eventName + "v Event for Reader: " + unknown.ReaderName);
            EventLog.WriteEntry("CardReaderService", "ATR: " + BitConverter.ToString(unknown.Atr ?? new byte[0]));
            EventLog.WriteEntry("CardReaderService", "State: " + unknown.State);


            //Not firing
        }

        private static void StatusChanged(object sender, StatusChangeEventArgs args)
        {
            //Console.WriteLine(">> StatusChanged Event for reader: {0}", args.ReaderName);
            //Console.WriteLine("ATR: {0}", BitConverter.ToString(args.Atr ?? new byte[0]));
            //Console.WriteLine("Last state: {0}\nNew state: {1}\n", args.LastState, args.NewState);
        }

        private static void MonitorException(object sender, PCSCException ex)
        {
            EventLog.WriteEntry("CardReaderService", "Monitor exited due an error:");
            EventLog.WriteEntry("CardReaderService", SCardHelper.StringifyError(ex.SCardError));
        }
    }
}

我已经删除了实际的数据库逻辑,因为它看起来工作正常,我相信在尝试释放上下文变量或将监视器连接到智能卡时它会崩溃。

还必须指出的是,我已尝试将服务从开始使用本地系统帐户更改为使用本地服务,以防出现某种访问权限错误。

如果有人能指出我出错的地方,我将非常感谢您的帮助。我对使用 PC/SC 还比较陌生,目前这个项目似乎遇到了障碍。

UPDATE

我现在已经解决了这个问题;事实证明 Windows 服务不喜欢使用语句。变量被放置在块的末尾,因此上下文变得无效。

using 块被注释掉,并且变量的声明是在它们的位置完成的。 OnStart方法的新代码如下:

protected override void OnStart(string[] args)
        {
            logger = LogManager.GetLogger(this.GetType().Name);
            aTimer = new System.Timers.Timer();
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = (1000 * 60 * 60 * 24); // Once a day; 1000ms * 60s * 60m * 24h
            aTimer.Enabled = true;

            // Entry point

            //using (context = new SCardContext())
            //{
                context = new SCardContext();
                context.Establish(SCardScope.System);
                readerNames = context.GetReaders();
                context.Release();
            //}

            if (readerNames == null || readerNames.Length == 0)
            {
                EventLog.WriteEntry("CardReaderService", "There are currently no readers installed.");
                return;
            }

            // Create a monitor object with its own PC/SC context. 
            // The context will be released after monitor.Dispose()
            //using (monitor = new SCardMonitor(new SCardContext(), SCardScope.System))
            //{
                monitor = new SCardMonitor(new SCardContext(), SCardScope.System);
                // Point the callback function(s) to the anonymous & static defined methods below.
                monitor.CardInserted += (sender, args0) => DisplayEvent("CardInserted", args0);
                //monitor.CardRemoved += (sender, args0) => DisplayEvent("CardRemoved", args0);
                //monitor.Initialized += (sender, args0) => DisplayEvent("Initialized", args0);
                //monitor.StatusChanged += StatusChanged;
                monitor.MonitorException += MonitorException;

                monitor.Start(readerNames);

                // Keep the window open
                //Console.ReadLine();
            //}

            logger.InfoFormat("CardScannerService Started at {0}", DateTime.Now.ToLongTimeString());
        }

希望这些信息可以帮助其他人。


我设法解决了问题 - 问题已更新以反映更改。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

作为 Windows 服务运行时的 PCSC.InvalidContextException 的相关文章

  • Microsoft Visual C++ 2008 和 R2007b 的 Mex 类型

    我想对 vs2008 和 matlab2007b 使用 mex 类型 我尝试了下面的代码 include
  • 当用户与 DateTimePicker 控件交互时会引发什么事件?

    我是 C 新手 在我的程序中使用 DateTimePicker Value Changed 事件 但我发现当用户单击箭头时发生 ValueChanged 事件 或者如果也以编程方式更改值 我只想识别 DateTimePicker 的用户交互
  • 如何从 OnChange 事件捕获文本框的值

    在我的 C MVC 应用程序中 我有一系列这样生成的文本框 foreach object item in items Html TextBox 渲染的结果是一系列看起来像这样的文本框
  • 这个洗牌算法有什么问题吗?

    我一直在做一些休闲假期计算 我的迷你项目是模拟意大利游戏 tomboli 一个关键的组成部分是对以下过程的模拟 游戏由一名男子控制 他拿着一袋 90 个弹珠 编号为 1 到 90 他从袋中随机取出一颗弹珠 每次向玩家喊出弹珠编号 经过一番思
  • 可空引用类型意外 CS8629 可空值类型对于临时变量可能为空

    在 C 8 项目中 我使用可为 null 的引用类型 并收到意外的 或者至少对我来说意外的 CS8629 警告 bool singleContent x DataInt null bool multiContent x DataNvarch
  • 如何从 UNC 中提取服务器名称

    谁能告诉我如何从 UNC 中提取服务器名称 ex 服务器名称 目录 目录 编辑 我很抱歉 但看起来我需要澄清一个错误 路径实际上更像是 服务器名 d 目录 我知道这可能会改变一些事情 怎么样Uri Uri uri new Uri serve
  • 如何找到 QDockWidget 标题栏的高度?

    我正在尝试找到 a 的高度QDockWidget标题栏 以便对自定义布局进行一些智能调整大小 但标题栏不是单独的小部件 它内置于停靠小部件的私有布局中 并且没有成员可以访问它 还有其他方法可以找到它的高度吗 是的 您可以使用以下命令找到标题
  • 自定义文件属性

    我需要遵循 在我的申请中 我有文件 需要随时签入和签出的文件 当我从应用程序中签出文档时 我需要将自定义属性添加到文件中 以便稍后在签入文档时可以识别它 我尝试使用以下代码使用 DSOFile 中的 OleDocumentPropertie
  • 在javascript中调用c#函数[重复]

    这个问题在这里已经有答案了 可能的重复 从 Javascript 调用 ASP NET 函数 https stackoverflow com questions 3713 call asp net function from javascr
  • 如何将焦点设置到 Windows 窗体应用程序中的控件?

    在 Windows 窗体应用程序中 when我是否编写代码以在应用程序启动时以及随后调用函数后将焦点设置到控件 例如 如果我有一个 DropDownList 一个 TextBox 和四个按钮 并且我希望将 Focus 设置为 DropDow
  • 如何使用实体框架更新特定记录的一个字段?

    我想要更新一个名叫 Pejman 的人的家庭情况 这是我的对象类 public class Person public int Id get set public string FirstName get set public string
  • 接口作为类型约束和接口作为参数之间的区别?

    如果我想创建一个采用实例的方法IList作为参数 或任何其他接口 但让我们使用IList作为一个例子 我可以创建一个带有类型约束的通用方法 例如 public static void Foo1
  • python:Windows终端中的unicode,使用的编码?

    我在 Windows 7 终端中使用 Python 解释器 我正在尝试了解 unicode 和编码 I type gt gt gt s gt gt gt s x89 gt gt gt u u gt gt gt u u xeb 问题1 字符串
  • C/C++ 特殊 CPU 功能的使用

    我很好奇 新的编译器是否使用了新 CPU 中内置的一些额外功能 例如 MMX SSE 3DNow 所以 我的意思是 在最初的 8086 中甚至没有 FPU 所以旧的编译器甚至不能使用它 但新的编译器可以 因为 FPU 是每个新 CPU 的一
  • 在 Qt C++ 中使用多个键

    我正在构建 坦克 游戏 我使用关键事件在地图上运行我的坦克 实际上我当时只能使用一把钥匙 但我需要有能力去完成任务 同时向上和离开 这是我的单键事件代码 switch event gt key case Qt Key Up if ui gt
  • 奇怪的 MSC 8.0 错误:“ESP 的值未在函数调用中正确保存...”

    我们最近尝试将一些 Visual Studio 项目分解为库 并且在测试项目中一切似乎都编译和构建得很好 其中一个库项目作为依赖项 然而 尝试运行该应用程序给我们带来了以下令人讨厌的运行时错误消息 运行时检查失败 0 ESP 的值未在函数调
  • C++ 中的 Ofstream 数组

    我想要在我的项目中使用 41 个输出文件来在其上写入文本 首先创建一个字符串数组list为了命名这些输出文件 然后我尝试定义一个 ofstream 对象数组并使用list命名它们 但我收到此错误 outfile cannot be used
  • 如何检查多个变量是否等于同一值?

    如何比较多个项目 例如 我希望检查所有变量 A B 和 C 是否都等于字符 X 或所有三个变量都等于 O 如果其中 2 个为 X 1 个为 O 则应返回 false I tried if A B C X A B C O Do whateve
  • 如果 foreach 是一个结构数组,它会复制每个元素吗?

    我有一个结构数组 做foreach运算符在迭代数组时复制每个元素 据我所理解foreach只是底层的语法糖转换为for 所以看来答案是否定的 但我很想得到一些确认 PS 看来应该有人已经问过了 但我无法轻易找到任何东西 因此 请以提供的参考
  • C# 泛型中的通配符等效项

    假设我有一个通用类 如下所示 public class GeneralPropertyMap

随机推荐