views:

161

answers:

2

I'm developing an application using C# in .Net Framework 3.5. In the application I create several objects from different dll (also developed in C#) using reflection. All this objects extend an abstract class (AApplication):

    private AApplication BuildApplication(string path)
    {
        Assembly appAssembly = Assembly.LoadFrom(path);
        Type[] typeArray = appAssembly.GetTypes();
        foreach (Type t in typeArray)
        {
            if (typeof(AApplication).IsAssignableFrom(t))
            {
                ConstructorInfo ci = t.GetConstructor(new Type[0]);
                AApplication app = ci.Invoke(null) as AApplication;
                return app;
            }
        }
        return null;
    }

The abstract class AApplication is in another library (named AppsLibrary), which also has images as resources. This class has a simple method to load an image:

    protected BitmapImage GetImage(string path)
    {
        BitmapImage bmp = new BitmapImage();
        bmp.BeginInit();
        bmp.UriSource = new Uri(path, UriKind.RelativeOrAbsolute);
        bmp.CacheOption = BitmapCacheOption.OnLoad;
        bmp.CreateOptions = BitmapCreateOptions.None;
        bmp.EndInit();
        return bmp;
    }

The problem: when I call this method (GetImage) from a class derived from AApplication that is in another dll an exception occurs:

'System.Security.Permissions.MediaPermission, WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) en System.Security.CodeAccessPermission.Demand() en MS.Internal.PresentationCore.SecurityHelper.DemandMediaPermission(MediaPermissionAudio audioPermissionToDemand, MediaPermissionVideo videoPermissionToDemand, MediaPermissionImage imagePermissionToDemand) en System.Windows.Media.Imaging.BitmapDecoder.DemandIfImageBlocked() en System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache) en System.Windows.Media.Imaging.BitmapImage.FinalizeCreation() en System.Windows.Media.Imaging.BitmapImage.EndInit() en AppsLibrary.AApplication.GetImage(String path)

The strange thing is that the exception occurs only on some computers. Any idea?

A: 

The error is related to full / partial trust. Can you supply some more info. such as where you are loading files from?

Mitch Wheat
The simplified structure of the application is:Windows.xaml (one project) UserControl.xaml (other project) : AAplication (another project)The method BuildApplication is in the project of Windows.xaml, the method GetImage is in AAplication class, and it's used in UserControl class.
Matias
A: 

See here http://social.msdn.microsoft.com/forums/en-US/wpf/thread/a8a6e332-4fc1-47ff-9263-5169aca16f57/, for some more info about trust across app domains.

Preet Sangha
I read all the information about Add-in, but I think that these solution is too big for this problem. Additionally these doesn't explain why the application works in some computers and not in others.
Matias