【问题标题】:Prism Unity interception errorPrism Unity 拦截错误
【发布时间】:2018-06-16 23:47:16
【问题描述】:

棱镜 = 6.3; unity拦截=5.3;

我正在重构一个类以使用 Unity Interception 和 PRISM viewModelLocator = true 来消除横切关注点。

在模块中,我注册了接口和类以及识别拦截器:

Container.RegisterType<ITaxViewModel, TaxViewModel>(
new Interceptor<InterfaceInterceptor>(), 
new InterceptionBehavior<LoggingInterceptionBehavior>());

我运行应用程序然后按下税收按钮,我收到以下错误:

System.Windows.Markup.XamlParseException HResult=0x80131501
Message='设置属性'Prism.Mvvm.ViewModelLocator.AutoWireViewModel' 抛出异常。行号“8”和行位置“14”。
Source=PresentationFramework

堆栈跟踪:在 System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory,布尔值 skipJournaledProperties,对象根对象, XamlObjectWriterSettings 设置,Uri baseUri)

在 System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader,布尔型 skipJournaledProperties,对象 rootObject, XamlAccessLevel accessLevel, Uri baseUri)

在 System.Windows.Markup.XamlReader.LoadBaml(Stream 流, ParserContext parserContext, Object parent, Boolean closeStream)

在 System.Windows.Application.LoadComponent(Object 组件,Uri 资源定位器)

在 FOO.Views.TaxView.InitializeComponent()

在 C:\FooControlLibrary\Views\TaxView.xaml:第 1 行

内部异常1:ActivationException:发生激活错误 在尝试获取 TaxViewModel 类型的实例时,键“”

内部异常 2:ResolutionFailedException:解决 依赖失败,类型=“FOO.VM.TaxViewModel”,名称=“(无)”。 异常发生时:解决时。例外是: ArgumentException - FOO.VM.TaxViewModel 类型不可拦截。 参数名称:interceptedType


发生异常时,容器为:

正在解决 FOO.VM.TaxViewModel,(无)

内部异常 3:ArgumentException:类型 FOO.VM.TaxViewModel 是 不可拦截。参数名称:interceptedType

我不确定 PRISM 是否可以解析 Unity 创建的代理。有谁知道怎么做?

【问题讨论】:

  • 您是否尝试将 Unity 5.x 与 Prism 6.3 一起使用?因为,那是行不通的。您只能在 Prism 7+ 中成功使用 Unity 5.x。
  • 我犯了一个错误,在左侧而不是右侧查看 NuGet Unity 包。 Unity 和 Unity.Interception 的安装版本均为 4.0.1。

标签: c# wpf unity-container prism


【解决方案1】:

我可以毫无例外地。 棱镜.Unity:6.3.0 统一拦截:4.0.1

protected override DependencyObject CreateShell()
{
    Container.AddNewExtension<Interception>();
    Container.RegisterType<jobsRepository>(new Interceptor<VirtualMethodInterceptor>(),
                                           new InterceptionBehavior<LogBehavior>());

    var jobsRepo = Container.Resolve<jobsRepository>();
    jobsRepo.GetJobs();
    return Container.Resolve<MainWindow>();
}

IInterceptionBehavior

public class LogBehavior : IInterceptionBehavior
{
    public bool WillExecute => true;

    public IEnumerable<Type> GetRequiredInterfaces()
    {
        return Enumerable.Empty<Type>();
    }

    public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
    {
        Debug.WriteLine("# before: " + input.MethodBase.Name);
        var result = getNext()(input, getNext);
        Console.WriteLine($"# end:{input.MethodBase.Name}");
        return result;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多