在WCF服务程序集中获取文件路径

5

我有一个程序集,被托管在IIS中的WCF服务中。该程序集使用了一些XSLT文件,我不确定应该将这些文件放在程序集项目自身中创建的文件夹中,还是放在WCF服务端,并且我该如何在程序集中获取xslt文件的物理路径?


有没有最终的解决方案?我有类似的问题。http://stackoverflow.com/questions/22691426/get-svchost-assembly-location-path-using-svcimplementation-assembly - Kiquenet
4个回答

3

由于托管在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;

我有 Service.Host.dll 和 SvcImpl.dll (<%@ ServiceHost Service="SvcImpl" %>) 在 IIS 中的 Service Host。 SvcImpl.dll 中的代码: 1)Assembly.GetEntryAssembly() 返回 null。 2)Assembly.GetExecutingAssembly() 返回 SvcImpl。 3)Assembly.GetCallingAssembly(); 返回 System.ServiceModel。如果 Assembly.GetEntryAssembly() 返回 null,如何在 SvcImpl 中以编程方式获取 Service.Host 程序集?查看详细信息 http://stackoverflow.com/questions/22691426/get-svchost-assembly-location-path-using-svcimplementation-assembly - Kiquenet

1
将它们放在被引用程序集的子文件夹中,将它们标记为内容并启用复制到输出目录
然后,在需要文件路径的程序集代码中,获取执行程序集的路径并将预期的子文件夹添加到路径中,例如:
var dllPath = Path.GetDirectoryName(  
    System.Reflection.Assembly.GetExecutingAssembly().Location);
var targetPath = Path.Combine(dllPath, "XsltFolder");

2
我按照您说的做了,但是出现了以下错误:“找不到路径的一部分'C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d5e86da5\28c7474e\assembly\dl3\e3edeed7\4bb2927d_561fcc01\AV.BackOffice.Caliber.DLL\XSLTs\Violations.xslt'。GetExecutingAssembly().Location没有返回预期的路径,但XSLTs文件夹已复制到调试目录中。” - VJAI

1

尝试使用AppDomain.CurrentDomain.RelativeSearchPath。


0

任何XSLT或XML文件都应该相对于WCF服务文件夹的根目录放置,可以通过以下方式获得根目录:

if (HttpContext.Current != null) { // "~/" 会给出WCF服务虚拟目录的根物理文件夹,例如:E:\PhyPath\WCFServiceFolder\ RequestPhysicalPath = HttpContext.Current.Server.MapPath("~/"); }


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接