locked
IsolatedStorageSettings throwing "Operation not permitted on IsolatedStorageFileStream" RRS feed

  • Question

  • The following line of code keeps throwing this exception in my application:


    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

    I've seen the same exception before when doing things like trying to open files which didnt exist in isolated storage etc, but I can't see how the above line of code would throw it.

    Any ideas?

    Thanks

    Wednesday, June 22, 2011 4:32 PM

Answers

  • Hi,

    As far as I know that the IsolatedStorageSettings.ApplicationSettings Property is used to get an instance of IsolatedStorageSettings that contains the contents of the application's IsolatedStorageFile, scoped at the application level, or creates a new instance of IsolatedStorageSettings if one does not exist. Since the declaration for  IsolatedStorageSettings.ApplicationSettings is contained in WriteToIsolatedStorage method which is called(modified the value of IsolatedStorageSettings.ApplicationSettings) by multiple threads concurrently, thus the error is encountered duo to any public static (Shared in Visual Basic) members of IsolatedStorageSettings are thread safe. Any instance members are not guaranteed to be thread safe.

    You can refer the link below for more information about IsolatedStorageSettings Class:

    http://msdn.microsoft.com/en-us/library/cc626492(v=VS.95).aspx

    The issue goes away if you move the IsolatedStorageSettings declaration outside of the method and up to class level.

    Actually ,for thread safe you can add a lock in WriteToIsolatedStorage method as below:

     public Page27()
            {
                InitializeComponent();
                Thread t1 = new Thread(new ThreadStart(DoWork));
                Thread t2 = new Thread(new ThreadStart(DoWork));
                t1.Start();
                t2.Start();
            }
            private void DoWork()
            {
                for (int i = 0; i < 22; i++)
                {
                    WriteToIsolatedStorage("test11", "test2");
                }
            }
    
            object flag = new object();
            private void WriteToIsolatedStorage(string key, string value)
            {
                lock (flag)
                {
                    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
                    if (!settings.Contains(key))
                    {
                        settings.Add(key, value);
                    }
                    else
                    {
                        settings[key] = value;
                    }
                }
              
            }

    Hope it can help you.

    Monday, August 1, 2011 11:19 PM

All replies

  • Is it possible you're running in some sort of private browsing mode?  Or maybe your Silverlight Configuration disallows isolated storage?

    Wednesday, June 22, 2011 4:34 PM
  • I should have mentioned that it works most of the time, it is just happening intermittently causing the application to crash

    Wednesday, June 22, 2011 4:42 PM
  • Hi,

    it is just happening intermittently causing the application to crash

    Since the issue is encountered intermittently  ,I would like to suggest you to use try - catch -retry block to retry that again when the exception is thrown.

    In addition ,I would recommend that please check the link below for a sample which describes how to use try and retry.

    C#: try and retry

    Hope it can help you.

    Sunday, June 26, 2011 11:27 PM
  • Hi,

    I eventually found this was being caused by multi threading. This issue can be easily reproduced with the following code:

     

     

           

    public MainPage()

            {

                InitializeComponent();

               

    Thread t1 = new Thread(new ThreadStart(DoWork));

               

    Thread t2 = new Thread(new ThreadStart(DoWork));

                t1.Start();

                t2.Start();

            }

           

    private void DoWork()

            {

               

    for (int i = 0; i < 9999; i++)

                {

                    WriteToIsolatedStorage(

    "test", "test");

                }                     

            }

           

    private void WriteToIsolatedStorage(string key, string value)

            {           


    var settings = IsolatedStorageSettings.ApplicationSettings;

    if (!settings.Contains(key))

                {

                    settings.Add(key, value);

                }

               

    else


                {

                    settings[key] = value;

                }

            }

     

    The issue goes away if you move the IsolatedStorageSettings declaration outside of the method and up to class level.

    Could this be a bug with the framework?

     

    Monday, August 1, 2011 10:36 AM
  • Hi,

    As far as I know that the IsolatedStorageSettings.ApplicationSettings Property is used to get an instance of IsolatedStorageSettings that contains the contents of the application's IsolatedStorageFile, scoped at the application level, or creates a new instance of IsolatedStorageSettings if one does not exist. Since the declaration for  IsolatedStorageSettings.ApplicationSettings is contained in WriteToIsolatedStorage method which is called(modified the value of IsolatedStorageSettings.ApplicationSettings) by multiple threads concurrently, thus the error is encountered duo to any public static (Shared in Visual Basic) members of IsolatedStorageSettings are thread safe. Any instance members are not guaranteed to be thread safe.

    You can refer the link below for more information about IsolatedStorageSettings Class:

    http://msdn.microsoft.com/en-us/library/cc626492(v=VS.95).aspx

    The issue goes away if you move the IsolatedStorageSettings declaration outside of the method and up to class level.

    Actually ,for thread safe you can add a lock in WriteToIsolatedStorage method as below:

     public Page27()
            {
                InitializeComponent();
                Thread t1 = new Thread(new ThreadStart(DoWork));
                Thread t2 = new Thread(new ThreadStart(DoWork));
                t1.Start();
                t2.Start();
            }
            private void DoWork()
            {
                for (int i = 0; i < 22; i++)
                {
                    WriteToIsolatedStorage("test11", "test2");
                }
            }
    
            object flag = new object();
            private void WriteToIsolatedStorage(string key, string value)
            {
                lock (flag)
                {
                    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
                    if (!settings.Contains(key))
                    {
                        settings.Add(key, value);
                    }
                    else
                    {
                        settings[key] = value;
                    }
                }
              
            }

    Hope it can help you.

    Monday, August 1, 2011 11:19 PM
  • Perfect I am much clearer on this now, thanks

    Thursday, November 3, 2011 9:09 AM