在运行时配置ASP.NET会话状态 [英] Configure ASP.NET Session State at runtime
问题描述
我们必须使用SQL Server会话状态的ASP.NET网站。国家在 Web.config中配置
这样的:
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>
在运行时,我们选择一个连接到基于主机的数据库。不幸的是,会话状态的连接字符串似乎很难$ C $光盘的web.config
。
有没有在运行时配置SQL Server会话状态,或使其指从是connectionStrings
部分连接字符串的方法吗?
Is there a way to configure SQL Server session state at runtime, or make it refer to a connection string from the connectionStrings
section?
推荐答案
事实证明,有这样做的一个相当简单的方法。会话状态提供了一个名为分区,在那里你可以发$ P $垫你的状态在多个功能SQL服务器。您可以提供一个功能选择基于会话ID(SID)在SQL Server。诀窍在于,你可以使用此功能在一台服务器,就可以动态选择服务器。
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.
的的web.config
配置如下:
<sessionState mode="SQLServer"
partitionResolverType="YourNamespace.PartitionResolver"
cookieless="false"
timeout="60" />
这是选择在SQL Server的功能如下:
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>;
}
}
此方法允许我们继续使用一个web.config文件为生产和发展。
This approach allowed us to continue using one web.config for both production and development.
这篇关于在运行时配置ASP.NET会话状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!