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?