Gotcha with HostingEnvironment.MapPath()

When developing WCF service the System.Web.Hosting.HostingEnvironment class is kind of useful as it will give you all sorts of information about the runtime environment of where you app is hosted.

string path = HostingEnvironment.MapPath(@"\MyFile.txt");

Now the problem is it only works when the service is hosted inside IIS, or the Cassini development server that comes with VS2008, not when the service is self hosted. Guess that makes sense but it is kind of easy to start with a WCF Service Library project template, even when you finally intend to host the service under IIS.


The problem is that the development service, WcfSvcHost.exe, acts as if it is running as a self hosting app and does not let you use the HostingEnvironment members.

So I guess it is better to always start with a WCF Service Application which acts more like an IIS hosted application.



One thought on “Gotcha with HostingEnvironment.MapPath()

  1. I am creating a window service and facing the same problem. Can you please some insight , how can i solve the problem in window service.

Leave a Reply

Your email address will not be published. Required fields are marked *