在Web和控制台应用之间共享配置文件

时间:2014-07-09 15:43:50

标签: c# asp.net .net iis configuration

我有一个Core类lib,里面有我所有的逻辑。

我有一个使用IIS的Web应用程序。

我有一个使用Http Listener的控制台应用程序。

两个主机项目都引用了包含connectionStringSource.config文件的Core lib。

主机项目已将“添加为链接”添加到Core中的connectionStringSource.config并设置为“复制到输出”。在IIS主机项目中的web.config中有

<connectionStrings configSource="bin\connectionStringSource.config"></connectionStrings>

但是我得到了:

  

[ArgumentException:路径中的非法字符。]
  [ConfigurationErrorsException:configSource属性无效:路径中的字符非法。 (第13行)]

如果我改为

<connectionStrings configSource="bin/connectionStringSource.config"></connectionStrings>

它说

  

configSource属性必须是相对物理路径,因此&#39; /&#39;不允许使用角色。

有没有办法分享这些设置?

我已按照herehere的建议进行操作,但它们似乎无法正常工作。

2 个答案:

答案 0 :(得分:7)

connections.config 文件添加到 SolutionItems 解决方案文件夹

enter image description here

在我的情况下它包含

<connectionStrings>
  <add name="MyContext"
       connectionString="Data Source=Server\NamedInstance;Initial Catalog=MyDB;Integrated Security=True;MultipleActiveResultSets=True"
    providerName="System.Data.SqlClient" />
</connectionStrings>

单击任何项​​目并编辑app.config或web.config

<connectionStrings configSource="connections.config"></connectionStrings>

现在点击项目并选择添加现有项,然后点击添加为链接,将 connections.config 添加为链接。< / p>

enter image description here

现在右键单击该项目并选择卸载项目

enter image description here

现在点击该项目,然后选择编辑项目

enter image description here

您将看到以下内容:

  <ItemGroup>
        <None Include="..\connections.config">
          <Link>connections.config</Link>
        </None>
        <None Include="App.config" />
      </ItemGroup>

为了能够引用此文件,我们需要说复制链接的内容文件以滚动到底部并添加

 <Target Name="CopyLinkedContentFiles" BeforeTargets="Build">
    <Copy SourceFiles="%(Content.Identity)" DestinationFiles="%(Content.Link)" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" Condition="'%(Content.Link)' != ''" />
  </Target>

重新加载项目。

enter image description here

重复您需要共享 connections.config

的项目

您可以拥有 connections.release.config 。然后在 Web.Release.config 中添加

 <connectionStrings xdt:Transform="SetAttributes" configSource="connections.release.config" />

答案 1 :(得分:1)

原来我在这里记录的Host.Owin.IIS有一个错误https://github.com/aspnet/Home/issues/95