Search code examples
c#.netrecursionstoragemscorlib

C# infinite recursion during resource lookup


I have a problem with this code:

if (_updater.IsNewVersionAvailable())
{
    _isolatedStorageFile.CreateDirectory("Folder");
    _isolatedStorageFile.CreateDirectory("Folder2");

    foreach (string file in Directory.GetFiles(_sharedFilesFolder + "\\Folder"))
    {
        string fileName = Path.GetFileName(file);
        //_isolatedStorageFile.CreateFile(fileName); // <- same problem

        using (var outputStream = _isolatedStorageFile.OpenFile("Folder/" + fileName, FileMode.Create, FileAccess.Write)) // <- here is the problem (I tried with backslash (\\) and also doesnt work.
        {
            using (var inputStream = File.OpenRead(file))
            {
                inputStream.CopyTo(outputStream);
            }
        }
    }
}

When I run the MS Test which called this piece of code I get this error:

error1

error2

The folders inside isolated storage are created normally ( I cannot create a file) The strangest thing is that once when I started the test the file has been created - it was 1/20 runs.

Any idea?


Solution

  • One thing you can try is, insert this in your code right before you're getting the infinite recursion issue (from here and here):

    try
    {
        throw new NotImplementedException();
    }
    catch (NotImplementedException ex)
    {
    }
    

    I was just trying to figure out an issue where we intended to retrieve something from isolated storage, and it got stuck in this inf recursion. I was browsing the .Net and MSTest sources, and it seems that:

    1. The file doesn't exist. Its trying to throw a FileNotFoundException from FileStream.Init > WinIOError.
    2. To throw the exception, it's trying to get a string with Environment.GetResourceString("IO.FileNotFound_FileName", str), str). From there you get to functions like InternalGetSatelliteAssembly. Its trying to locate mscorlib.
    3. Meanwhile, MSTest has defined an AssemblyResolver listener, which get's called at this point. It will iterate over some paths, doing a File.Exists check on them.
    4. File.Exist will check for permissions to that file. For code access permissions, it'll throw a SecurityException with the parameter: Environment.GetResourceString("Security_Generic").
    5. Loop back to point 2.
    6. (IsolatedStorage never gets to catch the FileNotFoundException, so it won't create a new one.)

    The NotImplementedException or AgrumentException seems to force mscorlib to be loaded, and the loop is avoided. Maybe there's a another way of making it easier to find, though.