【发布时间】:2009-12-03 20:16:41
【问题描述】:
我们在 .NET 3.5 应用程序中使用 CSLA(一个相当古老的版本),我们利用它的 NetRun 应用程序加载为我们的一些用户。对于那些不熟悉 NetRun 的人来说,NetRun.exe 基本上是一个安装在用户计算机上的应用程序“运行程序”(例如,安装到 c:\Program Files\NetRun\NetRun.exe)。用户只需启动 NetRun.exe 即可启动应用程序。
NetRun.exe 的作用如下:
(1) 创建一个新的 AppDomainSetup
Dim setupDomain As New AppDomainSetup()
setupDomain.ApplicationBase = CurrentDomainPath() ' this will be C:\Program Files\NetRun\
setupDomain.ConfigurationFile = "http://www.ourdomain.com/TheApp.xml" ' The app.config file is actually named TheApp.xml on the server because it has NetRun-specific config settings that don't belong in the standard TheApp.config that is used when the app is running directly from the server.
(2) 然后使用该 AppDomainSetup 创建一个新的 AppDomain
' create new application domain
Dim newDomain As AppDomain = AppDomain.CreateDomain("TheApp", Nothing, setupDomain)
(3) 然后 NetRun.Launcher(一个启动助手类 -- 主要用于常见的启动屏幕)在新的 AppDomain 中通过以下方式实例化:
' create launcher object in new appdomain
Dim launcher As Launcher = CType(newDomain.CreateInstanceAndUnwrap("NetRun", "NetRun.Launcher"), Launcher)
(4) 然后午餐助手类在新的 AppDomain 中运行应用程序
' use launcher object from the new domain to launch the remote app in that appdomain
launcher.RunApp()
(5) 在所有的启动画面之后,应用程序最终通过以下方式启动
Dim ass = Assembly.LoadFrom("http://www.ourdomain.com/TheApp.exe")
ass.EntryPoint.Invoke(ass.EntryPoint, Nothing)
因此,回顾一下,实际运行的应用程序的 AppDomain 的 ApplicationBase 是 C:\Program Files\NetRun\,而实际应用程序的入口点位于“http://www.ourdomain.com/TheApp.exe”中。到目前为止,一切顺利。
应用程序本身,除了 TheApp.exe 之外,还依赖于 ALibrary.dll。迄今为止,这对我们来说非常有效。即使 ALibrary.dll 具有被拉入 TheApp.xml 文件的配置条目,这些配置条目也始终可以正常读取(并继续这样做)。
在即将发布的版本中,我们添加了一个新的自定义配置文件部分,该部分在 ALibrary.dll 中定义。我们将新部分添加到 TheApp.xml 和 TheApp.config 文件中
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.Applicat...">
<section name="TheApp.My.MySettings" type="System.Configuration.ClientSettingsS..."/>
<section name="ALibrary.My.MySettings" type="System.Configuration.ClientSettingsS..."/>
</sectionGroup>
<!-- NEW CONFIG SECTION -->
<section name="fixedPriceExceptionModel" type="ALibrary.Configuration.FixedPrices.FixedPriceExceptionModelSection, ALibrary"/>
</configSections>
(为了节省篇幅,本文删除了部分内容)
但是,当我们尝试通过“正常”方式访问 NetRun 中启动的 TheApp.exe 中的新部分时
CType(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) _
.GetSection("fixedPriceExceptionModel"), FixedPriceExceptionModelSection)
我们得到以下异常:
创建时出错 配置节处理程序 固定价格异常模型:不能 加载文件或程序集 'ALibrary' 或 它的依赖项之一。系统 找不到指定的文件。 (http://www.ourdomain.com/TheApp.xml 第 8 行)
做一些研究,这是因为 .NET 配置库的程序集解析器在所有常见的地方寻找 ALibrary.dll; ApplicationBase、特定的子目录、.NET 核心目录,然后是 GAC。不幸的是,在这些位置中永远找不到 ALibrary.dll。当此应用程序直接在服务器上运行时,不使用 NetRun,应用程序不会抛出异常并正确读取配置部分。
我尝试过以不同方式设置 AppDomainSetup,因为它的 ApplicationBase 将设置为 http://www.ourdomain.com/,但随后调用 CType(newDomain.CreateInstanceAndUnwrap("NetRun", "NetRun.Launcher"), Launcher ) 炸弹,因为 NetRun.exe 不在服务器上。
这可能涉及很多内容,我希望我描述得足够好(并且没有让您感到厌烦)。也许没有简单的解决方案,但我已经用尽了我对该领域的有限知识,并希望 SO 上的其他人可能有神奇的修复,让我能够在 NetRun 和本地运行应用程序时可靠地访问自定义配置部分。
【问题讨论】:
标签: .net configuration appdomain