【问题标题】:app.config transformation not working for class library [duplicate]app.config 转换不适用于类库 [重复]
【发布时间】:2019-09-02 14:01:31
【问题描述】:

我有一个类库,其中有 app.config,转换为 app.Dev.config、app.Test.config。此类库由具有以下 web.config 转换的 Web Api 使用:Web.Dev.config、Web.Test.config。当 web api 在 DEV 中发布时,它会正确使用 web.dev.config,但是对于类库,它使用 app.config 而不是 app.dev.config

我正在使用来自 https://marketplace.visualstudio.com/items?itemName=GolanAvraham.ConfigurationTransform 的 ConfigurationTransform

我也尝试了来自:App.Config Transformation for projects which are not Web Projects in Visual Studio?的解决方案

app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="connectionUri" value="https://ssrsservice-test.azurewebsites.net" />
  </appSettings>
</configuration>

app.Dev.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="connectionUri" value="https://ssrsservice-dev.azurewebsites.net" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
  </appSettings>
</configuration>

尝试上述方法后,connectionUri 始终是测试的(来自 app.config),而不是使用 dev。

【问题讨论】:

  • 这些转换仅适用于 Web 项目。对于其他项目类型,您需要扩展。我想它叫猎豹。谷歌“visual studio extension cheetah”,你会找到的。
  • 您确定您的 Web API 会查找 app.config 吗?如果您的类库在 Web API 项目中使用,它将从 web.config 中查找其设置。现在,如果你在你的类库上运行单元测试并且这些测试依赖于设置(你应该避免),那么测试将在 app.config 中寻找它们。但是,对于使用您的类库的 Web API,web.config 将被引用。
  • 这里是SlowCheetah转换工具的链接marketplace.visualstudio.com/…
  • 你可以在不使用 Slow Cheetah 的情况下为可执行文件进行应用配置转换,但我从未见过它为类库做过
  • @RWRkeSBZ Web Api 正在寻找它自己的带有转换的 web.config,但是,类库从 app.config 获取连接字符串、REST 调用信息的 url。

标签: c# visual-studio app-config class-library


【解决方案1】:

https://stackoverflow.com/a/50506226/10485667

这篇文章中给出的解决方案帮助我得到了我正在寻找的东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-26
    • 2018-06-19
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    相关资源
    最近更新 更多