How to read an xml file from a WCF service? - wcf

I want to read a xml file from a WCF service.
The xml file, settings.xml is in the same folder as the service exe and the service is hosted as a Windows service.
when the service is accessed by the client the service exe is looking at system32 folder and throws file not found exception
C:\Windows\system32\Settings.xml
Here's the code which is in a try catch block and a FaultException is thrown which the client catches.
XmlSerializer serializer = new XmlSerializer(_appSettings.GetType());
StreamReader sr = new StreamReader("Settings.xml", Encoding.UTF8);
_appSettings = (SpecialityFinishingSettings)serializer.Deserialize(sr);
sr.Close();
Basically I am trying to read the xml and deserialize it into an object that can be used withing the service.
What do I do to read this file from within the service?
The serivce is installed using InstallUtil.exe

The problem is that "Settings.xml" isn't found as a file. You need to specify a full path. You can either:
Use an absolute path
Store a path in a config file. This path will be a folder that you will open files from, this can then be appended to the file name.
Get the path the app is running from as suggested by Anand (System.AppDomain.CurrentDomain.BaseDirectory + "Settings.xml";)

Related

Current Working directory for WCF Application

I have an VB6 com object which i need to wrap it in .net and expose as WCF web service.
The com .dll has some configuration files which needs to be placed in it current directory.
For testing the com object, i have created a console application and executed, from the logs generated by the component i can see that it is pointing to current directory and getting the config files and working as expected.
Now when i host this in WCF service, Com object stoped working. I wondered and gave below command in my wcf application
Logger.Log("current path: " + System.IO.Directory.GetCurrentDirectory());
Now in my log i see that current path is "c:\winnt\system32" !!!!!! And concluded that this might be the reason why the component is not working as expected in wcf.
Note: I have published my wcf into another folder in "d:" drive.
My Question :
What is the reason behind "c:\winnt\system32"?
Thanks for your help!
[Update]
Now it gets wierd, I have changed current directory with below method.
System.IO.Directory.SetCurrentDirectory()
Now the dll points to below path
C:\WINNT\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\service\b5fc8f06\28f29236\assembly\dl3\1945abc0\00563626_8b5bce01\

Can AzureReader2 read Azure storage configuration settings from the service config rather than the web.config?

Is there any way for the AzureReader2 plugin to read its connection string and endpoint config values from the service config file rather than just the web.config?
The problem is that we build Azure package files (.cspkg) and web.config files are embedded within the package. Therefore we strive to keep our web.config files constant across all different deployments (test, dev, and production). We normally deploy using a package file and a service config file.
Install AzureReader 2 via code during application startup instead, and you can pass it the connection string directly:
var nvc = new NameValueCollection();
nvc["endpoint"] = "http://127.0.0.1:10000/devstoreaccount1/";
nvc["connectionString"] = "UseDevelopmentStorage=true";
new AzureReader2Plugin(nvc).Install(Config.Current);

How to use ClassPathResource class in WSO2 Axis2 service?

I putted a text file in java class path on WSO2 Axis2 service project and uploaded it to WSO2 AS server , but my Axis2 service can not find it by ClassPathResource class. The Exception as the following:
IOException parsing XML document from class path resource [xxx.xml]
I am sure my xxx.xml file has been uploaded in the .aar package on AS server:
service.aar
...[META-INF]
...[package dir]
...xxx.xml
How to use ClassPathResource class to find a file resource under WSO2 Axis2 service??
Assuming you have the "xxx.xml" file at the top level of the hierarchy of the service archive, you can use the following java snippet to access it within the Axis2 service classes.
........
InputStream is = getClass().getResourceAsStream("/xxx.xml");
.......
Hope this helps!
Cheers,
Prabath

Zero dependency from the client configuration files for WCF services

Our aim is to have Zero dependency from the client configuration files for WCF services.
we are using ConfigurationChannelFactory() to create the channel and specify the ConfigSettings.
ConfigSettings is loaded using the following code
ConfigurationManager.OpenExeConfiguration(ConfigFilePath);
So we have to provide the ConfigFilePath here.
we have both windows and web clients.
we have used below approaches to find out the path
AppDomain.CurrentDomain.BaseDirectory + "bin\\" + executingAssembly.GetName().Name + ".dll"
Web client : AppDomain.CurrentDomain.BaseDirectory gives root folder of the
web applicaton so its works fine
Windows client : AppDomain.CurrentDomain.BaseDirectory gives path upto Debug/Release folder
so, its throws error
Assembly.GetExecutingAssembly().Location
Web client : Assembly.GetExecutingAssembly().Location gives path to the ASP.Net temp.
files , where we dont have the config files. So its throws an error.
Windows client : Assembly.GetExecutingAssembly().Location gives the correct location,
so its works fine.
In order to make it works in web clients, we have to add the following key in client's web.config files.
<hostingenvironment shadowcopybinassemblies="false">
but it adds a dependency to the clients.
Can you guys please help me to find the path without worrying about the clients?
have you tried this? I used GetCallAssembly() instead of GetExecutingAssembly() as this lives in a utility class in our project.
public static string AssemblyDirectory
{
get{
string codeBase = assembly.GetCallingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
Could you just probe both paths? In other words, check the Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin") folder and if you can't find the config file in there, check the Assembly.GetExecutingAssembly().Location folder instead? Since you indicate that the first approach works for web but not Windows clients, while the second approach works for Windows clients but not web, go with both!

How to Create WCF proxy class given wsdl file

I have wsdl file with me.But i don't have access to the file where it is deployed.
I need to generate proxy class using the WSDl file. I am getting error while using svcutil.ece to generate proxy file
Error: Cannot import wsdl:port Detail:
Cannot find definition for
http://www.cii.com/Soa/Foundation:Basi
cHttp. Service Description with
namespace
htt://www.cii.com/Soa/Foundat ion is
missing. Parameter name: name XPath to
Error Source:
//wsdl:definitions[#targetNamespace='http://www.cii.com/Soa/Foundation/MessageDefinition.xsd']/wsdl:service[#name='CareerServicesS
ervice']/wsdl:port[#name='BasicHttp']
Thanks,
Shodhan
If you are using svcutil with a locally downloaded WSDL file, you need to also download all related XSD files and pass them on the svcutil command line as well. A great tool for automatically downloading all related XSD files is disco.exe in the Windows / .NET SDK (and there may be a svcutil mode that does it as well, don't remember for sure).