Web.config 未在 IIS 中用于 WCF

Web.config not being used in IIS for WCF

我在 IIS 中有一个 WCF 服务 运行ning。我按照本教程将其托管在 IIS 中: https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-host-a-wcf-service-in-iis

我有一个简单的 Web.config 文件未被服务识别:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.serviceModel>
    <services>
      <service name="MyApp.Service1">
        <endpoint address=""
              binding="wsHttpBinding"
              contract="MyApp.IService1" />

    <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
  </service>
</services>
  </system.serviceModel>
</configuration>

当我 运行 这行代码时出现错误:对象未设置为对象的实例:

var test = ConfigurationManager.AppSettings["aspnet:UseTaskFriendlySynchronizationContext"];
var x = test.ToString();

我在与 DLL 相同的文件夹中有 Web.config 文件。整个服务都在 wwwroot 中。

是否缺少某些设置才能使 web.config 真正起作用? IIS 中是否需要设置某些内容才能使用 web.config?我该怎么做才能让 IIS 识别我的 web.config?

你的app.config文件生效失败是造成这个问题的主要原因。 如示例中所述。

  • Create a new file named "service.svc" in the application directory

  • Create a file named "Web.config" in the application directory

如果要在 IIS 中托管 wcf 服务库项目,则应按照以下步骤操作。

您必须将配置代码移动到托管环境可识别的配置文件中。然后你就可以正确阅读web.config。

Yes, it is a service class library. Not sure what you mean by the config file of the application. Is that the app.config?

这里是关于如何部署wcf服务库的官方文档。

https://docs.microsoft.com/en-us/dotnet/framework/wcf/deploying-a-wcf-library-project

一般来说,我们在windows服务托管环境中为wcf使用app.config。