views:

468

answers:

2

Right now I am working on a stub of a project. In the course of the project I need to be able to from a web front end set a message on a server to and then from an iPhone Query the Server to read the message.

While all the individual peices are working and my request is going through fine I am having trouble using this webmethod

[WebMethod()]
public void setMessage(string message)
{
    FileStream file = new FileStream("mymessage.txt", FileMode.OpenOrCreate, FileAccess.Write);
    StreamWriter sw = new StreamWriter(file);
    sw.Write(message);
    sw.Close();
    file.Close();
}

When I invoke this via HTTP Post using SOAP from an iPhone app.

I get the following xml in my console when I make the request.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>Server was unable to process request. ---&gt; Access to the path 'c:\windows\system32\inetsrv\myMessage.txt' is denied.</faultstring>
<detail />
</soap:Fault>
</soap:Body>
</soap:Envelope>

The Server is one configured by my boss, that is currently being used in other capacities as an in house test server for several different projects.

The server is an IIS server I do not know what version, with ASP.NET configured and installed.

The script is located in a different place than the program is trying to write. I assume that the script is being run from that directory and that is why it is trying to write there. Is that the case or am I missing something fundamental?

If there are any alternative suggestions as to how I could go about this I would love to hear them as well, as I am working of a pretty small knowledge base.

Thanks!

+3  A: 

Change "filename.txt" to

Server.MapPath("filename.txt")

or specify the full physical path of the file and grant NTFS permissions to the ASP.NET user to be able to access the folder.

Server.MapPath converts virtual paths (e.g. ~/helloworld.aspx) to physical paths (e.g. D:\WebSite\helloworld.aspx).

Mehrdad Afshari
+1  A: 

In addition to storing the file in a folder you have access to (were you unable to read the message?), you need to properly implement "using" blocks:

using (FileStream file = new FileStream("mymessage.txt", 
                    FileMode.OpenOrCreate, FileAccess.Write)) {
    using (StreamWriter sw = new StreamWriter(file)) {
        sw.Write(message);
    }
}


The reason it's writing to c:\windows\system32\inetsrv\ is that this is its default directory. If you want to write to a different directory, then you have tp specify it. Server.MapPath will do that, as has already been pointed out to you.

John Saunders