views:

7399

answers:

5

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).

[Updated] 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.

+2  A: 

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.

pmarflee
+6  A: 

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>
Mikko Rantanen
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.
Stefy
Updated answer with tested sample code.
Mikko Rantanen
use this which works with both ASPNET context or without System.Web.Hosting.HostingEnvironment.MapPath("~/folder/file");
Simon_Weaver
+1  A: 

Thanks for you responses.

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

Stefy
+22  A: 

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.

ongle
This solution worked best for me - I'd recommend using it rather than having to make all kinds of other settings changes...
Sam Schutte
Works very well. Thanks.
WOPR
Great simple answer; the best here even if it wasn't accepted.
Kirk Broadhurst
+1  A: 

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");

Osa