8

I have an IIS-hosted WCF service and I am trying to use Directory.Exists() method. If nonexistent network location is passed, this method hangs. I've googled for it and found that it's "kind of ok" due to Directory.Exists() inner implementation. But I wrote a simple console application which does the same and Directory.Exists() never hangs, always returns 'false'. I run the application under my (admin) account and IIS pool is running under 'Network Service'.
Do you have any ideas why? What is the difference between doing the same inside a service or a console application?

Qué Padre
  • 1,805
  • 2
  • 20
  • 35
  • How do you detect the hang? And in which TrustLevel does your IIS run? – TGlatzer Jan 24 '14 at 07:36
  • @Grumbler85 I can see that using a debugger and logging (which actually stops on the point of the method call). I run service on IIS 7.5, I didn't change Trust Level settings. – Qué Padre Jan 24 '14 at 07:50
  • You could try running the Application Pool for you service in IIS temporarily as local system - if it works fine under local system you would know it is permission related. – Sebastian K Jan 26 '14 at 14:40
  • What is the actual path? Is it an unc path or is the network location mapped to a drive letter? – rene Jan 26 '14 at 21:04
  • 1
    How are you sure that this method is definitely causing the service to hang? The documentation for this method indicates that it doesn't throw any exceptions, so I'd be very surprised if this is the culprit. Have you turned on WCF logging, as this may help you pin-point the problem. – Ben Smith Jan 26 '14 at 23:02
  • @Fresh Well, as I said earlier, I can see that service hangs through debugger and my custom logging stops at this point as well. I found an explanation here http://stackoverflow.com/questions/8434958/using-directory-exists-on-a-network-folder-when-the-network-is-down, but the question is all about why the behavior is different for console application and a service running under IIS. – Qué Padre Jan 27 '14 at 05:15
  • @rene actual path is UNC. – Qué Padre Jan 28 '14 at 05:18
  • Can you implement the native call on [`GetFileAttributesEx`](http://www.pinvoke.net/default.aspx/kernel32.getfileattributesex) and [`GetLastError`](http://www.pinvoke.net/default.aspx/user32/GetLastError.html) and let us know what the errornumber outcome is. – rene Jan 28 '14 at 09:36
  • 1
    I don't think the important point is "service vs console app", but rather "Network Service vs admin account". Network Service has restricted permissions by design, and probably can't access the network share. You could try using a different identity for your AppPool; one which has permission to access the network. – Joe Jan 29 '14 at 12:00

4 Answers4

2

This might be caused due to user authorization control stuff that is present in Windows (remember when you try to access a folder that you dont have access in Windows? It kind of waits a long time to answer "You can't access,..." or something like that).

Which is the user you are running the application? Your user id (as per what you said you can access that folder running the console application) is able to see that folder.

So, try to run the application as your own user id.

Furthermore, check this link: https://stackoverflow.com/a/21385162/1378854

Community
  • 1
  • 1
1

Are you sure the application is running as Network Service? A local Network Service account won't have access to the network.

Jason Geiger
  • 1,478
  • 16
  • 26
1

Use the UNC path instead of a mapped network drive, as the mapped drive is specific to your user account. Also, as KMan pointed out, make sure the application pool identity has access to the UNC path destination.

please visit this link:

http://msdn.microsoft.com/en-us/library/ff649309.aspx How to get working path of a wcf application?

thanks.

Community
  • 1
  • 1
Sumon Banerjee
  • 1,705
  • 4
  • 23
  • 39
1

The key here is really about running on Network Service vs. the admin account.

You know the later can acceess the network location, but you are not really sure if the earlier can.

Here is one example of a case where you could be expecting there is access, but it doesn't really work: https://serverfault.com/a/177150

The fact that the machine with the shared drive is not on a domain is where your main problem is

Community
  • 1
  • 1
eglasius
  • 34,909
  • 4
  • 58
  • 105