I am working on a c# project which needs to work on Windows and Linux (Mono) which when it starts up it reads some settings from an xml config file. On windows this is working fine but on Linux it is going wrong. It throwing an exception saying that it has an invalid URI but this can’t be correct as it works fine on Windows.
I thought maybe it was due to the file getting corrupted in some way during the transfer so I removed the config file and retyped it manually but it is still coming up with the same error.
Below is the code that reads in the config file
public Dictionary<string, string> readConfig(string sectionName, bool soapService=false)
{
Dictionary<string, string> config = new Dictionary<string, string>();
try
{
XmlDocument configXml = new XmlDocument();
string configPath = "";
if (soapService)
{
string applicationPath = HttpContext.Current.Server.MapPath(null);
configPath = Path.Combine(applicationPath, "config.xml");
}
else
{
string applicationPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
configPath = Path.Combine(applicationPath, "config.xml");
}
configXml.Load(configPath);
XmlNodeList options = configXml.SelectNodes(string.Format("/options/{0}", sectionName));
XmlNodeList parameters = configXml.GetElementsByTagName("item");
foreach (XmlNode option in options)
{
foreach (XmlNode setting in option)
{
string key = setting.Attributes["key"].Value;
string value = setting.Attributes["value"].Value;
config.Add(key, value);
}
}
}
catch (KeyNotFoundException ex)
{
Console.WriteLine("Config KeyNotFoundException: {0}", ex.Message);
}
catch (XmlException ex)
{
Console.WriteLine("Config XmlException: {0}", ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("Config Exception: {0}", ex.Message);
}
return config;
}
The full exception is Config Exception: Invalid URI: The Authority/Host could not be parsed
Below is the config.xml
<?xml version="1.0" encoding="utf-8" ?>
<options>
<database>
<item key="server" value="localhost" />
<item key="database" value="emailserver" />
<item key="username" value="myusername" />
<item key="password" value="mypassword" />
<item key="port" value="3306" />
<item key="logFile" value="email_server.txt" />
</database>
<EmailServer>
<item key="ip_address" value="127.0.0.1" />
<item key="port" value="12345" />
</EmailServer>
<SmtpServer>
<item key="ip_address" value="127.0.0.1" />
<item key="port" value="25" />
</SmtpServer>
<SendMailSettings>
<item key="smtp_server" value="smtp.gmail.com" />
<item key="smtp_port" value="587" />
<item key="smtp_useSSL" value="true" />
<item key="smtp_username" value="myusername" />
<item key="smtp_password" value="mypassword" />
<item key="smtp_useAuthentication" value="true" />
</SendMailSettings>
</options>
I don’t understand why it is displaying this error.
Thanks for any help you can provide.
UPDATE
Below is the stack trace as requested
StackTrace: at System.Uri.Parse (UriKind kind, System.String
uriString) [0x00000] in :0 at System.Uri.ParseUri
(UriKind kind) [0x00000] in :0 at System.Uri..ctor
(System.String uriString, Boolean dontEscape) [0x00000] in :0 at System.Uri..ctor (System.String uriString) [0x00000]
in :0 at System.Xml.XmlResolver.ResolveUri
(System.Uri baseUri, System.String relativeUri) [0x00000] in :0 at System.Xml.XmlUrlResolver.ResolveUri (System.Uri
baseUri, System.String relativeUri) [0x00000] in :0
at Mono.Xml2.XmlTextReader..ctor (System.String url,
System.Xml.XmlNameTable nt) [0x00000] in :0 at
System.Xml.XmlTextReader..ctor (System.String url,
System.Xml.XmlNameTable nt) [0x00000] in :0 at
System.Xml.XmlDocument.Load (System.String filename) [0x00000] in
:0 at BoardiesITSolutions.Config.readConfig
(System.String sectionName, Boolean soapService) [0x00000] in
:0
I’ve found out the problem, I don’t understand why this is the problem I’m guessing it must be a bug in Mono as it works fine in Windows.
Even though the following code works fine in Windows for some reason in Mono it throws that exception even though it does get the correct path. If I remove that code and just change the configPath to be
"config.xml"then it works fine.