Question

i have a client program that connect to my wcf service. i want to embed app config to C# code till users can not change or even see app.config .

but i cannot bring these two config setting to C# code:

<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>

And

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

i added some code like this

 System.Diagnostics.XmlWriterTraceListener xmlt = new System.Diagnostics.XmlWriterTraceListener("Trace.svclog", "myListener");
  System.Diagnostics.Trace.Listeners.Add(xmlt):

but did not work . when you set Trace Listener in app.config File , The application will automatic log exception,Warning,etc occured (That i want) but when i create System.Diagnostics.XmlWriterTraceListener i must write log (exceptions) my self.

about default proxy i found some classes but i can not find those setting in classes.

Questions :

1- I want to bring these Settings to C# code.(i want C# result be exact like app.config result)

2- is the app.config Stronger than C# code? can i find all app.config settings in c# classes?

Was it helpful?

Solution

Yes you can configure all of the settings for the WCF in code. Searching the stackoverflow I found a link were this questions has been asked wcf-configuration-without-a-config-file Also there is a link from the Microsoft MSDN MSDN Source. About the tracing of the WCF here is a link that also will help you:WCF Tracing in code

OTHER TIPS

I'll address the modification of the WCF Web.Config on the fly aspect of your question. Note that this method also works for accessing APPConfig files as well.

You can access configuration files outside the confines of your Form application.

System.Configuration.ExeConfigurationFileMap configMap = new ExeConfigurationFileMap("YourWCFAppPath\\web.config");
System.Configuration.Configuration webConfigFile = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);

From there you can GetSections of the configuration; Modify it and then save it back to the configuration file. I'm not at a machine with visual studio on it right now, so I'm not 100% certain on the syntax for getting the element section; once you have it you can modify the returned object and when done execute a save on the config file. When saving the configuration file you will need to specify the mode as modified.

webConfigFile.Save(ConfigurationSaveMode.Modified);

You will need to remember to have the WCF Application call a refresh on the configuration file it was already running at the time you modified the config file. If you had modified the Connection strings section you would execute the code below.

System.Configuration.ConfigurationManager.RefreshSection("connectionStrings");

What about embedding the app.config file into the assemblies? It should be an option in the properties of the app.config file.

Or you can configure it all in code and get rid of the app.config file altogether:

The Proxy can be configured like:

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

However, I believe WCF Tracing cannot be completely configured via code... you could try using another logging mechanism or ETW?

You can use System.Configuration.ConfigurationManager + System.Xml.XmlReader / System.Xml.Serialization.XmlSerializer to retrieve configuration from your file. Then you can store its content in database, and use manually in all places where it should be. Also as @xmidPOE advised, see this article wcf-configuration-without-a-config-file

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top