ASP.NET是否影响多线程单身Structuremap类? [英] Does ASP.NET multithreading impact Structuremap singleton class?

查看:160
本文介绍了ASP.NET是否影响多线程单身Structuremap类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET MVC项目我有一个是通过Structuremap实例化并配置为单例类。



由于ASP.NET本质上是多线程的,并Singleton模式不是默认线程安全的,它会造成什么问题?



我面临当多个实例正在返回配置为单例类的问题。 。可这个问题是因为实例正在从不同的线程请求



编辑:A更详细的描述对这个问题给出Structuremap在辛格尔顿返回多个实例



EDIT2:这里是什么我的课呢

 类DerviedClass描述:BaseInterface 
{
ISession的会话

DerivedClass()
{
会议= ObjectFactory.GetInstance<&ISession的GT;();
}

公共BOOL DoWork的
{
返回session.QueryOver< MyTable的方式>()行数()> 0;
}
}


解决方案

拍摄考虑到您的扩展版本,在 DerviedClass 的例子:我会说,这是一个非常危险的,可能会导致很多问题。



我的疑虑:



1)ASP 网络 + NHibernate的 == 的Isession 请求> 的。换句话说,当应用程序启动的 的,创建了辛格尔顿,并提供与的ISession 每页的当前请求的即第一次请求



嗯,至少,在的Isession 应通过工厂将收到需要的时候,可以肯定,这是的当前请求有关。



2)的情况下,NHibernate的的Isession 在构造函数中创建,旨在成为indpendent上在当前请求的,这可能意味着:




  • 这是长时间运行,打开更多的请求中未关闭 - 可能的锁定问题。或

  • 有需要一些开/关ISession的管理,而不是基于当前请求



的一点是,我没有看到Singleton模式的好处在这里。我们不需要,不提供普通,要求indpendent的东西。



我们做回依赖的请求(和它的会话范围)的数据。所以,我建议使用:

  .HybridHttpOrThreadLocalScoped()

与每个请求创建实例的开销是没有什么用的逻辑生命周期使用的效益比较。



不过,是的,这只是我的建议......因为我看到许多潜在问题(锁定,在会话ALA脏对象未预期的改变可能被COMMITED等)


In my ASP.NET MVC project I have a class which is instantiated via Structuremap and is configured as a singleton.

Given that ASP.NET is inherently multithreaded and the Singleton pattern is not threadsafe by default, will it cause any issues?

I faced an issue where multiple instances were being returned for a class configured as Singleton. Could this problem be because of the instances being requested from different threads.

EDIT : A more detailed description is given on this question.Structuremap in Singleton returning multiple instances

EDIT2 : Here is a description of what my class does

class DerviedClass: BaseInterface
{
   ISession session

   DerivedClass()
   {
      session = ObjectFactory.GetInstance<ISession>();
   }

   public bool DoWork
   {
       return session.QueryOver<MyTable>().RowCount() > 0;
   }
}

解决方案

Taking into account your extended version, the example of the DerviedClass: I would say, that this is a very risky, and could cause lot of issues.

My concerns:

1) ASP.NET + NHibernate == ISession per Request. Other words, when application starts (triggered by first request most likely, which could be more often if automated restarts ar in place), the Singleton is created and provided with an instance of the ISession per Current Request i.e First Request.

Well at least, the ISession should be recieved via Factory whenever needed, to be sure, that it is Current Request related.

2) In case, that the NHibernate ISession created in constructor, is intended to be indpendent on the Current Request, it could mean:

  • it is long running, opened and not closed among more requests - possible lock issues. Or
  • there is needed some open/close ISession management, not based on the current request

The point is, that I do not see the benefit of the Singleton pattern here. We do not need and do not provide common, request indpendent stuff.

We do return data dependent to the request (and its session scope). So, what I would suggest to use:

.HybridHttpOrThreadLocalScoped()

The overhead with creating instance per request is nothing in comparison with the benefit of logical life-cycle usage.

But, yes, this is just my suggestion... Because I see many potential issues (locking, un-intended changes ala dirty object in the session which could be commited etc.)

这篇关于ASP.NET是否影响多线程单身Structuremap类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆