如何在WCF中访问web.config?

时间:2011-05-12 22:19:37

标签: vb.net wcf configuration app-config

我有一个自托管的WCF(作为Windows服务),它有一个web.config文件。

在其中一个函数中,我有这个代码(似乎不起作用):

Shell(System.Configuration.ConfigurationManager.AppSettings("ExStream_CMD_File_Path"), AppWinStyle.NormalFocus)

web.config看起来像这样:

<configuration>
<configSections>
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
            <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
                <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
            </sectionGroup>
        </sectionGroup>
    </sectionGroup>
</configSections>
  <appSettings>
<add key="ExStream_CMD_File_Path" value="C:\HelloWorld.cmd"/>
  </appSettings>
<connectionStrings/>
<system.web>
...

由于某种原因,它不会去。所以我尝试在控制台应用程序中引用我的应用程序设置&amp;它的app.config。尝试使用ConfigurationManager.AppSettings读取它,但错误“不是Configuration的成员”,然后尝试了ConfigurationSettings.AppSettings - 但是错误的“方法已过时,请使用ConfigurationManager”。冗余!

如何在WCF中的配置文件中引用一个简单的字符串?

谢谢, 杰森

1 个答案:

答案 0 :(得分:2)

我不认为web.config具有正确的配置文件名,用于Windows服务,自托管服务(它适用于IIS中的虚拟主机服务)。尝试检查托管代码上AppDomain.CurrentDomain.SetupInformation.ConfigurationFile的值,该值将告诉您配置文件名应该是什么。并且调用System.Configuration.ConfigurationManager.AppSettings("ExStream_CMD_File_Path")应该可以正常工作。