Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 834
VJAI Avatar asked May 30 '11 12:05

VJAI


3 Answers

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;
like image 82
Maxim V. Pavlov Avatar answered Nov 15 '22 18:11

Maxim V. Pavlov


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");
like image 34
Jakob Möllås Avatar answered Nov 15 '22 17:11

Jakob Möllås


Try using AppDomain.CurrentDomain.RelativeSearchPath

like image 1
Shawn Avatar answered Nov 15 '22 19:11

Shawn