正在获取wcf服务程序集中文件的路径

本文关键字:文件 路径 集中 服务程序 获取 wcf | 更新日期: 2023-09-27 17:58:03

我有一个由IIS中托管的WCF服务引用的程序集。程序集使用了一些XSLT文件,我不知道在程序集项目本身或WCF服务端创建文件夹时,将这些文件转储到哪里,以及如何在程序集中获取XSLT文件的物理路径?

正在获取wcf服务程序集中文件的路径

由于IIS托管的WCF服务只倾向于将DLL复制到临时文件夹,而不是将设置为复制到输出的项目的内容,因此需要引用DLL的实际代码库。

var codeBase = System.Reflection.Assembly.GetCallingAssembly().CodeBase;
                    codeBase = codeBase.Substring(8); //Remove the "file:///" prefix in front of the actual path.
var dir = codeBase.Substring(0, codeBase.LastIndexOf("/", System.StringComparison.Ordinal) + 1); //Cut out the dll file name.
var file = @"ErrorMessages.xml";
var targetPath = dir + file;

将它们放在引用程序集的子文件夹中,标记为内容并启用复制到输出目录
然后,在需要文件路径的程序集代码中,获取正在执行的程序集的路径,并将预期的子文件夹添加到该路径中,例如:

var dllPath = Path.GetDirectoryName(  
    System.Reflection.Assembly.GetExecutingAssembly().Location);
var targetPath = Path.Combine(dllPath, "XsltFolder");

尝试使用AppDomain.CurrentDomain.RelativeSearchPath

任何XSLT或XML文件都应该相对于WCF服务文件夹的根文件夹放置,根文件夹可以实现如下:

if(HttpContext.Current!=null){//"~/"为WCF服务的虚拟目录的根物理文件夹提供此Biz dll支持。。。,//例如:E:''PhyPath''WCFServiceFolder''RequestPhysicalPath=HttpContext.Current.Server.MapPath("~/");}