简体   繁体   English

如何从由ServiceHost托管的IIS Web服务访问文件

[英]How to access a file from IIS web service hosted by ServiceHost

In my IIS application I open a file located in the wwwroot directory that way: 在我的IIS应用程序中,以这种方式打开位于wwwroot目录中的文件:

File.ReadAllText("ConfigFile.json");

IISExpress tries to open the file in C:\\Program Files (x86)\\IIS Express\\ConfigFile.json IISExpress尝试打开C:\\Program Files (x86)\\IIS Express\\ConfigFile.json

I thought the wwwroot directory was the working directory but apparently it's not the case. 我以为wwwroot目录是工作目录,但显然并非如此。

Log4net log files are written relatively to the working directory, and configuration manager files also. Log4net日志文件相对于工作目录写入,配置文件也相对写入。 So I don't understand why opening a file with System.IO.File I have C:\\Program Files (x86)\\IIS Express as the working directory. 因此,我不明白为什么要使用System.IO.File打开文件,而将C:\\Program Files (x86)\\IIS Express作为工作目录。

What's the best solution for that problem ? 解决该问题的最佳方法是什么? I suppose I don't have to touch the Current defined working directory. 我想我不必触摸当前定义的工作目录。

You will need to inject IHostingEnvironment into your class to have access to the ApplicationBasePath property value 您需要将IHostingEnvironment注入您的类中才能访问ApplicationBasePath属性值

Suppose IHostingEnvironment type is env then you can use 假设IHostingEnvironment类型为env则可以使用

File.ReadAllText(env.WebRootPath + "/ConfigFile.json");

like you have a function named Read then you can use 就像您有一个名为Read的函数,则可以使用

public void Read(IHostingEnvironment env)
{
   File.ReadAllText(env.WebRootPath + "/ConfigFile.json");
}

Ok, the solution that works in a IIS WCF Service, hosted by a ServiceHost class is: 好的,在ServiceHost类托管的IIS WCF服务中可以使用的解决方案是:

AppDomain.CurrentDomain.BaseDirectory + "/ConfigFile.json"

It gives the full absolute path that is valid in my IIS Express environment and in the deployed IIS environment. 它提供了在我的IIS Express环境和已部署的IIS环境中有效的完整绝对路径。

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

相关问题 如何从服务“内部”的代码访问 web.config 以获取托管在 IIS 中的 WCF 服务 - How to access the web.config for a WCF service hosted in IIS from the code “inside” the service 如何在IIS下托管服务时为WCF ServiceHost事件创建监听器? - How to Create a Listener for WCF ServiceHost events when service is hosted under IIS? 如何从客户端应用程序访问Windows服务上托管的Web服务? - How to access a Web Service hosted on a Windows Service from a client application? 如何仅为特定用户授予对IIS上托管的WCF Web服务的访问权限? - How to give access to WCF web service hosted on IIS only for specific users? 从Linux Web应用程序中使用IIS托管的WCF服务 - Consuming IIS-hosted WCF Service from Linux Web Application 如何使用Delphi 2007中的非IIS托管,WCF,C#Web服务? - How to consume non-IIS hosted, WCF, C# web service from Delphi 2007? 无法使用soapclient从php访问iis上托管的wcf服务 - Cannot access wcf service hosted on iis from php with soapclient 从IIS中托管的WCF服务访问文件系统 - Accessing the file system from a WCF service hosted in IIS 如何从 IIS7 中托管的另一个 WCF 服务评估托管 WCF 服务的 Windows 服务? - How to assess a Windows Service hosted WCF service from another WCF service hosted in IIS7? 如何将文件上传到IIS中托管的.NET 3.5 WCF服务? - How to upload a file to a .NET 3.5 WCF service hosted in IIS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM