简体   繁体   English

将app config嵌入到wcf中的c#代码中

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

i have a client program that connect to my wcf service. 我有一个连接到我的wcf服务的客户端程序。 i want to embed app config to C# code till users can not change or even see app.config . 我想将app config嵌入C#代码中,直到用户无法更改甚至看到app.config

but i cannot bring these two config setting to C# code: 但我不能将这两个配置设置带到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>

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. 当您在app.config文件中设置跟踪侦听器时,应用程序将自动记录异常,警告等发生(我想要),但是当我创建System.Diagnostics.XmlWriterTraceListener时,我必须写自己的日志(例外)。

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) 1-我想将这些设置带到C#代码。(我希望C#结果与app.config结果完全一样)

2- is the app.config Stronger than C# code? 2-是app.config比C#代码强吗? can i find all app.config settings in c# classes? 我可以在c#类中找到所有app.config设置吗?

Yes you can configure all of the settings for the WCF in code. 是的,您可以在代码中配置WCF的所有设置。 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 . 搜索stackoverflow我找到了一个链接,这个问题已经被问到wcf-configuration-without-a-config-file还有来自Microsoft MSDN MSDN Source的链接。 About the tracing of the WCF here is a link that also will help you: WCF Tracing in code 关于WCF的跟踪,这里有一个链接也可以帮助您: WCF跟踪代码

I'll address the modification of the WCF Web.Config on the fly aspect of your question. 我将在你的问题的飞行方面解决WCF Web.Config的修改。 Note that this method also works for accessing APPConfig files as well. 请注意,此方法也适用于访问APPConfig文件。

You can access configuration files outside the confines of your Form application. 您可以访问Form应用程序范围之外的配置文件。

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; 从那里你可以获得配置的GetSections; 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; 我现在不在装有visual studio的机器上,所以我不是100%肯定获取元素部分的语法; 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. 您需要记住在修改配置文件时,WCF应用程序调用它已在运行的配置文件上刷新。 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? 将app.config文件嵌入到程序集中怎么样? It should be an option in the properties of the app.config file. 它应该是app.config文件属性中的一个选项。

Or you can configure it all in code and get rid of the app.config file altogether: 或者您可以在代码中配置它并完全删除app.config文件:

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? 但是,我认为无法通过代码完全配置WCF跟踪...您可以尝试使用其他日志记录机制或ETW吗?

You can use System.Configuration.ConfigurationManager + System.Xml.XmlReader / System.Xml.Serialization.XmlSerializer to retrieve configuration from your file. 您可以使用System.Configuration.ConfigurationManager + System.Xml.XmlReader / System.Xml.Serialization.XmlSerializer从文件中检索配置。 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 另外,正如@xmidPOE建议的那样,请参阅本文wcf-configuration-without-a-config-file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM