在WCF中将应用程序配置嵌入到C#代码中

5
我有一个客户端程序连接到我的WCF服务。我想将应用程序配置嵌入到C#代码中,以至于用户不能更改甚至看到app.config。
但是我无法将这两个配置设置带到C#代码中:
<system.diagnostics>
    <sources>
      <source propagateActivity="true" name="System.ServiceModel" switchValue="Warning">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="NewListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
      <source name="System.ServiceModel.MessageLogging" switchValue="Warning,ActivityTracing" >
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="NewListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add initializeData="Trace.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
          name="NewListener" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack">
        <filter type="" />
      </add>
    </sharedListeners>
  </system.diagnostics>

并且
<system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true" >
      <proxy autoDetect="True" usesystemdefault="True"/>
    </defaultProxy>
  </system.net>

我添加了类似下面的代码:
 System.Diagnostics.XmlWriterTraceListener xmlt = new System.Diagnostics.XmlWriterTraceListener("Trace.svclog", "myListener");
  System.Diagnostics.Trace.Listeners.Add(xmlt):

但是没有起作用。当你在 app.config 文件中设置跟踪侦听器时,应用程序会自动记录异常、警告等发生的情况(这就是我想要的),但是当我创建 System.Diagnostics.XmlWriterTraceListener 时,我必须自己写日志(异常)。

关于默认代理,我找到了一些类,但在类中找不到这些设置。

问题:

1- 我想将这些设置带入 C# 代码中(我希望 C# 的结果与 app.config 的结果完全相同)。

2- app.config 是否比 C# 代码更强大?我能否在 C# 类中找到所有 app.config 设置?


这里有一个绝妙的解决方案 here - Brandon
4个回答

3

1
你可以使用 System.Configuration.ConfigurationManager + System.Xml.XmlReader / System.Xml.Serialization.XmlSerializer 来从文件中检索配置。然后,您可以将其内容存储在数据库中,并在所有应该使用它的地方手动使用。此外,正如 @xmidPOE 建议的那样,请参阅这篇文章 wcf-configuration-without-a-config-file

1
我将解决关于在运行时修改WCF Web.Config的问题。请注意,此方法也适用于访问APPConfig文件。
您可以访问超出表单应用程序范围的配置文件。
System.Configuration.ExeConfigurationFileMap configMap = new ExeConfigurationFileMap("YourWCFAppPath\\web.config");
System.Configuration.Configuration webConfigFile = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);

从那里,您可以获取配置的部分; 修改它,然后将其保存回配置文件。 我现在没有安装Visual Studio的机器,所以我不确定获取元素部分的语法;一旦您拥有它,就可以修改返回的对象,并在完成时对配置文件执行保存。 保存配置文件时,您需要指定模式为已修改。

webConfigFile.Save(ConfigurationSaveMode.Modified);

你需要记住让WCF应用程序调用刷新操作,以便在您修改配置文件时,它已经在运行的配置文件上进行更新。如果您修改了连接字符串部分,则可以执行以下代码。
System.Configuration.ConfigurationManager.RefreshSection("connectionStrings");

完全不想使用app.config,我只想用另一种方式来处理它...用户可以编辑app.config,我想把它移除并将设置放在代码中。谢谢回答。 - Mamad RN

1

把app.config文件嵌入到程序集中怎么样?这应该是app.config文件属性中的一个选项。

或者你可以完全在代码中配置它,摆脱app.config文件:

代理可以这样配置:

var proxy = HttpWebRequest.DefaultWebProxy;
proxy.Credentials = CredentialCache.DefaultCredentials;  

然而,我相信WCF跟踪不能完全通过代码进行配置...你可以尝试使用另一种日志记录机制或ETW吗?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接