在运行时配置 ASP.NET 会话状态

发布于 2024-09-27 18:54:31 字数 721 浏览 6 评论 0原文

我们有一个使用 SQL Server 会话状态的 ASP.NET 网站。状态在 Web.config 中配置如下:

<sessionState mode="SQLServer" sqlConnectionString="data source=TheServer;
    User ID=TheUser;password=ThePassword;" cookieless="false" timeout="480"/>

但存在三种环境(开发/登台/生产)。所有其他连接字符串的配置如下:

<configuration>
    <connectionStrings>
        <add name="Development_Db1" connectionString="..."/>
        <add name="Production_Db1" connectionString="..."/>
    </connectionStrings>
</configuration>

在运行时,我们根据主机名选择一个连接到数据库。不幸的是,会话状态连接字符串似乎是硬编码在 web.config 中的。

有没有办法在运行时配置 SQL Server 会话状态,或使其引用 connectionStrings 部分中的连接字符串?

We have an ASP.NET web site that uses SQL Server session state. The state is configured in Web.config like:

<sessionState mode="SQLServer" sqlConnectionString="data source=TheServer;
    User ID=TheUser;password=ThePassword;" cookieless="false" timeout="480"/>

But there are three environments (development / staging / production). All the other connection strings are configured like:

<configuration>
    <connectionStrings>
        <add name="Development_Db1" connectionString="..."/>
        <add name="Production_Db1" connectionString="..."/>
    </connectionStrings>
</configuration>

At runtime, we pick one to connect to the database based on hostname. Unfortunately, the Session State connection string appears to be hard coded in web.config.

Is there a way to configure SQL Server session state at runtime, or make it refer to a connection string from the connectionStrings section?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌面超妹 2024-10-04 18:54:31

事实证明,有一种相当简单的方法可以做到这一点。会话状态提供了一项名为 分区,您可以将状态分散到多个 SQL Server 上。您可以提供一个函数来根据会话 ID (SID) 选择 SQL Server。诀窍是您可以在一台服务器上使用此功能,只需动态选择服务器即可。

web.config 配置如下所示:

<sessionState mode="SQLServer" 
              partitionResolverType="YourNamespace.PartitionResolver" 
              cookieless="false" 
              timeout="60" />

选择 SQL Server 的函数如下所示:

public class PartitionResolver : IPartitionResolver
{
    public void Initialize() {}

    // The key is a SID (session identifier)
    public String ResolvePartition(Object key)
    {
        return <grab your config here>;
    }
}

这种方法允许我们继续使用同一个 web.config 进行生产和开发。

As it turned out, there was a fairly easy way of doing this. Session State provides a feature called Partitioning, where you can spread your state over multiple SQL Servers. You can provide a function to select the SQL Server based on the session id (SID). The trick is that you can use this feature with ONE server, just to choose the server dynamically.

The web.config configuration looks like:

<sessionState mode="SQLServer" 
              partitionResolverType="YourNamespace.PartitionResolver" 
              cookieless="false" 
              timeout="60" />

The function that chooses the SQL Server looks like:

public class PartitionResolver : IPartitionResolver
{
    public void Initialize() {}

    // The key is a SID (session identifier)
    public String ResolvePartition(Object key)
    {
        return <grab your config here>;
    }
}

This approach allowed us to continue using one web.config for both production and development.

手长情犹 2024-10-04 18:54:31

如上所述,我认为 web.config 中不应该同时包含 dev 和 prod 连接字符串。您可以使用 Web 部署项目来解决该问题。您可以使用 Web 部署项目来根据构建替换您的配置设置。例如,您可以有两个外部配置文件,分别称为connectionStrings.dev.config 和connectionStrings.prod.config。如果您在 Debug 中构建,它将使用 dev.config,但如果您在 Release 中构建,它将使用 prod.config。

它与 VS 08 和 10 略有不同。以下是一些参考:

VS 2008 - http://johnnycoder.com/blog/2010/01/07/deploy-aspnet-web-applications-with-web-deployment-projects/

VS 2010 - http://www.hanselman.com/blog/WebDeploymentMadeAwesomeIfYoureUsingXCopyYoureDoingItWrong.aspx

As mentioned above, I think you should not have both dev and prod connections strings in the web.config. You can use a Web Deployment Project so solve that issue. You can use a web deployment project to replace your config settings based upon the build. For instance, you could have an two external config files called connectionStrings.dev.config and connectionStrings.prod.config. If you build in Debug, it would use the dev.config, but if you build in Release, it would use prod.config.

It's a little different from VS 08 and 10. Here are some references:

VS 2008 - http://johnnycoder.com/blog/2010/01/07/deploy-aspnet-web-applications-with-web-deployment-projects/

VS 2010 - http://www.hanselman.com/blog/WebDeploymentMadeAwesomeIfYoureUsingXCopyYoureDoingItWrong.aspx

锦上情书 2024-10-04 18:54:31

根据本文,您可以自定义会话状态提供程序:

http://www.exforsys.com/tutorials/asp.net-2.0/asp.net-2.0-customizing-the-session-state-mechanism.html

机制 这里的信息可用于设计一个环境感知会话状态提供程序,该提供程序可以根据 .config 文件中的配置或其他一些环境密钥来选择连接字符串。

According to this article, you can customize the session state provider:

http://www.exforsys.com/tutorials/asp.net-2.0/asp.net-2.0-customizing-the-session-state-mechanism.html

The information here could be used to design an environment-aware session state provider that could select the connection string based on a configuration in the .config file, or some other environmental key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文