Hej marvinq,
Hvorfor ikke bruge en af de indbyggede måde at håndtere indstillinger i .net.
Her er et komplet kode eksempel:
Der er to dele, en ServiceLibrary assembly og så en simpel konsol applikation, som bruger service librariet.
ServiceLibrary
- MyService.cs
- Configuration.cs
Konsol
- Program.cs
- App.config
//Her ServiceLibrary koden
using System.Configuration;
namespace ServiceLibrary
{
public class MyService
{
public string Server { get; private set; }
public int Port { get; private set; }
public string Path { get; private set; }
public MyService()
{
Server = Configuration.GetConfiguration().Server;
Port = Configuration.GetConfiguration().Port;
Path = Configuration.GetConfiguration().Path;
}
}
}
//Her er selve custom konfigurations koden
using System.Configuration;
namespace ServiceLibrary
{
public class Configuration : ConfigurationSection
{
public static Configuration GetConfiguration()
{
Configuration configuration =
ConfigurationManager
.GetSection("MyServiceSettings")
as Configuration;
if (configuration != null)
return configuration;
else
{
//Here you could throw an exception explaining how the user should include the configuration needed to use this service.
}
return new Configuration();
}
[ConfigurationProperty("server", IsRequired = true)]
public string Server
{
get
{
return this["server"] as string;
}
}
[ConfigurationProperty("port", IsRequired = true)]
public int Port
{
get
{
return (int)this["port"];
}
}
[ConfigurationProperty("path", IsRequired = true)]
public string Path
{
get
{
return this["path"] as string;
}
}
}
}
//Her kommer så programmet som skal bruge servicen
namespace Console1
{
class Program
{
static void Main(string[] args)
{
MyService service = new MyService();
Console.WriteLine(string.Format("
http://{0}:{1}/{2}/Services",
service.Server, service.Port, service.Path));
Console.ReadKey();
}
}
}
//Og her her selve konfig filen.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="MyServiceSettings" type="ServiceLibrary.Configuration, ServiceLibrary"/>
</configSections>
<MyServiceSettings server="server1" port="8888" path="MyService" />
</configuration>
Om der er brug for mere forklaring til de enkelte dele, skal du endelig spørge.
/Hallur