A
A
Anton Martsen2013-12-13 21:01:25
IIS
Anton Martsen, 2013-12-13 21:01:25

How to solve a problem with writing a file to an ASP.NET (IIS) application folder?

Hello.
Faced the following problem:
files stopped being written to the local folder of the application hosted on IIS.
Application architecture:
ASP.NET MVC 3 application, it has several WCF services. One of them just provides the functionality of downloading a file. The clint of this service is the silverlight application. It is loaded through it. The method inside the client asynchronously pulls the web service and gives it batches of data that should be written.
I added a new web service to the application, but after deploying the new version, this functionality stopped working. Reverting to an older version didn't help.
Locally everything is tested and works (both new and old versions).
With the help of logging, we managed to find out what the following piece of code does not execute:

void SendFile()
{
  // какой-то код, действия которого можно отследить

  //Режимы открытия надо будет уточнить, но они не менялись уже много лет
  using (FileStream stream = File.Open(filename, FileMode.Append, FileAccess.Write, FileShare.Read))
  {
    // внуть using не передается управление, ничего нельзя отловить
    try
    {
      //
    }
    catch (Exception ex)
    {
      //
    }
  }
}

Falls, probably, on creation of a flow. Write permission suspect.
IIS 7, pool on .NET 4 is spinning.
In IIS logs, it gives an error 500. So far, it is impossible to get more details due to the peculiarities of the system.
Can you suggest where to dig? We looked at the rights to IIS, everything looks OK.
I can add information later.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Martsen, 2013-12-16
@martsen

Rewrote the code so that you can catch the error.
Threw : System.IO.DirectoryNotFoundException: Could not find a part of
the
path -not-fin...
support.microsoft.com/kb/827421
Solution:
Manually create a folder where the file will be written.

string dir = Path.GetDirectoryName(filePath);

if (!Directory.Exists(dir))
  Directory.CreateDirectory(dir);

FileStream stream = null;
try {
  stream = File.Open(filename, FileMode.Append, FileAccess.Write, FileShare.Read);
  ....
}
  catch(Exception ... ) {
  ...
}
finally {
  if(stream != null)
  stream.Dispose()
}

I
Ilya Glebov, 2013-12-14
@IljaGlebov

You can try to deploy using and get an exception

FileStream stream = null;
try {
  stream = File.Open(filename, FileMode.Append, FileAccess.Write, FileShare.Read);
 ....
}
catch(Exception ... ) {
   ...
}
finally {
  if(stream != null)
     stream.Dispose()
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question