我找到了一篇不错的文章:Singleton WCF Proxy。它讲述了使用Castle Windsor DI容器实现WCF代理生命周期的方法。
从
我已经复制了该帖子中的代码(稍作修改),如下所示:
我想使用Unity容器提供相同的功能。似乎
该类提供的所有方法如下所示:
从
Castle.MicroKernel.Lifestyle
命名空间实现抽象类AbstractLifestyleManager
,覆盖了3个方法:Resolve
、Dispose
和Release
。在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)相同的功能?
(附注:没有访问容器上下文的权限,那么我该如何解决对象呢?)
谢谢!