在DI容器中自定义生命周期管理(WCF代理:Unity vs Castle Windsor)

3
我找到了一篇不错的文章:Singleton WCF Proxy。它讲述了使用Castle Windsor DI容器实现WCF代理生命周期的方法。
Castle.MicroKernel.Lifestyle命名空间实现抽象类AbstractLifestyleManager,覆盖了3个方法:ResolveDisposeRelease。在Release方法中,我们可以访问context,从中解析服务实例。
我已经复制了该帖子中的代码(稍作修改),如下所示:
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)
    {

    }
}

我想使用Unity容器提供相同的功能。似乎Microsoft.Practices.Unity命名空间中的LifetimeManager类(以及可选的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
}

下面是问题:

如何在第二个例子(使用Unity)中提供与第一个例子(使用Castle Windsor)相同的功能?

(附注:没有访问容器上下文的权限,那么我该如何解决对象呢?)

谢谢!

1个回答

0

我会尝试回答我的问题(希望是正确的...)。

我发现了这篇文章编写自定义生命周期管理器。我一直在尝试根据之前描述的解决方案来实现它,基于那篇文章和之前的一篇文章:单例WCF代理

下面是我创建的内容。当然,我必须测试那段代码。乍一看,它还不错,但我以后再看。

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 的实用程序类,用于缓存服务实例(它包含哈希表和一些实用方法,如 GetRemoveAndDispose),但它对于在此处粘贴来说过于简单。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接