1
votes

I have written a simple WCF service that accepts and stores messages. It works fine when hosted locally. I still works when hosted on IIS 6. But when I enable the service's ability to store the messages to xml I get the following error: Access to c:\windows\system32\inetsrv\Onno.xml has been denied (translated from dutch, so may not match the true English error message). This is curious as the service does not run from the mentioned directory. Moreover, the file onno.xml is not present. The service should create it as

xelement.Save("onno.xml");

when

File.Exists("onno.xml")==false

What is wrong?

  • Do I have to specify a default directory for File IO operations inside IIS hosted WCF service?
  • Do I have to adjust permissions?

Edit: I tried to implement Mehrdad's solution with the MapPath function thus:

   public void Persist(Message message)
    {
        foreach (var recipient in message.Recipients)//recipient is a string
        {
            XElement xml_messages;
            string path;
            try
            {
                path = HttpContext.Current.Server.MapPath("~/"+recipient+FileExtension);
                //FileExtension=".xml"
                //Null reference exception thrown from this line
            }
            catch (Exception e)
            {
                throw new Exception("Trying to get path " + e.Message);
            }
            try
            {
                xml_messages = XElement.Load(path);
            }
            catch
            {
                xml_messages = XElement.Parse("<nothing/>");
            }
            var element = (XElement) message;
            if (xml_messages.IsEmpty)
            {
                xml_messages =
                    new XElement("messages",
                                 new XAttribute("recipient", recipient),
                                 element
                        );  
            }
            else
            {
                xml_messages.Add(element);
            }
            xml_messages.Save(path);
        }
    }

However I am greeted by a null reference exception?! The exception is generated in the MapPath line. Help?

1

1 Answers

5
votes

Yes. You should specify a full path for the file. You can use

System.Web.HttpContext.Current.Server.MapPath("~/onno.xml")

to get the full path of the file (if you want to write in the application directory).

Note that HttpContext.Current is unavailable to WCF services not running in ASP.NET compatibility mode. You can enable ASP.NET compatibility mode with

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

configuration option and [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] attribute. However, this will limit your WCF service to ASP.NET environment. This is not a good thing.

Alternatively, you could use a configuration setting for the base path and use

System.IO.Path.Combine(defaultPath, "onno.xml")

to get the full path. This solution is more flexible.

Also, NTFS write permission to that location should be granted to the user account under which the application pool is running.