3

I have a Web application. It sends a series of images to the server (no problem there) and then uses code from this tutorial to create a PowerPoint presentation. The presentation is saved in a directory on the web server and the URL is returned to the user.

However, the file is still in use and attempting to access it generates a 500.0 error in IIS 7.5.

If I open the task manager and kill the w3wp.exe process that belongs to the NETWORK SERVICE user everything works as intended (the file can be downloaded and viewed). I also have another w3wp process belonging to DefaultAppPool, but it doesn't seem to cause a problem.

I'm new to this .Net coding, so it is very possible I forgot to close something down in code. Any ideas?

Edit: This is the method that creates a series of png's from image data that is encoded into a string. It uses a Guid to create a unique bit of a directory path, and checks to make sure it doesn't exist and then creates the directory and places the images there.

It looks like the offending method is this one:

So the offending method is this one:

    public void createImages(List<String> imageStrings)
    {
        UTF8Encoding encoding = new UTF8Encoding();
        Decoder decoder = encoding.GetDecoder();

        Guid id = Guid.NewGuid();
        String idString = id.ToString().Substring(0, 8);

        while (Directory.Exists(imageStorageRoot + idString))
        {
            id = Guid.NewGuid();
            idString = id.ToString().Substring(0, 8);
        }

        String imageDirectoryPath = imageStorageRoot + idString + "\\";
        DirectoryInfo imagePathInfo = Directory.CreateDirectory(imageDirectoryPath);

        for (int i = 0; i < imageStrings.Count; i++)
        {
            String imageString = imageStrings[i];
            Byte[] binary = Convert.FromBase64String(imageString);

            using (FileStream stream = new FileStream(imageDirectoryPath + idString + i.ToString() + ".png", FileMode.Create))
            {
                using (BinaryWriter writer = new BinaryWriter(stream))
                {
                    writer.Write(binary);
                }
            }
        }
    }

Edit 2: If there is a better to about doing things please let me know. I am trying to learn here!

Edit 3: So upon further examination, I can comment out all this code. In fact, the second instance of w3wp.exe starts up as soon as a browser hits the website. I am now wondering if this might have something else to do with our stack? Its a Flex app, that uses WebOrb for remoting to some C# classes.

Does anyone know why this second open instance of w3wp.exe (owned by NETWORK SERVICE) would prevent the file from opening properly? Is there some way to get it release it's hold on the file in question?

Fred
  • 31
  • 1
  • 3
  • 1
    Chances are that your code does not close the file properly, therefore not releasing the lock. Please post the code for writing the file. – Oded Dec 14 '11 at 19:29

2 Answers2

3

Make sure you've closed the file after using it (better yet put the code that accesses your files in using statements). Post some code if you need help figuring out where the issue is.

M.Babcock
  • 18,753
  • 6
  • 54
  • 84
  • I posted some code. If you could have a look I would appreciate it. – Fred Dec 14 '11 at 23:11
  • Actually the code looks pretty decent. Are you sure this is where the error is occurring? Are the files uploaded individually or simultaneously? What event do you perform just before the error occurs? – M.Babcock Dec 14 '11 at 23:22
  • Hmmmm I've been experiment further. If I comment out all code in that method, the second instance of w3wp.exe (owned by NETWORK SERVICE) still starts and doesn't end. – Fred Dec 14 '11 at 23:40
  • Well that eliminates the code you posted as the culprit (unless it also has the problem). Are you able to debug your solution? – M.Babcock Dec 14 '11 at 23:43
  • I can attach to w3wp.exe and step through the code, yes. Do you have any ideas? I am out of my element here. – Fred Dec 14 '11 at 23:49
  • When you see it happening: Attach to w3wp, pause the solution and review your application threads. Figure out which one is running code that has the file handle open and then walk through it to determine why it is hanging. – M.Babcock Dec 14 '11 at 23:55
  • Ok, I've attached and placed a break point at the start of the above method. If I open the threads tab I don't see much. There are 11 threads. Do you perchance have a link to an example of this type of debugging? It's all new to me. – Fred Dec 15 '11 at 02:09
  • I don't but I can walk you through it (10 years of experience doing so in various environments) – M.Babcock Dec 15 '11 at 02:11
  • Each thread contains the name of the current method they are currently executing (unless they are executing a framework specific method). Do you have any threads in wait state? Based on your issue I would look for a method waiting on an IO operation. – M.Babcock Dec 15 '11 at 02:14
0

The sample looks good. Did you deviate from the code structure?

 using (Package pptPackage = 
    Package.Open(fileName, FileMode.Open, FileAccess.ReadWrite))
  {
       // your code goes inside there
  }

If your code does contain the using statement you should be fine. If not add a Dispose call to your Package object when you are done or you will leave the file open for a long time until (possibly) the finalizer will kill it.

Alois Kraus
  • 13,229
  • 1
  • 38
  • 64
  • No I left the sample's code intact. I think it is my poor code that is causing the issue. I posted the offending bit of code. – Fred Dec 14 '11 at 23:11