WCF - reading an xml file in a service using XElement.Load - wcf

i have a simple xml file in a wcf service that i am trying to load using Xelement.Load("sample.xml") which is not reading the file. What's the right way of doing this?
The service is supposed to return an xml to an asp.net application.
TIA

I got it to work by providing the ABSOLUTE path as the parameter to the XElement.Load() method, RELATIVE path would be better though.

You should try something like this then.
var appPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
bodyFile = Path.Combine(appPath, #"<File Name Path");
This will work relative to your application's physical path.

Related

How to read file in webRoot dir from EJB3?

I have an EmailSrvc Stateless Session Bean (EJB3), deployed on Glassfish. The bean needs to read an email template stored in /emailTemplates/someTemplate.html, process the template and send the email. The questions:
1) is this web dir suggested above an appropriate location for keeping the email templates?
2) how do I reference the file? I'd like to avoid direct filesystem paths as the server may be clustered. I was thinking I could get the webRootfolder via the ServletContext, but I haven't figured out how to look it up from within the bean.
Thanks
I just figured I can put the emailTemplates folder underneath WEB-INF/classes and get an inputStream of the file I need like this :
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("emailTemplates/someTemplate.html");
Not sure I like having the templates in the classes folder however, it doesn't seem appropriate. Hopefully somebody else can propose a better solution?

Reading Xml from an absolute path

I need to access a remote Xml document from a WCF service. Right now I have:
XmlReader reader = XmlReader.Create("path");
But since the Xml doc is elsewhere on our network I need to give the XmlReader an absolute path, as opposed to having it look deeper in the project folder. How do I do this? I've found surprisingly little information about this. It seems like this should be a simple thing to do. Any help is appreciated!
Thanks
You can use overload that accepts Stream parameters as follows:
using (FileStream fileStream = new FileStream(#"\\computername\shared path"))
using (XmlReader reader = XmlReader.Create(fileStream))
{
// perform your custom code with XmlReader
}
Please note that you need appropriate permission to open remote stream. In WCF service context you may need to use impersonation.

How to Create proxy class from WSDL File in .net

I have to use WSDL file to create proxy class.
For this purpose, I have created one simple service and got the WSDL for the same, and saved into my disk by using Save as... in Internet Explorer. (I hope I'm doing right here).
With the above procedure, I got WSDL file.
If I use the following command:
D:\Ashok>wsdl CalculatorService.wsdl
I am getting some error like shown below:
Error: Unable to import binding 'BasicHttpBinding_ICalculatorService'
from names pace 'http://tempuri.org/'.
- Unable to import operation 'Add'.
- The element 'http://tempuri.org/:Add' is missing.
I have googled for the same and understood that I need to do some configuration settings, but I couldn't understand what exactly I need to do.
Can anybody please suggest me!
Add a Service Reference and point it at the WSDL
edit
As you cant use add Service Reference...
http://support.microsoft.com/kb/820122
end edit
If you look closely at the WSDL see how it has something like this inside it:
<s:import schemaLocation="http://..." />
This means that this WSDL is not a standalone file but it depends on some other files (this is how WCF decided to expose the WSDL). So either save all other files on disk (and change the schemaLocation to reflect their current location) which is hard since you need to do it many times. Or get a flat version of the wsdl. Or use "add service reference" in VS.

Load App.Config at runtime

I am creating a test project using IBM's Rational Functional Tester(RFT) tool(we are using the VB version of it and it only supports till .net 3.5).In this project I am making all database queries using the entity framework.The problem is entity framework retrieves "Metadata", "Connection String" etc from the App.Config file,and RFT won't let me add a App.Config to the project(I guess it is designed that way -- I googled adding an App.Config file to an rft project and came up with nothing), and the Entity Framework requires you to have the app.config file at the entry point.I was building the string to pass to entity framework in code, but my boss really does not like that.
So looking at my options I think any of the below 2 solutions should suffice(or if you have a better solution please advise).
Is their any way to load an App.Config during run-time.
Is their any way to add an App.Config to an RFT project(should
really be my first question).
If you guys could help me with this it will be great.
Thanks in advance.
After a lot of research i found you can load the config file at runtime by using,
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Assembly.GetExecutingAssembly().ManifestModule.Name + ".config");
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var configValue = config.ConnectionStrings.ConnectionStrings["refrenceNameinConfigfile"].ConnectionString;
just make sure your App.config is within the folder where your exe file is running

VB check inside a folder to see if there are files

I am trying to get my vb.net application to look inside a folder on the web server and then let me know whether there are files in there or if the folder is empty...Would anybody know where i would begin? Thanks
Use the DirectoryInfo.EnumerateFiles() method.
Dim myDir as DirectoryInfo = new DirectoryInfo(pathToDir)
If (myDir.EnumerateFiles().Any())) Then
' Got files in direcotry!
End If
If you are also interested in finding out if there are directories within this one, there is also DirectoryInfo.EnumerateDirectories().
I would suggest you have a look at Directory.GetFiles
If your program is running on the web server, you can simply check whether Directory.GetFileSystemEntries(path) returns anything.
If your program is running on a client, you'll need to make a server-side script that calls Directory.GetFileSystemEntries and returns a value to the client.