简体   繁体   English

如何从未与虚拟目录在同一服务器上运行的WCF服务访问虚拟目录?

[英]How do I access a virtual directory from a WCF service that is not running on the same server as the virtual directory?

I have a WCF service called "SQLInterfaceLibrary" that is running on a machine via WcfSvcHost.exe. 我有一个名为“SQLInterfaceLibrary”的WCF服务,它通过WcfSvcHost.exe在一台机器上运行。 I can call all of its endpoints using WcfTestClient.exe by connecting to the URI " http://localhost:8733/Design_Time_Addresses/SQLInterfaceLibrary/mex ". 我可以通过连接到URI“ http:// localhost:8733 / Design_Time_Addresses / SQLInterfaceLibrary / mex ”使用WcfTestClient.exe调用其所有端点。 In fact, I can even connect to it directly from a C# application by using the "References.cs" file that I generate by calling SvcUtil.exe. 实际上,我甚至可以使用我通过调用SvcUtil.exe生成的“References.cs”文件直接从C#应用程序连接到它。 However, the requirements of my project state that I must access an IIS virtual directory from this service. 但是,我的项目要求声明我必须从此服务访问IIS虚拟目录。 I found another StackOverflow post that suggests using the "HttpContext.Current.Server.MapPath" function. 我找到了另一个建议使用“HttpContext.Current.Server.MapPath”函数的StackOverflow帖子。 However, when I call this from within my service, I get the following error: 但是,当我从我的服务中调用它时,我收到以下错误:

"System.NullReferenceException:'Object reference not set to an instance of an object.'" “System.NullReferenceException:'对象引用未设置为对象的实例。'”

I think this is happening, because I am trying to access a virtual directory on a different machine. 我认为这种情况正在发生,因为我试图访问另一台机器上的虚拟目录。 Can anyone with experience in WCF programming vouch for this? 任何有WCF编程经验的人都可以为此担保吗? You don't need to provide a detailed explanation, just yes or no. 您无需提供详细说明,只是是或否。 Thanks! 谢谢!

By default, wcf's httpcontext is null, you should set aspNetCompatibilityEnabled to true in your web.config . 默认情况下,wcf的httpcontext为null,您应该在web.config中将aspNetCompatibilityEnabled设置为true。

   <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
  multipleSiteBindingsEnabled="true" />

If httpcontext is still null, you could add AspNetCompatibilityRequirements attribute to your service 如果httpcontext仍为null,则可以向服务添加AspNetCompatibilityRequirements属性

[AspNetCompatibilityRequirements(RequirementsMode =AspNetCompatibilityRequirementsMode.Required)]
public class CalculatorService : ICalculatorService
{
    public double Add(double a, double b)
    {
      HttpContext context=  HttpContext.Current;
      string dir =  Directory.GetCurrentDirectory();
        return a + b;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM