关于Silverlight和棱镜

时间:2011-11-16 10:24:29

标签: silverlight prism

我正在使用silverlight5和prism4开发样本。当我运行我的应用程序时,我得到以下异常

“重写成员时违反了继承安全规则:'System.Exception.get_InnerException()'。重写方法的安全性可访问性必须与要覆盖的方法的安全性可访问性相匹配。”

并且堆栈跟踪如下

在System.ComponentModel.Composition.Hosting.ImportEngine.PartManager.TryOnComposed()    在System.ComponentModel.Composition.Hosting.ImportEngine.TrySatisfyImportsStateMachine(PartManager partManager,ComposablePart部分)    在System.ComponentModel.Composition.Hosting.ImportEngine.TrySatisfyImports(PartManager partManager,ComposablePart part,Boolean shouldTrackImports)    在System.ComponentModel.Composition.Hosting.ImportEngine.SatisfyImports(ComposablePart部分)    在System.ComponentModel.Composition.Hosting.ComposablePartExportProvider。<> c_ DisplayClass2.b _0()    在System.ComponentModel.Composition.Hosting.CompositionServices.TryInvoke(Action action)    在System.ComponentModel.Composition.Hosting.ComposablePartExportProvider.Compose(CompositionBatch批处理)    在System.ComponentModel.Composition.Hosting.CompositionContainer.Compose(CompositionBatch批处理)    在System.ComponentModel.Composition.AttributedModelServices.ComposeExportedValue [T](CompositionContainer容器,T exportedValue)    在Microsoft.Practices.Prism.MefExtensions.MefBootstrapper.RegisterBootstrapperProvidedTypes()    在Microsoft.Practices.Prism.MefExtensions.MefBootstrapper.ConfigureContainer()    在Microsoft.Practices.Prism.MefExtensions.MefBootstrapper.Run(Boolean runWithDefaultConfiguration)    在Microsoft.Practices.Prism.Bootstrapper.Run()    在Honeywell.CIU888.Shell.App.Application_Startup(Object sender,StartupEventArgs e)    在MS.Internal.CoreInvokeHandler.InvokeEventHandler(UInt32 typeIndex,委托handlerDelegate,Object sender,Object args)    在MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj,IntPtr unmanagedObjArgs,Int32 argsTypeIndex,Int32 actualArgsTypeIndex,String eventName,UInt32 flags)

任何人都有帮助。

以下是我的代码,

BootStrapper.cs

public class BootStrapper : MefBootstrapper
{

    protected override void InitializeShell()
    {
        base.InitializeShell();

        Application.Current.RootVisual = (UIElement)Shell;
    }

    protected override DependencyObject  CreateShell()
    {
        return Container.GetExportedValue<MainPage>();           
    }                    

    protected override void ConfigureAggregateCatalog()
    {            
        base.ConfigureAggregateCatalog();

        AggregateCatalog.Catalogs.Add(new AssemblyCatalog(this.GetType().Assembly));                    
    }

    protected override IModuleCatalog CreateModuleCatalog()
    {

    }
}

app.xaml.cs

公共部分类App:应用程序     {

    public App()
    {
        this.Startup += this.Application_Startup;
        this.Exit += this.Application_Exit;
        this.UnhandledException += this.Application_UnhandledException;

        InitializeComponent();
    }

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        //this.RootVisual = new MainPage();
        (new BootStrapper()).Run();
    }

    private void Application_Exit(object sender, EventArgs e)
    {

    }

    private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
    {
        // If the app is running outside of the debugger then report the exception using
        // the browser's exception mechanism. On IE this will display it a yellow alert 
        // icon in the status bar and Firefox will display a script error.
        if (!System.Diagnostics.Debugger.IsAttached)
        {

            // NOTE: This will allow the application to continue running after an exception has been thrown
            // but not handled. 
            // For production applications this error handling should be replaced with something that will 
            // report the error to the website and stop the application.
            e.Handled = true;
            Deployment.Current.Dispatcher.BeginInvoke(delegate { ReportErrorToDOM(e); });                
        }
    }

    private void ReportErrorToDOM(ApplicationUnhandledExceptionEventArgs e)
    {
        try
        {
            string errorMsg = e.ExceptionObject.Message + e.ExceptionObject.StackTrace;
            errorMsg = errorMsg.Replace('"', '\'').Replace("\r\n", @"\n");

            System.Windows.Browser.HtmlPage.Window.Eval("throw new Error(\"Unhandled Error in Silverlight Application " + errorMsg + "\");");
        }
        catch (Exception)
        {
        }
    }
}

如果我调用bootsrapper.run(),则会出现异常。

我也在“在浏览器中运行时提升信任”模式和签名的xap文件中运行我的项目。

1 个答案:

答案 0 :(得分:0)

"Security attributes need to be re-applied on types that derive from other types that also have security attributes"

基本上,如果您派生自应用了安全属性的类(例如[SecurityCritical]),则必须在任何覆盖成员/方法上重复这些属性。

如果您可以提供相关代码,那么解释起来会更容易,但您总是可以使用DotPeek来检查基类的代码(如果您没有源代码),因为它将显示属性。