不能将佣金问题应用于组件后期绑定,因为它似乎是一个无目标代理。目前不支持这些
Can not apply commission concerns to component Late bound because it appears to be a target-less proxy. Currently those are not supported
我设计了一个简单的单元测试模式并使用了 moq 库。但是执行Container.Resolve<IInterface>()
方法时抛出异常。以下为温莎城堡报名方式:
container.Register(Component.For<TBase>().UsingFactoryMethod(() =>
{
var mockObject = new Mock<TBase>();
var mockType = typeof(TBase);
if (container is Core.IoCContainer.IoCCore.CustomContainer)
{
var resolvingDelege = (container as Core.IoCContainer.IoCCore.CustomContainer).ResolvingTestDelegateList[mockType];
if (resolvingDelege != null)
{
resolvingDelege(mockType, mockObject);
}
}
return mockObject.Object;
})
.LifestyleTransient());
执行IoCCore.Container.Resolve< ISampleDataContext>();
时,抛出异常。异常消息是 "Can not apply commission concerns to component Late bound Data.Context.ISampleDataContext because it appears to be a target-less proxy. Currently those are not supported."
我搜索了一下问题,发现mockObject.Object
是Castle.DynamicProxy.IProxyTargetAccessor
。因此,在解析实例时,下面 Castle.MicroKernel.ComponentActivator.AbstractComponentActivator
class 中的代码是问题发生的地方:
protected virtual void ApplyCommissionConcerns(object instance)
{
if (Model.Lifecycle.HasCommissionConcerns == false)
{
return;
}
instance = ProxyUtil.GetUnproxiedInstance(instance);
if (instance == null)
{
// see http://issues.castleproject.org/issue/IOC-332 for details
throw new NotSupportedException(string.Format("Can not apply commission concerns to component {0} because it appears to be a target-less proxy. Currently those are not supported.", Model.Name));
}
ApplyConcerns(Model.Lifecycle.CommissionConcerns, instance);
}
UsingFactoryMethod
正在解析的对象是Castle.DynamicProxy.IProxyTargetAccessor
,它是由模拟对象创建的。但是,Castle Windsor 假设 IProxyTargetAccessor
是由它自己创建的,而不是模拟对象。这会导致 CommissionConcerns
的空实例从我的模拟实例返回。
这个错误是否有可能的解决方法?我应该等待更新吗?
我针对这个问题制定了解决方法。我更改了 UseFactoryMethod 方法而不是下面的代码
Component.For<TBase>().UsingFactoryMethod(() =>
{
var mockObject = new Mock<TBase>();
var mockType = typeof(TBase);
if (container is Core.IoCContainer.IoCCore.CustomContainer)
{
var resolvingDelege = (container as Core.IoCContainer.IoCCore.CustomContainer).ResolvingTestDelegateList[mockType];
if (resolvingDelege != null)
{
resolvingDelege(mockType, mockObject);
}
}
//The code that was changed
//==============================================
var type = mockObject.Object.GetType();
var field = type.GetField("__target");
field.SetValue(mockObject.Object, new Object());
//==============================================
return mockObject.Object;
})
.LifestyleTransient());
我设计了一个简单的单元测试模式并使用了 moq 库。但是执行Container.Resolve<IInterface>()
方法时抛出异常。以下为温莎城堡报名方式:
container.Register(Component.For<TBase>().UsingFactoryMethod(() =>
{
var mockObject = new Mock<TBase>();
var mockType = typeof(TBase);
if (container is Core.IoCContainer.IoCCore.CustomContainer)
{
var resolvingDelege = (container as Core.IoCContainer.IoCCore.CustomContainer).ResolvingTestDelegateList[mockType];
if (resolvingDelege != null)
{
resolvingDelege(mockType, mockObject);
}
}
return mockObject.Object;
})
.LifestyleTransient());
执行IoCCore.Container.Resolve< ISampleDataContext>();
时,抛出异常。异常消息是 "Can not apply commission concerns to component Late bound Data.Context.ISampleDataContext because it appears to be a target-less proxy. Currently those are not supported."
我搜索了一下问题,发现mockObject.Object
是Castle.DynamicProxy.IProxyTargetAccessor
。因此,在解析实例时,下面 Castle.MicroKernel.ComponentActivator.AbstractComponentActivator
class 中的代码是问题发生的地方:
protected virtual void ApplyCommissionConcerns(object instance)
{
if (Model.Lifecycle.HasCommissionConcerns == false)
{
return;
}
instance = ProxyUtil.GetUnproxiedInstance(instance);
if (instance == null)
{
// see http://issues.castleproject.org/issue/IOC-332 for details
throw new NotSupportedException(string.Format("Can not apply commission concerns to component {0} because it appears to be a target-less proxy. Currently those are not supported.", Model.Name));
}
ApplyConcerns(Model.Lifecycle.CommissionConcerns, instance);
}
UsingFactoryMethod
正在解析的对象是Castle.DynamicProxy.IProxyTargetAccessor
,它是由模拟对象创建的。但是,Castle Windsor 假设 IProxyTargetAccessor
是由它自己创建的,而不是模拟对象。这会导致 CommissionConcerns
的空实例从我的模拟实例返回。
这个错误是否有可能的解决方法?我应该等待更新吗?
我针对这个问题制定了解决方法。我更改了 UseFactoryMethod 方法而不是下面的代码
Component.For<TBase>().UsingFactoryMethod(() =>
{
var mockObject = new Mock<TBase>();
var mockType = typeof(TBase);
if (container is Core.IoCContainer.IoCCore.CustomContainer)
{
var resolvingDelege = (container as Core.IoCContainer.IoCCore.CustomContainer).ResolvingTestDelegateList[mockType];
if (resolvingDelege != null)
{
resolvingDelege(mockType, mockObject);
}
}
//The code that was changed
//==============================================
var type = mockObject.Object.GetType();
var field = type.GetField("__target");
field.SetValue(mockObject.Object, new Object());
//==============================================
return mockObject.Object;
})
.LifestyleTransient());