问题描述
限时送ChatGPT账号..在尝试让 HostingEnvironment.MapPath()
与我的 WCF 应用程序一起工作时遇到一些问题.
Having some problems trying to get HostingEnvironment.MapPath()
to work with my WCF app.
我用静态方法创建了一个类来检查 HttpContext.Current
是否为空:
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;
}
}
以及我通过它所做的一切都只返回 null (ServerPath.MapPath(~/file.xml")
和 Server.PathPath("./file.xml")
). 如果有人想知道为什么我有字符串结果";那是因为我添加了 if (string.IsNullOrEmpty(result))
并添加了 result = Directory.GetCurrentDirectory() + path;
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;
是否有其他人在使用 WCF 测试客户端进行测试时遇到过这样的问题?
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.
在我忘记之前,我的 app.config
中的 system.serviceModel
中有 <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
> 也是.
Before I forget, I have <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
within the system.serviceModel
in my app.config
as well.
推荐答案
重复:HttpContext.Current.Server null
使用那个问题的答案.
来源:https://stackoverflow/a/6304591/1449777
HttpContext.Current 返回 null,因为您的 Windows 服务不在 IIS 或其他 Web 服务器提供商的保护下运行.
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^ 应该返回执行服务的路径..
System.Reflection.Assembly.GetExecutingAssembly().Location ^ should return the path of the executing service..
这篇关于HostingEnvironment.MapPath(path) 在 WCF 应用程序中返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论