【发布时间】:2010-07-06 13:23:52
【问题描述】:
我找到了不错的帖子:Singleton WCF Proxy。
它是关于使用Castle Windsor DI 容器实现WCF 代理生命周期的。
从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 命名空间(以及可选的IRequiresRecovery 接口)中的LifetimeManager 类专用于此。
该类提供的所有方法如下所示:
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)相同的功能?
(PS:无法访问容器的上下文,所以我如何解析对象?)。
问候
【问题讨论】:
标签: c# wcf castle-windsor unity-container