使用多个app.config文件

本文关键字:config 文件 app | 更新日期: 2023-09-27 17:50:29

我有一个加载其他库(DLL)的控制台应用程序。我使用的是Spring.NET。我的控制台应用程序非常简单,加载通过DI配置的app.config来初始化所选的库。

代码
ContextRegistry.GetContext();
配置(app.config)

<?xml version="1.0"?>
<configuration>
    <configSections>
        <sectionGroup name="spring">
            <section name="context" 
               type="Spring.Context.Support.ContextHandler, Spring.Core"/>
            <section name="objects" 
               type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
    </sectionGroup>
</configSections>
<spring>
    <context>
        <resource uri="config://spring/objects"/>
        <resource uri="assembly://MyDLL/MyDLL.Config/Services.xml"/>
    </context>
    <objects xmlns="http://www.springframework.net" 
             xmlns:aop="http://www.springframework.net/aop">
    </objects>
</spring>
<startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup></configuration>

每个库都有自己的"services.xml"文件。这些库也使用Spring.NET。

<objects xmlns="http://www.springframework.net" 
         xmlns:aop="http://www.springframework.net/aop">
<object id="componentObj" 
        type="MyDLL.Services.ComponentService, MyDLL" 
        singleton="true" />
<object 
 id="componentServiceHost" 
 type="Spring.ServiceModel.Activation.ServiceHostFactoryObject, Spring.Services">
    <property name="TargetName" 
              value="componentObj" />
</object>
</objects>

得到一个异常"进程因StackOverflowException而终止。"如果我注释掉<object id="componentServiceHost",则不会出现异常。

调试中的VS控制台显示

类型为'System '的第一次异常。InvalidOperationException'在System.ServiceModel.dll中发生

类型'Spring.Objects.Factory '的第一次机会异常。ObjectCreationException' occurred in Spring.Core.dll

类型'Spring.Objects.Factory '的第一次机会异常。ObjectCreationException' occurred in Spring.Core.dll

Managed (v4.0.30319)'已退出,代码为-2147023895 (0x800703e9).

使用多个app.config文件

您还可以将spring配置放在纯xml文件中,并将其包含在库中。这样,您就可以轻松地共享此配置,而无需共享app.config。

您必须在主机控制台应用程序的配置中显式引用此配置文件,但这没有重复。控制台应用程序主机的app.config看起来像这样:

...
<spring>
  <context>
    <resource uri="file://services.xml"/>
    <resource uri="assembly://MyAssembly/MyDataAccess/data-access.xml"/>
  </context>
</spring>
查看spring.net文档如何在xml中配置配置。