使用多个app.config文件

时间:2013-01-03 17:05:22

标签: c# .net spring.net

我有一个加载其他库(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.ServiceModel.dll中出现'System.InvalidOperationException'类型的第一次机会异常

Spring.Core.dll中出现'Spring.Objects.Factory.ObjectCreationException'类型的第一次机会异常

Spring.Core.dll中出现'Spring.Objects.Factory.ObjectCreationException'类型的第一次机会异常

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

1 个答案:

答案 0 :(得分:1)

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

您必须在主机控制台应用程序的配置中显式引用此配置文件,但这不重复。控制台应用程序host will look like this的app.config:

...
<spring>
  <context>
    <resource uri="file://services.xml"/>
    <resource uri="assembly://MyAssembly/MyDataAccess/data-access.xml"/>
  </context>
</spring>

请参阅spring.net docs how to configure a configuration in xml