2017-02-17 133 views
1

最初的问题变得多余,因为它是基于错误的假设,所以它被编辑以提供关于Sessions, Instancing and Concurrency以及下面接受的答案的一些信息。ASP.net实例模式有什么区别?

+0

请详细说明。例如,你在谈论依赖注入和IoC容器吗?哪一个......等 – ps2goat

+0

链接的文章不包含单词“静态”或“AppDomain”。你从哪里得到这些信息? –

+0

这篇文章的链接就是显示'PerCall'的描述。静力学分离的事实来自经验。静态引用上的'lock'在'Single'模式下工作,但不能在'PerCall'模式下工作。由于观察CLR性能计数器,“AppDomain”未被创建的假设升级。 –

回答

1

它通常会在各种托管类型中相同:IIS,Windows服务,WCF。

ASP.net如何实现内部工作,使用什么机制隔离静态变量,而无需为每个服务调用创建新的AppDomain。

有一个很好的代码项目文章,解释了高层次的差异,如果你想看到底下你应该使用像ILSpy或Reflector这样的反编译器来查看实现细节。 实现的不同之处在于,每个请求或每个会话的对象是单例,实例化还是汇集的方式

这里是代码项目文章的一个片段:Three ways to do WCF instance management

enter image description here

enter image description here

enter image description here

要设置PerCall的InstanceContext使用ServiceBehaviour属性:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 

这里是服务行为属性的源代码:

https://referencesource.microsoft.com/#System.ServiceModel/System/ServiceModel/ServiceBehaviorAttribute.cs,b743193260862969,references

具体在那里建立了instanceModeContext提供者:

dispatch.InstanceContextProvider = InstanceContextProviderBase.GetProviderForMode(this.instanceMode, dispatch); 

if ((this.instanceMode == InstanceContextMode.Single) && 
    (dispatch.SingletonInstanceContext == null)) 
{ 
    if (singleton == null) 
    { 
     if (this.wellKnownSingleton != null) 
     { 
      singleton = new InstanceContext(serviceHostBase, this.wellKnownSingleton, true, false); 
     } 
     else if (this.hiddenSingleton != null) 
     { 
      singleton = new InstanceContext(serviceHostBase, this.hiddenSingleton, false, false); 
     } 
     else 
     { 
      singleton = new InstanceContext(serviceHostBase, false); 
     } 

     singleton.AutoClose = false; 
    } 
    dispatch.SingletonInstanceContext = singleton; 
} 

所以,你可以看到区别是wellKnownSingleton之间的开关,一个hiddenSingleton或者两者都不是,这是实现对象是单例,实例化或合并的方式。

您可以深入了解代码库并查看代码以查看有关实现细节的更多信息。

+0

感谢您的好帖子。正如我在上面的评论中所说的,我们有几个实例运行在不同的机器上,它让我在星期五晚上感到恐慌,因为我的世界里静态只能通过创建AppDomain而被隔离^ - ^ –