1

Having some problems trying to get HostingEnvironment.MapPath() to work with my WCF app.

I created a class with a static method to check to see if HttpContext.Current is null:

public class ServerPath
{
    public static string MapPath(string path)
    {
        string result;
        if (HttpContext.Current != null)
            result = HttpContext.Current.Server.MapPath(path);
        result = HostingEnvironment.MapPath(path);

        return result;
    }
}

and everything I through at it just returns null (ServerPath.MapPath(~/file.xml") and Server.PathPath("./file.xml")). If anyone is wondering why I have 'string result'; it is because I added if (string.IsNullOrEmpty(result)) and added result = Directory.GetCurrentDirectory() + path;

Has anyone else experienced issues like this when testing with the WCF test client?

Do let me know if it is something to do with the binding/need to see an example of it.

Before I forget, I have <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> within the system.serviceModel in my app.config as well.

StuartLC
  • 104,537
  • 17
  • 209
  • 285
Matt
  • 2,691
  • 3
  • 22
  • 36
  • Possible duplicate of [HttpContext.Current.Server null](http://stackoverflow.com/questions/6304532/httpcontext-current-server-null) – Jeff B Apr 05 '17 at 12:37
  • Web vs Wcf? I don't think this is a duplicate @JeffBridgman – Matt Apr 06 '17 at 14:28

2 Answers2

1

Duplicate: HttpContext.Current.Server null

Use the answer from that quesiton.

SOURCE: https://stackoverflow.com/a/6304591/1449777

HttpContext.Current is returning null because your Windows Service is not running under the umbrella of IIS or some other web server provder.

However, you can find the executing path of your service using reflection:

System.Reflection.Assembly.GetExecutingAssembly().Location ^ should return the path of the executing service..

Community
  • 1
  • 1
Tom
  • 1,330
  • 11
  • 21
  • Problem is, System.Reflection.Assembly.GetExecutingAssembly().Location returns the path of the dll. I think I am just going to have to add this as a key in my app.config file :( – Matt Jul 04 '12 at 08:07
0

If you make the XML file an embedded resource, you can then replace the name of the assembly with the name of the XML file. Don't forget to set Copy Always.

stu
  • 1