2012-07-03 45 views
1

试图让HostingEnvironment.MapPath()与我的WCF应用程序一起工作时遇到一些问题。在WCF应用程序中返回null的HostingEnvironment.MapPath(路径)

我创建了一个类的静态方法来检查,看看是否HttpContext.Current为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; 
    } 
} 

,一切都通过我在它只是返回null(ServerPath.MapPath(~/file.xml")Server.PathPath("./file.xml"))。如果有人想知道为什么我有'字符串结果';这是因为我添加了if (string.IsNullOrEmpty(result))并添加了result = Directory.GetCurrentDirectory() + path;

当使用WCF测试客户端进行测试时,是否有其他人遇到类似问题?

请让我知道它是否与绑定有关/需要查看它的一个示例。

在我忘记之前,我的<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>也在我的app.configsystem.serviceModel之内。

+0

可能重复[HttpContext.Current.Server null](http://stackoverflow.com/questions/6304532/httpcontext-current-server-null) –

+0

Web vs Wcf?我不认为这是重复的@JeffBridgman – Matt

回答

1

重复:HttpContext.Current.Server null

使用从quesiton答案。

SOURCE:因为你的Windows服务是不是IIS的雨伞或一些其他的web服务器provder下运行https://stackoverflow.com/a/6304591/1449777

HttpContext.Current是返回null。 。

System.Reflection.Assembly.GetExecutingAssembly()的位置 ^应该返回执行服务的路径..

+0

问题是,System.Reflection.Assembly.GetExecutingAssembly()。Location返回dll的路径。我想我只是将不得不添加此作为我的app.config文件中的一个关键:( – Matt

0

如果:

然而,你可以使用反射找到你的服务的执行路径您将XML文件作为嵌入式资源,然后可以使用XML文件的名称替换程序集的名称。不要忘记设置复制总是。

相关问题