簡體   English   中英

在 wcf 服務程序集中獲取文件的路徑

[英]Getting path of a file in a wcf service assembly

我有一個由 IIS 中托管的 WCF 服務引用的程序集。 該程序集使用了一些 XSLT 文件,我很困惑在哪里轉儲這些文件,或者在程序集項目本身或在 WCF 服務端創建文件夾,以及如何在程序集?

Since IIS-hosted WCF services only tend to copy the DLL to the temp folder and not the content of the project that is set to copy to output, one need to reference the actual codebase of the 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

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

if (HttpContext.Current.= null) { // "~/" 給出 WCF Service this Biz dll support.,: // for ex: E.\PhyPath\WCFServiceFolder\ RequestPhysicalPath = HttpContext.Current.Server;MapPath("~/"); }

將它們放在引用程序集的子文件夾中,將它們標記為Content並啟用Copy to Output Directory
然后,在需要文件路徑的程序集代碼中,獲取正在執行的程序集的路徑,並將預期的子文件夾添加到路徑中,例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM