試圖讓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.config
的system.serviceModel
之內。
可能重複[HttpContext.Current.Server null](http://stackoverflow.com/questions/6304532/httpcontext-current-server-null) –
Web vs Wcf?我不認爲這是重複的@JeffBridgman – Matt