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

问题描述:

我有一个由IIS中托管的WCF服务引用的程序集。程序集使用一些XSLT文件,我很困惑在哪里转储这些文件,或者在程序集项目本身或WCF服务端创建一个文件夹,以及如何获取程序集中xslt文件的物理路径?在wcf服务程序集中获取文件的路径

+0

关于它的任何最终解决方案?我有类似的问题http://*.com/questions/22691426/get-svchost-assembly-location-path-using-svcimplementation-assembly – Kiquenet 2014-03-28 08:40:41

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

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()。Loaction没有返回我认为的预期路径,而是将XSLTs文件夹复制到调试目录。 – VJAI 2011-05-31 05:56:41

因为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; 
+0

我有Service.Host.dll和SvcImpl.dll() IIS中的服务主机。 SvcImpl.dll中的此代码: 1)Assembly.GetEntryAssembly()获取null。 2)Assembly.GetExecutingAssembly()获取SvcImpl。 3)Assembly.GetCallingAssembly();获取System.ServiceModel。 如何在SvcImpl中以编程方式获取Service.Host程序集,如果Assembly.GetEntryAssembly()获取null ?.查看详细信息http://*.com/questions/22691426/get-svchost-assembly-location-path-using-svcimplementation-assembly – Kiquenet 2014-04-01 06:54:11

尝试使用AppDomain.CurrentDomain.RelativeSearchPath

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

如果( HttpContext.Current!= null) //“〜/”给出WCF服务的虚拟目录的根物理文件夹,这个Biz DLL支持..., //例如:E:\ PhyPath \ WCFServiceFolder \ RequestPhysicalPath = HttpContext.Current.Server。在MapPath( “〜/”); }