【问题标题】:How to MapPath with RequestContext in WCF Service如何在 WCF 服务中使用 RequestContext 映射路径
【发布时间】:2012-05-10 05:22:47
【问题描述】:

这个MSDN article 说:

HttpContext: Current is always null when accessed from within a WCF service. Use RequestContext instead.

我正在尝试从我的 IIS 托管 WCF 服务加载一些 XSD 文件。问题是,我不知道如何像在任何旧的香草 ASP.NET 网站中那样执行 Server.MapPath(),例如:

HttpContext.Current.Server.MapPath(schemaUri);

在 IIS 托管的 WCF 服务中使用 RequestContext 的等效方法是什么?

架构位于服务应用程序根目录的“架构”目录中。使用 web.config 中的自定义配置部分引用它们,如下所示:

<schemas>
    <add uri="~/Schemas/foo.xsd" xmlNamespace="http://foo.bar/types" />
</schemas>

我试图像这样加载:

var schemaUri = HttpContext.Current.Server.MapPath(schema.Uri);

这在普通的 ASP.NET 网站中运行良好,而不是在 IIS 托管的 WCF 服务中。

【问题讨论】:

    标签: c# asp.net .net wcf xsd


    【解决方案1】:

    您可以使用HostingEnvironment.MapPath

    【讨论】:

    • 哇!甜的!这甚至适用于 UnitTest 项目!非常感谢!
    • 如果您点击链接,您会发现 HostingEnvironment 类的命名空间是 System.Web.Hosting
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多