【问题标题】:How do I get my application to recognize the app.config file?如何让我的应用程序识别 app.config 文件?
【发布时间】:2014-11-18 23:03:15
【问题描述】:

这是一个直到现在对我来说从未真正成为问题的概念。在 VB.NET 中,每个应用程序都会自动生成一个 app.config 文件。多年来我一直忽略它们,但最近,随着我的任务是使用 Web 服务做越来越多的工作,它们似乎能够发挥有用的作用。这主要是绑定和端点的配置。我过去在代码中完成了所有这些工作,但我认为使用 app.config 会很有用,这样在端口号或 IP 发生更改的情况下,我可以只更改元素数据,而不是使用更改重新编译项目(我实际上目前为此使用 appsettings,但您明白了 - 我想要一个更完整的解决方案)。

所以,我的问题是当应用程序部署在我自己以外的计算机上时,我的应用程序似乎无法识别 app.config 文件。目前我得到可怕的“找不到引用合同 blahblah 的默认端点元素”,这似乎表明我的端点信息没有被读取。如果我在代码中做同样的事情,它就可以正常工作。

除了编译之外,我还需要做些什么来从 app.config 获取绑定/端点等以应用吗?

代码示例(可行):

Dim epa As New EndpointAddress("https://www.mysite.com/devserviceloc/test1.svc")
Dim binding As New BasicHttpBinding
binding.Name = "secureHttpBinding"
binding.Security.Mode = BasicHttpSecurityMode.Transport
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None
binding.Security.Transport.Realm = ""
Dim test As New TestingService1.test1Client(binding, epa)

App.config 等效项(不起作用)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">

    <requiredRuntime version="v4.0.20506"/>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="secureHttpBinding">
          <security mode="Transport">
            <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
          </security >
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
    <endpoint address="https://www.mysite.com/devserviceloc/test1.svc" binding="basicHttpBinding"
      bindingConfiguration="secureHttpBinding" contract="TestingService1.Itest1"
      name="BasicHttpBinding_Itest1" />
    </client>
  </system.serviceModel>

  <system.windows.forms jitDebugging="true" />
</configuration>

【问题讨论】:

  • 实际构建应用时,需要调用YourApplication.exe.config。您是否尝试在您的应用程序中使用文字 app.config?如果是这样,如果是 EXE,则需要将其重命名为 assemblyname.exe.config;如果是 DLL,则需要将其重命名为 assemblyname.dll.config。 Visual Studio 通常会为您执行此操作。
  • 如果您的 Web 服务是使用 IIS 提供的,那么配置文件需要称为 Web.config。
  • @TyCobb 是的,这正是我想要做的。我不知道我必须重命名它以匹配程序集名称。我的 IDE 没有为我命名 - 它始终是“app.config”。 (如果您发布了答案,我可以投票。)
  • @DeanOC - 因为 web.config 默认命名为 web.config,所以我在服务器端的 web 服务没有任何问题。我的问题是可以使用 app.config 文件的 dll 和 exe。

标签: .net vb.net web-services wcf-binding


【解决方案1】:

实际构建应用程序时,需要调用YourApplication.exe.config

如果是 EXE,则需要将其重命名为 assemblyname.exe.config,如果是 DLL,则需要重命名为 assemblyname.dll.config。 Visual Studio 通常会在您编译时为您执行此操作。在源文件夹中仍然是 app.config,但应该在 Debug & Release 文件夹中重命名。

You can see this mentioned here in the Application Configuration Files section.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    相关资源
    最近更新 更多