2012-07-03 43 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文件的名稱替換程序集的名稱。不要忘記設置複製總是。

相關問題