c# - 将应用程序配置嵌入到 wcf 中的 c# 代码中

标签 c# wcf configuration wcf-client wcf-configuration

我有一个连接到我的 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 文件中设置 Trace Listener 时,应用程序将自动记录发生的异常、警告等(我想要的),但是当我创建 System.Diagnostics.XmlWriterTraceListener 时,我必须写入日志(异常)我自己。

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

问题:

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

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

最佳答案

是的,您可以在代码中配置 WCF 的所有设置。搜索 stackoverflow 我找到了一个链接,这个问题已经被问到 wcf-configuration-without-a-config-file 还有一个来自 Microsoft MSDN MSDN Source 的链接. 关于 WCF 的跟踪,这里有一个链接也可以帮助您:WCF Tracing in code

关于c# - 将应用程序配置嵌入到 wcf 中的 c# 代码中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15247907/

相关文章:

c# - 通过 WSDL url 使用 Web 服务

javascript - 加载 Javascript 文件的问题

c# - WCF 请求期间出现 CommunicationException,基础连接已关闭

.net - 需要 WCF 服务

c# - 在 WCF 中找不到引用契约(Contract)的默认端点元素

configuration - 在我自己的自定义 appSettings 上使用 foreach 所需的代码

android - android 2.3 中的方向没有改变

c# - 如何获取应用程序的进程名称?

c# - 用 C# 动态读取 COBOL 重新定义

java - 如何将配置设置注入(inject) Autowiring 的 spring bean?