これに出くわし、将来の読者のために私のソリューションを追加すると思いました。これは基本的に、CSの「切り替え」をカプセル化し、単一の管理ポイントを提供するMauricio Schefferが提案したものです(各セッション呼び出しに渡す必要があるよりも、これが好きです。 「ミス」して失敗します)。
クライアントの認証中に接続文字列を取得し、コンテキストを設定してから、次のIConnectinProvider実装を使用して、セッションが開かれるたびにCSにその値を設定します。
/// <summary>
/// Provides ability to switch connection strings of an NHibernate Session Factory (use same factory for multiple, dynamically specified, database connections)
/// </summary>
public class DynamicDriverConnectionProvider : DriverConnectionProvider, IConnectionProvider
{
protected override string ConnectionString
{
get
{
var cxnObj = IsWebContext ?
HttpContext.Current.Items["RequestConnectionString"]:
System.Runtime.Remoting.Messaging.CallContext.GetData("RequestConnectionString");
if (cxnObj != null)
return cxnObj.ToString();
//catch on app startup when there is not request connection string yet set
return base.ConnectionString;
}
}
private static bool IsWebContext
{
get { return (HttpContext.Current != null); }
}
}
次に、NHConfig中に配線します。
var configuration = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005
.Provider<DynamicDriverConnectionProvider>() //Like so