NHibernate + IIS7.5 抛出“会话工厂已使用 nhibernate.current_session 密钥进行配置”

发布于 2024-12-02 17:01:28 字数 539 浏览 0 评论 0原文

使用 S#arp Architecture 2.3 RC,我尝试发布两个主要使用相同代码库的网站。我发布的第一个应用程序运行正确,但是,在部署并尝试运行第二个应用程序时,我收到以下异常:

Access to the path 'C:\Windows\TEMP\nhibernate.current_session--851346262.bin' is denied.

当我刷新窗口时,我收到此异常:

A session factory has already been configured with the key of nhibernate.current_session

我什至尝试将它们加载到单独的应用程序池中并且错误仍然存​​在。我不明白为什么我的两个应用程序的会话在 IIS 上发生冲突,而它们在 Visual Studio 上运行得很好。

仅供参考,我正在尝试在 Windows Server 2008 R2 + IIS 7.5 上运行它们

有什么帮助吗?

Using S#arp Architecture 2.3 RC, I am trying to publish two websites which use -mostly- the same codebase. The first of the applications I publish runs correctly, however, upon deploying and trying to run the second application, I get the following exception:

Access to the path 'C:\Windows\TEMP\nhibernate.current_session--851346262.bin' is denied.

When I refresh the window, I then get this exception:

A session factory has already been configured with the key of nhibernate.current_session

I have even tried loading them in separate Application Pools and the error persists. I don't understand how is it that my two applications' sessions are colliding on IIS, whereas they run just fine with Visual Studio.

FYI, I am trying to run them on Windows Server 2008 R2 + IIS 7.5

Any help?

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

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

发布评论

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

评论(3

稚然 2024-12-09 17:01:28

我通过将应用程序池的身份更改为具有更多权限的身份(例如 LocalSystem)来“修复”它。如果您有权访问源代码,则不是合适的修复方法。

I "fixed" it by changing the application pool's identity to one with more permission e.g. LocalSystem. Not a suitable fix if you have access to the source.

じ违心 2024-12-09 17:01:28

对于使用 #arch 和 NHibernateConfigurationFileCache 的单个网站的情况。问题很可能来自
- NHibernateConfigurationFileCache 使用 Path.GetTempPath() 来保存配置
- 网站使用网络服务或应用程序池标识下的应用程序池运行
- 该身份没有临时路径的写入权限

更好的解决方案是更改代码以使用身份具有权限的临时路径,或者
授予对该身份的临时路径的写入权限(例如,将 c:\windows\temp 上的写入权限授予
IIS APPPOOL\CustomAppPool)

For the case where it is a single website using #arch and NHibernateConfigurationFileCache. The problem most likely comes from
- NHibernateConfigurationFileCache using Path.GetTempPath() to persist the config
- The website is run using application pool under network service or application pool identity
- The identity doesn't have write permission to the temp path

The better solution will be to change the code to use a temp path where the identity will have permission, or
Grant write permission to the temp path for the identity (e.g. grant write permission on c:\windows\temp to
IIS APPPOOL\CustomAppPool)

╄→承喏 2024-12-09 17:01:28

看起来 S#arp 架构尝试为这两个应用程序使用相同的 NHibernate 会话工厂。它可能会发生冲突,因为当它在磁盘上保留配置时,它对两个 Web 应用程序使用相同的默认“工厂密钥”(nhibernate.current_session)。您应该使用不同的“工厂密钥”吗?为此,您可以阅读有关多个的常见问题解答部分数据库(尽管您没有多个数据库,但您的解决方案可能是相同的):

问:如何添加对多个数据库的支持?

A:: 要向您的项目添加对其他数据库的支持:

创建一个 NHibernateForOtherDb.config 文件,其中包含
到新数据库的连接字符串

在 YourProject.Web.Mvc/Global.asax.cs 中,紧接在
NHibernateSession.Init() 初始化第一个会话工厂,
对 NHibernateSession.AddConfiguration() 的额外调用。虽然
首先 NHibernate 初始化假设有一个默认的工厂密钥,你将
需要为第二次初始化提供显式工厂密钥。
该工厂密钥将由绑定到的存储库引用
新数据库也是如此...

看看 S#arp sources 我发现他们使用以下逻辑在磁盘上创建文件:

    var fileName = string.Format(
        "{0}-{1}.bin", 
        configKey, 
        Assembly.GetCallingAssembly().CodeBase.GetHashCode());

因此,如果您不想更改配置/工厂密钥,您可以尝试从不同的程序集调用此代码。

编辑:

看起来 NHibernateSession.Init 不接受允许您指定工厂密钥的重载。但它还有其他公共方法,您可以调用而不调用 Init:

NHibernateSession.InitStorage(storage);
try {
    NHibernateSession.AddConfiguration("%PUT YOUR OWN FACTORY KEY%", ...);
} catch {
    NHibernateSession.Storage = null;
    throw;
}

It looks like S#arp Architecture tries to use the same NHibernate session factory for both applications. It maybe colliding because it uses the same default 'factory key' (nhibernate.current_session) for both web apps when it persists configuration on disk. Should you be using different 'factory keys'? In order to do this you can read the FAQ section about multiple databases (although you don't have multiple databases, your solution may be the same):

Q: How do I add support for multiple databases?

A:: To add support for an additional database to your project:

Create an NHibernateForOtherDb.config file which contains the
connection string to the new database

Within YourProject.Web.Mvc/Global.asax.cs, immediately under the
NHibernateSession.Init() to initialize the first session factory, an
additional call to NHibernateSession.AddConfiguration(). While the
first NHibernate initialization assumes a default factory key, you'll
need to provide an explicit factory key for the 2nd initialization.
This factory key will be referred to by repositories which are tied to
the new database as well...

Looking at S#arp sources I found that they use following logic to create file on disk:

    var fileName = string.Format(
        "{0}-{1}.bin", 
        configKey, 
        Assembly.GetCallingAssembly().CodeBase.GetHashCode());

So if you don't want to change config/factory key you can try calling this code from different assembly.

EDIT:

It looks like NHibernateSession.Init does not take overload that lets you specify factory key. But it has other public methods that you can call instead of Init:

NHibernateSession.InitStorage(storage);
try {
    NHibernateSession.AddConfiguration("%PUT YOUR OWN FACTORY KEY%", ...);
} catch {
    NHibernateSession.Storage = null;
    throw;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文