简体   繁体   English

.NET代理/加载器应用程序体系结构

[英].NET proxy/loader application architecture

I want to launch a WPF App, in a 2nd AppDomain, from a 'loader' class. 我想从“加载程序”类的第二个AppDomain中启动WPF应用程序。 If the WPF App times itself out, I want it to fire an event back to the loader class and the loader class will Unload() the the 2nd AppDomain and show a login screen. 如果WPF应用程序自身超时,我希望它将一个事件触发回loader类,并且loader类将Unload()第二个AppDomain并显示登录屏幕。 If the user logs back in, the same process will repeat. 如果用户重新登录,将重复相同的过程。

I have this working to a degree by : 我通过以下方式在某种程度上进行了工作:

  1. Loader class creates the 2nd AppDomain and class B in that domain via CreateInstanceAndUnwrap. 加载程序类通过CreateInstanceAndUnwrap在该域中创建第二个AppDomain和类B。

  2. Loader class creates a MarshalByRefObject that has a Timeout event, and passes it to a B.StartUp(MBRO), which passes the MBRO on to the constructor of the WPF App(). Loader类创建一个具有Timeout事件的MarshalByRefObject,并将其传递给B.StartUp(MBRO),后者将MBRO传递给WPF App()的构造函数。 Loader class adds a handler to MBRO.Timeout. 加载程序类将处理程序添加到MBRO.Timeout。

  3. WPF App times out, calls MBRO.Timeout, which is handled by the Loader class. WPF应用程序超时,调用MBRO.Timeout,该事件由Loader类处理。 In the event handler, Loader class shuts down B WPF App and shows the login window. 在事件处理程序中,Loader类关闭B WPF App并显示登录窗口。

The problem is that I can't Unload the 2nd AppDomain in step 3. When I do it, it shuts down the host appdomain as well (no exceptions or anything, the whole thing shuts down). 问题是我无法在步骤3中卸载第二个AppDomain。执行此操作时,它也会同时关闭主机appdomain(没有异常或任何事情,整个事情都会关闭)。

I think the problem happens because the event handler delegate is being fired by the WPF App which is in the 2nd domain, and therefore I am trying to pull the rug from under App Domain from within a delegate it has fired. 我认为发生问题是因为事件处理程序委托由第二域中的WPF App触发,因此我试图从触发的委托中将其从App Domain下拉下。

Is this correct? 这个对吗? Does it work like this across Domains? 它在整个域中都这样工作吗?

In summary, can anyone suggest a way where you can launch a 2nd AppDomain, receive an event from the 2nd AppDomain and Unload() the 2nd AppDomain upon receiving that event? 总而言之,有人可以提出一种方法来启动第二个AppDomain,从第二个AppDomain接收事件并在接收到该事件后对第二个AppDomain进行Unload()吗? I think I need someone to decouple the receipt of the event from the act of Unloading the app domain. 我认为我需要有人将事件的接收与卸载应用程序域的行为脱钩。

Yes, this is correct. 是的,这是正确的。 The problem is that the stack trace passes from the primary domain through the second domain and back to the primary domain. 问题是堆栈跟踪从主域穿过第二个域,然后又回到主域。

You have a few choices here. 您在这里有几种选择。

  1. You call into the app domain. 您调用应用程序域。 If you could make it so that the method you call returns with a specific status code (eg Success or Timeout ), you don't have the stack problem anymore; 如果可以使调用的方法返回特定的状态代码(例如SuccessTimeout ),则不再有堆栈问题;

  2. You call the app domain from a second thread: 您从第二个线程调用应用程序域:

    1. From the first thread, create a new thread which creates the app domain and starts the application; 在第一个线程中,创建一个新线程,该新线程创建应用程序域并启动应用程序;

    2. Still in the first thread, create a AutoResetEvent and do a WaitOne() on that; 仍然在第一个线程中,创建一个AutoResetEvent并在其上执行一个WaitOne()

    3. When the event is executed from the app domain, it will not execute on the first thread. 从应用程序域执行事件时,它将不会在第一个线程上执行。 I'm not sure whether the event will execute on the second event and if not, creating the secondary thread will not be necessary. 我不确定该事件是否将在第二个事件上执行,如果不是,则不需要创建辅助线程。 From the thread the comes in on, set a 'Timeout' flag somewhere and do a Set() on the event; 从线程进入,在某个地方设置“ Timeout”标志,并在事件上执行Set()

    4. The primary thread wakes up and knows what to do depending on the status of the flag. 主线程唤醒并根据标志的状态知道该怎么做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM