开发者

Getting path of a file in a wcf service assembly

I've an assembly that is referenced by a WCF service hosted in IIS. The assembly uses some XSLT files and I'm confused where to dump these files either creating a开发者_如何学编程 folder in assembly project itself or in the WCF service side and how I can get the physical path of the xslt file in the assembly?


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;


Put them in a sub-folder of the referenced assembly, mark them as Content and enable Copy to Output Directory.
Then, in the assembly code where you need the path to the file, get the path of the executing assembly and add expected sub-folder to the path, for example:

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


Try using AppDomain.CurrentDomain.RelativeSearchPath


Any XSLT, or XML files should be placed relative to root folder of WCF Service folder, root folder can be achieved as below:

if (HttpContext.Current != null) { // "~/" gives root physical folder of the virtual directory of the WCF Service this Biz dll supporting..., // for ex: E:\PhyPath\WCFServiceFolder\ RequestPhysicalPath = HttpContext.Current.Server.MapPath("~/"); }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜