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.



image



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.



image





Enjoy!

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>