我正在从 2.5.1 版升级到 3.1.0 版,而过去的工作不再适用。我正在尝试解决一个创建使用泛型的组件的工厂。抛出的错误是“ComponentActivator:无法代理”,其中无法解析的工厂名称
public class MyObject { }
public class MyContext { }
public class DummyComponent<T> : IDummyComponent<T> where T : MyObject { }
public interface IDummyComponent<T> { }
public interface IDummyComponentFactory
{
IDummyComponent<T> Create<T>(object o);
}
class Program
{
static void Main(string[] args)
{
var windsorContainer = new WindsorContainer();
windsorContainer.AddFacility<TypedFactoryFacility>()
.Register(
Component.For(typeof(IDummyComponent<>)).ImplementedBy(typeof(DummyComponent<>)),
Component.For<IDummyComponentFactory>().AsFactory());
var factory = windsorContainer.Resolve<IDummyComponentFactory>(); <-- Error Occurs Here
var myDummyComponent = factory.Create<MyObject>(new object());
}
}
此代码在 Entity Framework 存储库中以下列方式使用,用于将 EntityContext 传递到另一个存储库,以便像这样处理已删除的对象:
public virtual IEnumerable<T> Where(Expression<Func<T, bool>> predicate, bool showDeleted = false, MergeOption mergeOption = MergeOption.AppendOnly)
{
if (typeof(IDeletable).IsAssignableFrom(typeof(T)))
{
var factory = Container.Instance.Resolve<IDeletableRepositoryFactory>();
var repository = factory.GetDeletableRepository<T>(EntityContext);
return repository.Where(predicate, showDeleted, mergeOption);
}
return GetObjectSet(mergeOption).Where(predicate);
}
更新:包括内部异常
System.Security.VerificationException
{"Operation could destabilize the runtime."} at
Castle.MicroKernel.Proxy.ProxyOptions.get_MixIns() in
c:\BuildAgent\work\5b096cace0fecb1f\src\Castle.Windsor\MicroKernel\Proxy\ProxyOptions.cs:line 96 at
Castle.Windsor.Proxy.DefaultProxyFactory.CreateProxyGenerationOptionsFrom(ProxyOptions proxyOptions, IKernel kernel, CreationContext context, ComponentModel model) in
c:\BuildAgent\work\5b096cace0fecb1f\src\Castle.Windsor\Windsor\Proxy\DefaultProxyFactory.cs:line 178 at
Castle.Windsor.Proxy.DefaultProxyFactory.Create(IKernel kernel, Object target, ComponentModel model, CreationContext context, Object[] constructorArguments) in
c:\BuildAgent\work\5b096cace0fecb1f\src\Castle.Windsor\Windsor\Proxy\DefaultProxyFactory.cs:line 105 at
Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstance(CreationContext context, ConstructorCandidate constructor, Object[] arguments) in
c:\BuildAgent\work\5b096cace0fecb1f\src\Castle.Windsor\MicroKernel\ComponentActivator\DefaultComponentActivator.cs:line 123`