如何在运行时WCF中在app.config中编写端点信息?

时间:2022-09-01 08:46:14

Is there a way to write in a app.config file the information about endpoints? Actually I want to read the <service> tag from one app.config file and want to write it in other app.config file's <Services> tag.

有没有办法在app.config文件中写入有关端点的信息?实际上我想从一个app.config文件中读取 标签,并希望将其写入其他app.config文件的 标签中。

Please anyone tell me how can I do it?

请有人告诉我怎么办?

Actually I have a config file called "WCFService.exe.config" which i want to read in my program , so what i am writing is:

实际上我有一个名为“WCFService.exe.config”的配置文件,我想在我的程序中阅读,所以我写的是:

string path = Path.Combine(Application.StartupPath, "WCFService.exe.config");
Configuration config = ConfigurationManager.OpenExeConfiguration(path)  

ServicesSection serviceSection = ConfigurationManager.GetSection("system.serviceModel/services") as ServicesSection;

ServiceElementCollection sereleColl = serviceSection.Services;

but i get nothing in sereleColl.

但我在sereleColl中什么都没得到。

2 个解决方案

#1


Take a look at the ConfigurationManager class

看一下ConfigurationManager类

I don't remember how to do it though.

我不记得怎么做了。

Edit:

To access it you have to add a reference to System.Configuration.

要访问它,您必须添加对System.Configuration的引用。

Edit 2:

Changing the appsettings can be done like this:

更改appsettings可以这样做:

Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
AppSettingsSection appSettings = config.AppSettings;
KeyValueConfigurationElement setting = appSettings.Settings["MyAppSettingsKey"];
setting.Value = "newValue";
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

You can access the WCF settings by typing:

您可以通过键入以下内容来访问WCF设置:

ConfigurationSectionGroup sct = config.SectionGroups["system.serviceModel"];

Hope this helps.

希望这可以帮助。

Comment:

Your code is working fine here. However, I changed

你的代码在这里工作正常。但是,我改变了

string path = Path.Combine(Application.StartupPath, "WCFService.exe.config");

to

string path = Application.ExecutablePath;

This will use the config file of the application currently running. I don't know why your path doesn't work. Either it's that, or there must be an error in your config file?

这将使用当前运行的应用程序的配置文件。我不知道为什么你的道路不起作用。要么是这样,要么配置文件中一定有错误?

#2


Your calling "OpenExeConfiguration" - this will open the config for the currently executing app.

您的呼叫“OpenExeConfiguration” - 这将打开当前正在执行的应用程序的配置。

You need to read up on the .NET 2.0 configuration system: there's an excellent 3-part series on the .NET configuration system on CodeProject:

您需要阅读.NET 2.0配置系统:CodeProject上有一个关于.NET配置系统的优秀3部分系列:

There's a series of really good articles on you to demystify the .NET 2.0 configuration system on CodeProject:

有一系列非常好的文章可以揭开CodeProject上的.NET 2.0配置系统的神秘面纱:

  1. Unraveling the mysteries of .NET 2.0 configuration

    揭开.NET 2.0配置的神秘面纱

  2. Decoding the mysteries of .NET 2.0 configuration

    解码.NET 2.0配置的奥秘

  3. Cracking the mysteries of .NET 2.0 configuration

    破解.NET 2.0配置的奥秘

Highly recommended! Jon Rista did a great job explaining the configuration system in .NET 2.0.

强烈推荐! Jon Rista在.NET 2.0中解释配置系统方面表现出色。

#1


Take a look at the ConfigurationManager class

看一下ConfigurationManager类

I don't remember how to do it though.

我不记得怎么做了。

Edit:

To access it you have to add a reference to System.Configuration.

要访问它,您必须添加对System.Configuration的引用。

Edit 2:

Changing the appsettings can be done like this:

更改appsettings可以这样做:

Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
AppSettingsSection appSettings = config.AppSettings;
KeyValueConfigurationElement setting = appSettings.Settings["MyAppSettingsKey"];
setting.Value = "newValue";
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

You can access the WCF settings by typing:

您可以通过键入以下内容来访问WCF设置:

ConfigurationSectionGroup sct = config.SectionGroups["system.serviceModel"];

Hope this helps.

希望这可以帮助。

Comment:

Your code is working fine here. However, I changed

你的代码在这里工作正常。但是,我改变了

string path = Path.Combine(Application.StartupPath, "WCFService.exe.config");

to

string path = Application.ExecutablePath;

This will use the config file of the application currently running. I don't know why your path doesn't work. Either it's that, or there must be an error in your config file?

这将使用当前运行的应用程序的配置文件。我不知道为什么你的道路不起作用。要么是这样,要么配置文件中一定有错误?

#2


Your calling "OpenExeConfiguration" - this will open the config for the currently executing app.

您的呼叫“OpenExeConfiguration” - 这将打开当前正在执行的应用程序的配置。

You need to read up on the .NET 2.0 configuration system: there's an excellent 3-part series on the .NET configuration system on CodeProject:

您需要阅读.NET 2.0配置系统:CodeProject上有一个关于.NET配置系统的优秀3部分系列:

There's a series of really good articles on you to demystify the .NET 2.0 configuration system on CodeProject:

有一系列非常好的文章可以揭开CodeProject上的.NET 2.0配置系统的神秘面纱:

  1. Unraveling the mysteries of .NET 2.0 configuration

    揭开.NET 2.0配置的神秘面纱

  2. Decoding the mysteries of .NET 2.0 configuration

    解码.NET 2.0配置的奥秘

  3. Cracking the mysteries of .NET 2.0 configuration

    破解.NET 2.0配置的奥秘

Highly recommended! Jon Rista did a great job explaining the configuration system in .NET 2.0.

强烈推荐! Jon Rista在.NET 2.0中解释配置系统方面表现出色。