1

I am trying to get the path of my WCF service folder hosted on my web server using C# code. I am using below logic to get the path:

 string codeBase = Assembly.GetExecutingAssembly().CodeBase;
 UriBuilder uri = new UriBuilder(codeBase);
 string path1 = Uri.UnescapeDataString(uri.Path);
 string path2 = Path.GetDirectoryName(path1);
 path2 = path2.Substring(0, path2.LastIndexOf("\\"));

When I run this code on my local machine, it gives me correct path starting from the drive letter like "D:\appdir\servicehost\". However, when I run it on my web server, it does not work as expected because the IIS virtual directory on my web server is pointing to a shared location pointing to some other machine. In this case, the initial IP address part is omitted and it directly starts from the shared drive name, like

"\SharedFolder\servicehost\"

Instead, I am expecting the code to return the whole path. When the service is hosted on a local drive, it should give me "D:....." and when it is hosted on a shared drive then it should give me the path including the IP address like "\\10.44.22.11\SharedFolder\servicehost"

This is causing a file load logic to fail on my web server having the same code as my local machine because it does not find the file located at a wrong location which excludes the IP address.

Hence, I decided to use the "URI" string which contains the whole path of the dll file starting from "file://..". So I can cut the "file:" part and the dll name part of the string and get the whole path. But this does not seem to be the right way and I am sure there will be the more sophisticated way to get it worked in both cases.

Is there any common way of coding this, which I can use in both these scenarios to get the full path?

Anil Soman
  • 2,443
  • 7
  • 40
  • 64
  • Have you tried adding the file as an Embedded Resource and then using other .Net APIs to load that resource instead of getting/mapping to physical directories? – JayV Mar 28 '18 at 10:00
  • That is one option available, however I need to provide file path to a third party service as a parameter. That service has no other option but accept two parameters, one file type = "flat file" and second "file path" – Anil Soman Mar 28 '18 at 10:20
  • Take a look at this question: https://stackoverflow.com/questions/10384894/how-to-mappath-with-requestcontext-in-wcf-service and the HostingEnvironment.MapPath method (msdn: https://msdn.microsoft.com/en-us/library/system.web.hosting.hostingenvironment.mappath.aspx) – JayV Mar 28 '18 at 10:45
  • @JayV: HostingEnvironment gives me an invalid path C:\inetpub\wwwroot\SharedFolder\.... In this path, SharedFolder does not exist under wwwroot folder.. it is infact the name of shared folder on other IP – Anil Soman Mar 28 '18 at 10:59
  • what is the value of the `Physical Path` property under IIS config for your site/virtual directory? – JayV Mar 28 '18 at 11:10
  • It is a path pointing to shared drive. The shared drive is located on other PC. The path looks like "//10.44.xx.xx//shared_drive_name/website/" – Anil Soman Mar 28 '18 at 11:36

1 Answers1

0

I created a new IIS App Pool as per the image: enter image description here Important thing for me was to set the NetworkService as the identity account otherwise I wasn't getting access to the network.

Then I created a share on another server to hold my TestWcfService and pointed the IIS Application at it:

enter image description here

In addition, the ACLs on the file system are set to EVERYONE, FULL CONTROL for ease of testing.

The service has this line:

String result = String.Format("HostingEnvironment.MapPath: {0}", HostingEnvironment.MapPath("~\\MyFile.txt"));

Resulting in this output in my testing client:

HostingEnvironment.MapPath: \\Company\Shares\1 - JayV\TestWcfService\MyFile.txt

To access the file internally within the Wcf Service, you use the return value of HostingEnvironment.MapPath as above.

To access the file remotely, from a client talking to your web service use: http://localhost/Remote/myfile.txt

My Virtual Application has no other virtual sub-directories, the Wcf Service is run from this Virtual Application and not from the root web site

JayV
  • 3,238
  • 2
  • 9
  • 14
  • The path of txt file starts from "\\Company\..". Using this path, are you able to read the file content? or are you able to check if the file exists or not using code? In my case, unless I prefix the path with IP address of shared folder source, it is not able to recongnise the file – Anil Soman Mar 29 '18 at 03:48
  • Yes, I can do everything with the file. Check it exists and read its contents. `HostingEnvironment.MapPath` returns a full UNC path that you can use to access the file from any Windows application – JayV Mar 29 '18 at 07:57