[英]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.