代码之家  ›  专栏  ›  技术社区  ›  jwaliszko

DI容器中的自定义生存期管理(wcf代理:Unity vs Castle Windsor)

  •  3
  • jwaliszko  · 技术社区  · 15 年前

    我找到了一个不错的帖子: Singleton WCF Proxy .

    温莎城堡

    抽象类的实现 AbstractLifestyleManager Castle.MicroKernel.Lifestyle 命名空间重写3个方法: Resolve , Dispose Release . 在 释放 方法我们可以访问 ,我们可以从中解析服务实例。

    我从那篇文章中复制了下面的代码(稍作改动):

    public class SingletonWCFProxyLifestyleManager : AbstractLifestyleManager
    {
        private object instance;
    
        public override object Resolve(Castle.MicroKernel.CreationContext context)
        {
            lock (base.ComponentActivator)
            {
                if (this.instance == null)
                {
                    this.instance = base.Resolve(context);
                }
                else
                {
                    ICommunicationObject communicationObject = this.instance as ICommunicationObject;
                    if (communicationObject != null &&
                        communicationObject.State == CommunicationState.Faulted)
                    {
                        try
                        {
                            communicationObject.Abort();
                        }
                        catch { }
    
                        this.instance = base.Resolve(context);
                    }
                }
            }
            return this.instance;
        }
    
        public override void Dispose()
        {
            if (this.instance != null)
            {
                base.Release(this.instance);
            }
        }
    
        public override void Release(object instance)
        {
    
        }
    }
    

    团结 容器。看起来像是 LifetimeManager 上课地点 Microsoft.Practices.Unity 命名空间(可选) IRequiresRecovery 接口)专用于此。

    该类提供的所有方法如下所示:

    public class SingletonWCFProxyLifestyleManager : LifetimeManager, IRequiresRecovery  
    {
       public override object GetValue()
       {
          throw new NotImplementedException();
       }
    
       public override void RemoveValue()
       {
          throw new NotImplementedException();
       }
    
       public override void SetValue(object newValue)
       {
          throw new NotImplementedException();
       }
    
       #region IRequiresRecovery Members   
       public void Recover()
       {
          throw new NotImplementedException();
       }    
       #endregion
    }
    

    问题是:

    (PS:无法访问容器的上下文,因此如何解析对象?)。

    当做

    1 回复  |  直到 14 年前
        1
  •  0
  •   jwaliszko    15 年前

    我将试着回答我的问题(我希望这是正确的…)。

    我找到了这个帖子 Writing Custom Lifetime Managers Singleton WCF Proxy .

    下面是我创造的。当然我必须测试代码。第一眼看上去还可以,但我等会儿再看。

    public class SingletonWCFProxyLifestyleManager : LifetimeManager, IRequiresRecovery, IDisposable
       {
          private static readonly object _locker = new object();
          private Guid _key;
    
          public SingletonWCFProxyLifestyleManager()
          {
             _key = Guid.NewGuid(); 
          }
    
          public override object GetValue()
          {
             Monitor.Enter(_locker);
             object result = Storage.Instance.Get(_key);
             if (result != null)
             {
                ICommunicationObject communicationObject = result
                                                           as ICommunicationObject;
                //If the proxy is in faulted state, it's aborted and a new proxy is created
                if (communicationObject != null &&
                    communicationObject.State == CommunicationState.Faulted)
                {
                   try
                   {
                      communicationObject.Abort();
                   }
                   catch
                   {
                   }
    
                   Dispose();
                   return null;   //Return before releasing monitor
                }
                Monitor.Exit(_locker);
             }
             return result;
          }
    
          public override void RemoveValue()
          {
    
          }
    
          public override void SetValue(object newValue)
          {
             Storage.Instance.Set(_key, newValue);
             TryToReleaseMonitor(); 
          }
    
          #region IRequiresRecovery Members
    
          public void Recover()
          {
             TryToReleaseMonitor();
          }
    
          #endregion
    
          private void TryToReleaseMonitor()
          {
             try
             {
                Monitor.Exit(_locker);
             }
             catch(SynchronizationLockException)
             {
             } // This is ok, just means we don't hold the lock
          }
    
          #region IDisposable Members
    
          public void Dispose()
          {
             object result = Storage.Instance.Get(_key);
             if (result != null)
             {
                try
                {
                   Storage.Instance.RemoveAndDispose(_key);
                }
                catch
                {
                   ICommunicationObject communicationObject = result as ICommunicationObject;
                   if (communicationObject != null)
                   {
                      communicationObject.Abort();
                   }
                }
             }
          }
    
          #endregion
       }
    

    Storage Get RemoveAndDispose ),但粘贴到这里太简单了。