Unload child AppDomain that contains POS.net hardware causing CannotUnloadAppDomainException RRS feed

  • Question

  • I am using .net 4.0, with POS.net 1.12 and I create a hardware class in a new child AppDomain so that any unhandled exceptions do not kill my parent AppDomain.

    I can create the child AppDomain and make calls to it no problem. However, if I try to unload the AppDomain I get the exception "CannotUnloadAppDomainException".

    I have googled the issue and the exception normally occurs when threads cannot be killed. I do not actually create any new threads in the child class.

    I managed to pinpoint the piece of code that causes this error. If I create the POS hardware class and it only creates POS objects then it works fine. If, however, I call method "Open()" on any piece of hardware, then this exception occurs when unloading. Now before I attempt the unload, I close down all hardware and I have made sure the clean up code gets hit, so I am unsure what the issue is.

    Here is the code to create and unload the AppDomain:

    AppDomain hardwareDomain = AppDomain.CreateDomain("Hardware domain"); IHardwareManager hardwareManager = (IHardwareManager)hardwareDomain.CreateInstanceFromAndUnwrap(typeof(OposHardwareManager).Assembly.Location, typeof(OposHardwareManager).FullName); hardwareManager.StartupHardware(); hardwareManager.CloseDownHardware(); hardwareManager = null; // **** causes exception AppDomain.Unload(hardwareDomain);

    And here is the hardware class:

    public class OposHardwareManager : MarshalByRefObject, IHardwareManager
        private PosExplorer _posExplorer;
        private PosPrinter _printer;
        public void StartupHardware()
            // create the hardware explorer
            this._posExplorer = new PosExplorer();
            // create and enable the printer
            DeviceInfo printerInfo = this._posExplorer.GetDevice(DeviceType.PosPrinter);
            PosDevice printerDevice = this._posExplorer.CreateInstance(printerInfo);
            this._printer = (PosPrinter)printerDevice;
            // ***** this line here, if run, causes the exception on unload 
            this._printer.DeviceEnabled = true;
        public void CloseDownHardware()
            this._printer = null;
            this._posExplorer = null;

    Any ideas?


    Wednesday, March 20, 2013 12:56 PM

All replies

  • Check maybe there are some details in Message and InnerException fields of intercepted exception.

    Wednesday, March 20, 2013 8:44 PM