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

Mam*_*d R 5 c# configuration wcf 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>
Run Code Online (Sandbox Code Playgroud)

<system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true" >
      <proxy autoDetect="True" usesystemdefault="True"/>
    </defaultProxy>
  </system.net>
Run Code Online (Sandbox Code Playgroud)

我添加了一些这样的代码

 System.Diagnostics.XmlWriterTraceListener xmlt = new System.Diagnostics.XmlWriterTraceListener("Trace.svclog", "myListener");
  System.Diagnostics.Trace.Listeners.Add(xmlt):
Run Code Online (Sandbox Code Playgroud)

但是没有用。当您在app.config File中设置Trace Listener时,应用程序将自动发生日志异常,警告等(我想要的),但是当我创建System.Diagnostics.XmlWriterTraceListener时,我必须自己写日志(例外)。

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

问题:

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

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

小智 3

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