所谓两次PerSession WCF服务的静态构造函数两次、静态、函数、WCF

2023-09-07 15:41:56 作者:永不放弃

不能udnerstand为什么类型的构造函数 PerSession / WCF服务在叫了两次。 ConcurrencyMode 。 刚刚发布了五款并发客户端里面做同样的WCF服务的方法调用,在日志中我看到静态构造函数调用了两次,第一次3秒第二次与后其他进程ID /的ThreadId 。没有例外无论是在构造函数本身也不WCF跟踪日志。 构造函数的执行时间大约是10毫秒为每个日志。这将导致所有静态字段不是所有的服务实例之间共享的假设和发生5客户端连接我有5个服务和两个不同的静态环境使变化ONSE静态字段不是体现为大家服务。

这个问题混淆了很多东西,因为我依靠在多个业务实例共​​享一些静态缓存。

服务托管在 IIS 。没有重新启动IIS,程序池回收在此时间间隔。

[AspNetCompatibilityRequirements(RequirementsMode =   AspNetCompatibilityRequirementsMode.Allowed)] [ServiceBehavior(   InstanceContextMode = InstanceContextMode.PerSession,   IncludeExceptionDetailInFaults = TRUE,   ConcurrencyMode = ConcurrencyMode.Multiple) 公共类WcfService {     私人静态只读的ILog记录;     私有静态挥发布尔typeInitialized;     静态WcfService()     {         尝试         {             //这里是typeInitialized是虚假的两个呼叫             记录仪= LogManager.GetLogger(LOGNAME);             logger.InfoFormat([PID:{0}] [THID:{1}] BEGIN静态构造函数,                Process.GetCurrentProcess(),身份证,                Thread.CurrentThread.ManagedThreadId);         }         赶上(例外的例外)         {            logger.Error(,异常的类型施工阶段的错误);         }         最后         {             logger.InfoFormat([PID:{0}] [THID:{1}] END静态构造函数,                Process.GetCurrentProcess(),身份证,                Thread.CurrentThread.ManagedThreadId);            typeInitialized = TRUE;         }     } }

解决方案

假设你正在主持与IIS服务,这是正常的行为,除非你明确地配置过程只从被剥离了允许一个单一的AppDomain。

6步达成基于ZooKeeper的分布式Session实现

如果你正在运行的进程的列表中,你会发现每一个进程ID在您的记录对应的w3wp.exe托管一个单独的应用程序域的副本。

Can't udnerstand why type constructor for PerSession/ WCF service was calling twice. ConcurrencyMode is Multiple. Just launching five simultaneous clients which do the same WCF service method call, in a log I see that static constructor was called twice, the first time and after 3 seconds second time with an other ProcessId/ThreadId. No exceptions neither in constructor itself nor WCF trace logs. Constructor execution time is ~10 milliseconds as per log. This results in all static fields are not shared between all service instances as supposed and in case of 5 client connections I have 5 services and two different static contexts so change in onse static field is not reflected for all services.

This issue confuses many things since I am relying on some static caches shared across multiple service instances.

Service is hosted in IIS. No IIS restarts, AppPool recycles on this time interval.

[AspNetCompatibilityRequirements(RequirementsMode =
  AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(
  InstanceContextMode = InstanceContextMode.PerSession, 
  IncludeExceptionDetailInFaults = true, 
  ConcurrencyMode = ConcurrencyMode.Multiple)]
public class WcfService
{ 
    private static readonly ILog logger;
    private static volatile bool typeInitialized;

    static WcfService()
    {
        try
        {
            // Here is typeInitialized is false in both calls
            logger = LogManager.GetLogger("LogName");

            logger.InfoFormat("[PID:{0}] [THID:{1}] BEGIN Static constructor",
               Process.GetCurrentProcess().Id,
               Thread.CurrentThread.ManagedThreadId);
        }
        catch (Exception exception)
        {
           logger.Error("error on type construction stage", exception);
        }
        finally
        {
            logger.InfoFormat("[PID:{0}] [THID:{1}] END Static constructor",
               Process.GetCurrentProcess().Id,
               Thread.CurrentThread.ManagedThreadId);               
           typeInitialized = true;
        }
    }
}

解决方案

Assuming you're hosting your service with IIS, this is normal behaviour unless you explicitly configure the process to only permit a single AppDomain from being spun up.

If you look at the list of running processes, you'll find each process Id in your log corresponds with a copy of w3wp.exe hosting a separate appdomain.