Server.Map alternative in WCF – HostingEnvironment.MapPath

  • Home
  • Blog
  • Server.Map alternative in WCF – HostingEnvironment.MapPath

If you are ASP.NET programmer then you already know about Server.MapPath. It is used to map a physical location on webserver for asp.net.  You can find more information about Server.MapPath from the following location.

http://msdn.microsoft.com/en-us/library/ms524632(v=vs.90).aspx

You can still use that in WCF(Windows Communication Foundation) service http bindings. As we know WCF Service can use other non http bindings like TCP/IP,MSMQ and Named Pipe. At that time HttpContext of WCF will be none so server.mappath will not be available as current context is not available. Because its a child class of HttpContext.Current.

HostingEnvironment.MapPath works on all kind of bindings. So when you have other bindings like TCP/IP and MSMQ it will be available as it is inherited from the System.Web.Hosting namespace. You can find more information from below MSDN link.

http://msdn.microsoft.com/en-IN/library/system.web.hosting.hostingenvironment.mappath.aspx

You can use HostingEnvironment.MapPath like following.

HostingEnvironment.MapPath(path);

That’s it. Hope you like it. Stay tuned for more..