views:

800

answers:

3

Hi,

WCF service has one method ( Let's say TestMethod) in which I try to create a File Stream like this :

            System.IO.FileStream fs = new System.IO.FileStream(@"D:\Test.xml", System.IO.FileMode.Open);

My Client and Service is on the same solution.

When the Client makes a call to TestMethod ( Exposed in Web service ) it will give this error:

Access to the path 'D:\DXDirectoryAuth.xml' is denied.

Please Help!!

A: 

Security!

The reason being you are trying to access a file location outside of the directory where you have hosted your WCF service...

You are either going to have to grant the account the WCF runs under permissions to that directory or move the file into the directory\sub-directory where you are hosting the WCF service.

Ollie

AWC
Thanks Ollie.. can you please guide me how can I achieve this??My Service folder is "MyService", I have put the xml file in MyService folder.. but still it is giving this error
Ashish Ashu
+2  A: 

Okay, if you have put the file in the directory or a sub-directory of your WCF service you should be able to access the file without any permissions issues.

The question is how are you attempting to access the file?

You should probably get the current directory of the service then append the relative file location onto the current directory and then attempt to open the file something like this:

var appPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
bodyFile = Path.Combine(appPath, @"templates\email.txt");

var body = File.OpenText(bodyFile).ReadToEnd();

HTH

Ollie

AWC
A: 

Have you tried:

System.IO.FileStream fs = new System.IO.FileStream(@"D:\Test.xml", System.IO.FileMode.Open, System.IO.FileAccess.Read);

The default constructor of FileStream() asks for read and write access.

Mr. Smith