问题描述
我有一个由IIS中托管的WCF服务引用的程序集。程序集使用一些XSLT文件,我不知道在哪里转储这些文件,要么在程序集项目本身中或在WCF服务端中创建一个文件夹,如何在程序集中获取xslt文件的物理路径?
解决方法
由于由IIS托管的WCF服务仅倾向于将DLL复制到temp文件夹,而不倾向于将项目的内容复制到输出,因此需要引用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;
, 尝试使用AppDomain.CurrentDomain.RelativeSearchPath
, 将它们放在引用的程序集的子文件夹中,将它们标记为“内容”,然后启用“复制到输出目录”。
然后,在需要文件路径的程序集代码中,获取执行程序集的路径,并将所需的子文件夹添加到该路径,例如:
var dllPath = Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location);
var targetPath = Path.Combine(dllPath,\"XsltFolder\");
, 任何XSLT或XML文件都应相对于WCF服务文件夹的根文件夹放置,可以通过以下方式实现该根文件夹:
如果(HttpContext.Current!= null)
{
// \“〜/ \”给出该Biz dll支持的WCF服务虚拟目录的根物理文件夹,...
//例如:E:\\ PhyPath \\ WCFServiceFolder \\
RequestPhysicalPath = HttpContext.Current.Server.MapPath(\“〜/ \”);
}