70

I want to get the working folder of a WCF application. How can I get it?

If I try

HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath)

I get a null reference exception (the Http.Current object is null).


What I meant with the working folder was the folder where my WCF service is running. If I set aspNetCompatibilityEnabled="true", I get this error:

The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error.

7 Answers 7

173

I needed the same information for my IIS6 hosted WCF application and I found that this worked for me:

string apPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;

As always, YMMV.

6
  • 2
    This solution worked best for me - I'd recommend using it rather than having to make all kinds of other settings changes... Commented Jun 3, 2009 at 19:23
  • I don't have System.Web.Hosting , I really don't know the reason why . . . Commented Dec 14, 2010 at 17:16
  • Thanks ongle :) I'm success with using AppDomain :) Commented Dec 16, 2010 at 2:29
  • 2
    @nXqd: You need to ensure that you've not got the Client Profile version of the framework selected. Commented Jan 30, 2011 at 18:13
  • This answer just saved my day. Please make it the accepted one, since using ASP.NET Compatibility is not always preferred. This one works regardless of ASP.NET Compatibility being on or off.
    – Roy
    Commented May 14, 2011 at 10:42
32

Please see ongle's answer below. It is much better than this one.

Updated after more information

The following worked for me. I tested it with a new WCF Service I hosted on IIS through a Service1.svc.

  1. Add <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> to web config. <system.serviceModel>..</ ..> existed already.
  2. Add AspNetCompatibilityRequirementsAttribute to the service with Mode Allowed.
  3. Use HttpContext.Current.Server.MapPath("."); to get the root directory.

Below is the full code for the service class. I made no changes in the IService1 interface.

[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
    public void DoWork()
    {
        HttpContext.Current.Server.MapPath(".");
    }
}

And below is an excerpt from the web.config.

<system.serviceModel>
    <!-- Added only the one line below -->
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

    <!-- Everything else was left intact -->
    <behaviors>
        <!-- ... -->
    </behaviors>
    <services>
        <!-- ... -->
    </services>
</system.serviceModel>

Old answer

What do you mean by the Working Folder? WCF services can be hosted in several different ways and with different endpoints so working folder is slightly ambiguous.

You can retrieve the normal "Working folder" with a call to Directory.GetCurrentDirectory().

HttpContext is an ASP.Net object. Even if WCF can be hosted on IIS, it's still not ASP.Net and for that reason most of the ASP.Net techniques do not work by default. OperationContext is the WCF's equivalent of HttpContext. The OperationContext contains information on the incoming request, outgoing response among other things.

Though the easiest way might be to run the service in ASP.Net compatibility mode by toggling it in the web.config. This should give you access to the ASP.Net HttpContext. It will limit you to the *HttpBindings and IIS hosting though. To toggle the compatibility mode, add the following to the web.config.

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
3
  • What I meant with the "Working folder" was the physical path where my WCF service was running. I have a xml file there and I want to read it. Directory.GetCurrentDirectory() is not working, and when I try the setting you said about compatibility mode I get this error: The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error. Commented Apr 26, 2009 at 20:11
  • 9
    use this which works with both ASPNET context or without System.Web.Hosting.HostingEnvironment.MapPath("~/folder/file"); Commented Apr 2, 2010 at 5:09
  • The AspNetCompatibilityMode won't work if you have to access HttpContext.Current in a method which is called via multithreading.
    – Anchit
    Commented Feb 16, 2012 at 10:42
21

Depending on what you want. I usually want to resolve a url like "~/folder/file". This is what worked.

System.Web.Hosting.HostingEnvironment.MapPath("~/folder/file");
0
19

More general, I am using this one

AppDomain.CurrentDomain.BaseDirectory
1
  • +one for a solution without System.Web
    – Filip
    Commented Sep 28, 2015 at 19:37
11

The aspNetCompatibilityEnabled="true" should have resolved my problem, but I got this error:

The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error.

I resolved my problem with getting the physical path of my running WCF service by getting it from my current app domain:

AppDomain.CurrentDomain.BaseDirectory
1
  • 1
    If you run ServiceHost manually then only AppDomain.CurrentDomain.BaseDirectory points to correct source. Thx man !
    – mastak
    Commented Dec 2, 2011 at 16:43
3

In order to reference ASP.NET features like the HttpContext object, you need to run your WCF app in ASP.NET compatibility mode. This article explains how to do this.

2

Use HostingEnvironment.ApplicationPhysicalPath in WCF to find your application physical path. Use namespace using System.Web.Hosting;

Not the answer you're looking for? Browse other questions tagged or ask your own question.