简体   繁体   English

使用多个app.config文件

[英]using multiple app.config files

I have a console application that loads other libraries (DLL's). 我有一个加载其他库(DLL)的控制台应用程序。 I'm using Spring.NET. 我正在使用Spring.NET。 My console application is very simple, load the app.config that is configured through DI to initialize the chosen library. 我的控制台应用程序非常简单,请加载通过DI配置的app.config来初始化所选的库。

Code

ContextRegistry.GetContext();

Configuration (app.config) 配置(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>

Each library has its own "services.xml" file. 每个库都有其自己的“ services.xml”文件。 These libraries also use Spring.NET. 这些库也使用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>

Getting a excpetion "Process is terminated due to StackOverflowException." 得到一个例外“进程由于StackOverflowException而终止。” If I comment out the <object id="componentServiceHost" , the exception does not occur. 如果我注释掉<object id="componentServiceHost" ,则不会发生异常。

The VS console in debug shows 调试中的VS控制台显示

A first chance exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.dll 类型“ System.InvalidOperationException”的第一次机会异常发生在System.ServiceModel.dll中

A first chance exception of type 'Spring.Objects.Factory.ObjectCreationException' occurred in Spring.Core.dll 类型为“ Spring.Objects.Factory.ObjectCreationException”的第一次机会异常发生在Spring.Core.dll中

A first chance exception of type 'Spring.Objects.Factory.ObjectCreationException' occurred in Spring.Core.dll 类型为“ Spring.Objects.Factory.ObjectCreationException”的第一次机会异常发生在Spring.Core.dll中

Managed (v4.0.30319)' has exited with code -2147023895 (0x800703e9). 托管(v4.0.30319)已退出,代码-2147023895(0x800703e9)。

You can also place spring configuration in plain xml files and include those with your library. 您还可以将spring配置放在纯xml文件中,并将其包含在库中。 This way, you can easily share this configuration without the need of sharing an app.config. 这样,您可以轻松共享此配置,而无需共享app.config。

You do have to explicitly reference this configuration file in the configuration of your host console application, but that's no duplication. 您必须在主机控制台应用程序的配置中明确引用此配置文件,但这不是重复的。 The app.config of your console application host will look like this : 控制台应用程序主机的app.config 将如下所示

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

See the spring.net docs how to configure a configuration in xml . 参见spring.net docs 如何在xml中配置配置

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

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