简体   繁体   English

程序集的ConnectionString

[英]ConnectionString for assemblies

I have a website with a connection string listed in its web.config. 我有一个网站,其web.config中列出了一个连接字符串。 The connection string is altered by the Publish feature so that it can reference a development database until it is released, when it references a separate release database. 连接字符串由“发布”功能更改,以便它可以引用开发数据库,​​直到它被引用,当它引用单独的发布数据库时。

The website accesses the databases through some assemblies, though. 但是,该网站通过一些程序集访问数据库。 They are Class Libraries so they can't be Published, at least as far as I can tell. 它们是类库,因此它们无法发布,至少就我所知。 I read that the web.config would override the app.config connectionstrings, but that doesn't seem to be happening. 我读到web.config会覆盖app.config连接字符串,但似乎没有发生。

Whenever I Publish the release site references the development database, unless I alter the assemblies app.config files to reference the release database. 每当我发布发布站点引用开发数据库时,除非我更改程序集app.config文件以引用发布数据库。

I don't want to have to remember to do that every time. 我不想每次都记得那样做。 How do I handle this? 我该如何处理?

You've got two issues here: 你有两个问题:

1. How to remember to publish the correct settings each time you publish: 1.如何记住每次发布时发布正确的设置:

One way to deploy such settings is by using web.config transformations in Visual Studio. 部署此类设置的一种方法是在Visual Studio中使用web.config转换。 This is pretty easy to set up and means that you do not have to remember to update the settings each time you publish. 这很容易设置,这意味着您不必记住每次发布时都更新设置。

As well as debug and release environments, you can also create transforms for "UAT", "Staging", "Beta" or whatever other configs you might need. 除了调试和发布环境,您还可以为“UAT”,“Staging”,“Beta”或您可能需要的任何其他配置创建转换。

You might find these articles useful: here , here and here . 您可能会发现这些文章很有用: 这里这里这里

For example, here is a transform for a Release environment: 例如,以下是Release环境的转换:

    <?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <connectionStrings>
      <add name="MyDB" 
        connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" 
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </connectionStrings>
    <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
      <customErrors defaultRedirect="GenericError.htm"
        mode="RemoteOnly" xdt:Transform="Replace">
        <error statusCode="500" redirect="InternalError.htm"/>
      </customErrors>
  </system.web>
</configuration>

Above, you will see that the transform for Release mode sets the attributes of the MyDB connection string (xdt:Transform="SetAttributes"), removes the debug attribute from the compilation section and replaces the customErrors section with a new version. 在上面,您将看到Release模式的转换设置了MyDB连接字符串的属性(xdt:Transform =“SetAttributes”),从编译部分删除了debug属性,并用新版本替换了customErrors部分。

It's a simple, yet very powerful technique. 这是一种简单但非常强大的技术。

2. How to get your assemblies to pick up the settings in the web.config 2.如何让程序集在web.config中获取设置

If your libraries have been written in the usual way, they should be retrieving their connection strings by simply accessing the [Web]ConfigurationManager.ConnectionStrings property. 如果您的库是以通常的方式编写的,那么它们应该只需访问[Web] ConfigurationManager.ConnectionStrings属性来检索它们的连接字符串。 Like @Bob Horn says, they should then pick up the settings from the host process's config file (in this case the web.config of your web app). 就像@Bob Horn说的那样,他们应该从主机进程的配置文件中获取设置(在这种情况下是你的web应用程序的web.config)。

However, sometimes you might find that a library is getting its settings from a .Settings file in the project, in which case things get a little more complicated. 但是,有时您可能会发现某个库正在从项目中的.Settings文件中获取其设置,在这种情况下,事情变得更复杂一些。 You will need to copy the settings section of the app.config in to the web.config. 您需要将app.config的settings部分复制到web.config中。 (You can also do this using the transforms technology described above.) (您也可以使用上述变换技术执行此操作。)

If you have access to the source code of the other assemblies, find the part of the code that retrieves connection strings. 如果您可以访问其他程序集的源代码,请找到检索连接字符串的代码部分。 If it's not accessing the ConfigurationManager class, then that might explain why it's not picking up the web.config file. 如果它没有访问ConfigurationManager类,那么这可能解释了为什么它没有获取web.config文件。

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

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