2011-05-30 75 views
5

我有一個由IIS中託管的WCF服務引用的程序集。程序集使用一些XSLT文件,我很困惑在哪裏轉儲這些文件,或者在程序集項目本身或WCF服務端創建一個文件夾,以及如何獲取程序集中xslt文件的物理路徑?在wcf服務程序集中獲取文件的路徑

+0

關於它的任何最終解決方案?我有類似的問題http://stackoverflow.com/questions/22691426/get-svchost-assembly-location-path-using-svcimplementation-assembly – Kiquenet 2014-03-28 08:40:41

回答

0

將它們放在引用程序集的子文件夾中,將它們標記爲內容並啓用複製到輸出目錄
然後,在你需要的文件路徑彙編代碼,獲得執行的程序集的路徑,並添加預期的子文件夾的路徑,例如:

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

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

我有Service.Host.dll和SvcImpl.dll(<%@ ServiceHost服務=「SvcImpl」%>) IIS中的服務主機。 SvcImpl.dll中的此代碼: 1)Assembly.GetEntryAssembly()獲取null。 2)Assembly.GetExecutingAssembly()獲取SvcImpl。 3)Assembly.GetCallingAssembly();獲取System.ServiceModel。 如何在SvcImpl中以編程方式獲取Service.Host程序集,如果Assembly.GetEntryAssembly()獲取null ?.查看詳細信息http://stackoverflow.com/questions/22691426/get-svchost-assembly-location-path-using-svcimplementation-assembly – Kiquenet 2014-04-01 06:54:11

1

嘗試使用AppDomain.CurrentDomain.RelativeSearchPath

0

任何XSLT,或XML文件應該被相對於WCF服務文件夾的根文件夾放置,根文件夾,可以實現如下:

如果( HttpContext.Current!= null) //「〜/」給出WCF服務的虛擬目錄的根物理文件夾,這個Biz DLL支持..., //例如:E:\ PhyPath \ WCFServiceFolder \ RequestPhysicalPath = HttpContext.Current.Server。在MapPath( 「〜/」); }