C# HostingEnvironment.MapPath(路径)在WCF应用程序中返回null

C# HostingEnvironment.MapPath(路径)在WCF应用程序中返回null,c#,wcf,C#,Wcf,尝试获取HostingEnvironment.MapPath()以使用我的WCF应用程序时遇到一些问题 我使用静态方法创建了一个类,以检查HttpContext.Current是否为null: public class ServerPath { public static string MapPath(string path) { string result; if (HttpContext.Current != null)

尝试获取
HostingEnvironment.MapPath()
以使用我的WCF应用程序时遇到一些问题

我使用静态方法创建了一个类,以检查
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测试客户端进行测试时,是否有其他人遇到过类似的问题

请务必让我知道这是否与绑定有关/需要查看一个示例

在我忘记之前,我在我的
app.config
中的
system.serviceModel
中也有

重复:

用那个问题的答案

资料来源:

HttpContext.Current返回null,因为您的Windows服务未在IIS或其他web服务器验证程序的保护下运行

但是,您可以使用反射找到服务的执行路径:

System.Reflection.Assembly.GetExecutionGassembly()位置 ^应返回正在执行的服务的路径。

重复:

用那个问题的答案

资料来源:

HttpContext.Current返回null,因为您的Windows服务未在IIS或其他web服务器验证程序的保护下运行

但是,您可以使用反射找到服务的执行路径:

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

如果将XML文件设为嵌入式资源,则可以用XML文件的名称替换程序集的名称。不要忘记设置“始终复制”。

如果将XML文件设为嵌入式资源,则可以用XML文件的名称替换程序集的名称。是否执行不要忘记设置“始终复制”。

问题是,System.Reflection.Assembly.GetExecutionGassembly()。Location返回dll的路径。我想我必须将其作为键添加到app.config文件中:(问题是,System.Reflection.Assembly.GetExecutionGassembly()).Location返回dll的路径。我想我只需要在app.config文件中添加一个键:(可能是Web与Wcf的重复?我不认为这是Web与Wcf的重复?可能是Web与Wcf的重复?我不认为这是@JeffBridgman的重复