gpt4 book ai didi

c# - MEF 组合错误不传播内部异常(通过消息除外)

转载 作者:行者123 更新时间:2023-11-30 20:55:06 24 4
gpt4 key购买 nike

我正在使用 MEF 将来自不同来源的模块加载到我的应用程序中。我有一个例子(下面的代码),我创建了一个可组合的类,它在构造函数中抛出异常。该异常导致组合失败,从而抛出一个异常,说明“为什么”......在声明中它说原因是 InvalidCastException......这是真的。

如果我打印异常,我知道它失败的原因,但我如何检索抛出的实际原始异常 (InvalidCastException),以便我正确处理它——而不仅仅是一个generic catch,它会错过其他异常——在要求该类实例的模块中?查询 CompositionException.Errors 也没有给我原始异常...

代码:

using System.ComponentModel.Composition;
using Microsoft.Practices.ServiceLocation;

[Export(typeof(A))]
Class A
{
[ImportingConstructor]
public A(ISomeinterface x, ISomeOtherInterface y)
{
//// Throw some exception (in my code it happens to be an InvalidCastException)
throw new InvalidCastException ("Unable to cast object of type 'Sometype' to type 'Someothertype'.");
}
}

Class B
{
public B(IServiceLocator serviceLocator)
{
try
{
//// Here is where the exception would be thrown as an "ActivationException"
A myAInstance = serviceLocator.GetInstance(A);
}
catch (ActivationException activationException)
{
//// Print original activation exception from trying to get the service
System.Diagnostics.Debug.WriteLine(activationException);

if (ex.InnerException is CompositionException)
{
CompositionException compositionException = (CompositionException)activationException.InnerException;

//// *** Here is where I want to retrieve the InvalidCastException in order to handle it properly
//// *** Also, componentException.InnerException is "null" and so is the componentException.Errors[0].Exception.InnerException
}
else
{
throw;
}
}
}
}

输出:

Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type A, key "" ---> System.ComponentModel.Composition.CompositionException: The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

1) Unable to cast object of type 'Sometype' to type 'Someothertype'.

Resulting in: An exception occurred while trying to create an instance of type 'A'.

Resulting in: Cannot activate part 'A'.
Element: A --> A --> DirectoryCatalog (Path="C:\path\to\code\")

Resulting in: Cannot get export 'A (ContractName="A")' from part 'A'.
Element: A (ContractName="A") --> A --> DirectoryCatalog (Path="C:\path\to\code\")

at System.ComponentModel.Composition.Hosting.CompositionServices.GetExportedValueFromComposedPart(ImportEngine engine, ComposablePart part, ExportDefinition definition)
at System.ComponentModel.Composition.Hosting.CatalogExportProvider.GetExportedValue(CatalogPart part, ExportDefinition export, Boolean isSharedPart)
at System.ComponentModel.Composition.ExportServices.GetCastedExportedValue[T](Export export)
at System.ComponentModel.Composition.ExportServices.<>c__DisplayClass10`2.<CreateSemiStronglyTypedLazy>b__d()
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at Microsoft.Practices.Prism.MefExtensions.MefServiceLocatorAdapter.DoGetInstance(Type serviceType, String key) in c:\release\WorkingDir\PrismLibraryBuild\PrismLibrary\Desktop\Prism.MefExtensions\MefServiceLocatorAdapter.cs:line 73
at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)
--- End of inner exception stack trace ---
at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key)
at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance[TService]()
at <code> line whatever...
System.ComponentModel.Composition.CompositionException: The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

最佳答案

我找到了实际异常嵌套在 compositionException.Errors 层次结构深处的解决方案。人们会认为原始异常会在原始组合异常下,但实际上这是访问它的方法:

它恰好是另一个组合异常的错误之一内的内部异常,它本身就是原始组合异常中的错误之一

CompositionException compositionException = (CompositionException)activationException.InnerException;
CompositionException innerCompositionException = compositionException.Errors[0].Exception;
InvalidCastException castException = (InvalidCastException)innerCompositionException.Errors[0].Exception.InnerException

我不会删除它,以防其他人遇到同样的事情。

关于c# - MEF 组合错误不传播内部异常(通过消息除外),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18516253/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com