簡體   English   中英

將app config嵌入到wcf中的c#代碼中

[英]embed app config into c# code in wcf

我有一個連接到我的wcf服務的客戶端程序。 我想將app config嵌入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設置嗎?

是的,您可以在代碼中配置WCF的所有設置。 搜索stackoverflow我找到了一個鏈接,這個問題已經被問到wcf-configuration-without-a-config-file還有來自Microsoft MSDN MSDN Source的鏈接。 關於WCF的跟蹤,這里有一個鏈接也可以幫助您: WCF跟蹤代碼

我將在你的問題的飛行方面解決WCF Web.Config的修改。 請注意,此方法也適用於訪問APPConfig文件。

您可以訪問Form應用程序范圍之外的配置文件。

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

從那里你可以獲得配置的GetSections; 修改它,然后將其保存回配置文件。 我現在不在裝有visual studio的機器上,所以我不是100%肯定獲取元素部分的語法; 一旦你擁有它,你可以修改返回的對象,完成后在配置文件上執行保存。 保存配置文件時,您需要將模式指定為已修改。

webConfigFile.Save(ConfigurationSaveMode.Modified);

您需要記住在修改配置文件時,WCF應用程序調用它已在運行的配置文件上刷新。 如果您修改了連接字符串部分,則可以執行以下代碼。

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

將app.config文件嵌入到程序集中怎么樣? 它應該是app.config文件屬性中的一個選項。

或者您可以在代碼中配置它並完全刪除app.config文件:

代理可以配置為:

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

但是,我認為無法通過代碼完全配置WCF跟蹤...您可以嘗試使用其他日志記錄機制或ETW嗎?

您可以使用System.Configuration.ConfigurationManager + System.Xml.XmlReader / System.Xml.Serialization.XmlSerializer從文件中檢索配置。 然后,您可以將其內容存儲在數據庫中,並在所有應該使用的位置手動使用。 另外,正如@xmidPOE建議的那樣,請參閱本文wcf-configuration-without-a-config-file

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM