IIS 上托管的 WCF 网络服务 returns 空 xml 响应

WCF webservice hosted on IIS returns empty xml response

我创建了一个托管在 IIS window 服务器环境中的 rest wcf 服务。当我尝试从 IIS 访问服务 (http://localhost:8081/Service1.svc/EmployeeDump) 时,它 return 的空白 xml 响应。它不会给出任何错误,只是一个空白 xml 被 returned。但是,可以从浏览器访问服务 wsdl(元数据)文件。当我 运行 它在 Visual Studio 上并且端点方法 return 的数据响应在 xml 时,该服务工作正常。但是当我在 IIS 服务器上托管相同的服务时,它 return 的空白响应我不知道为什么。

我尝试从服务器管理器安装 HTTP 激活功能并尝试更改 Web 配置参数,例如添加主机选项,但仍然是同样的问题。

以下是我的网络配置:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
  </configSections>
  <connectionStrings>
    <add name="MyDB"connectionString="server=xyz\abc;database=testDB;Integrated Security = SSPI;"/>
  </connectionStrings>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
  <system.web>
    <compilation targetFramework="4.6.1" debug="true"/>
    <httpRuntime targetFramework="4.6.1"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpTransportSecurity">
          <security mode="Transport">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service name="MasterDumpService.Service1" behaviorConfiguration="myServiceBehavior">
        <endpoint address="" binding="webHttpBinding" behaviorConfiguration="webBehavior" contract="MasterDumpService.IService1"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8081/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="myServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="MasterDumpService.ServiceAuthenticator, MasterDumpService"/>
          </serviceCredentials>
        </behavior>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb"/>
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
    </providers>
  </entityFramework>
</configuration>

下面是我从托管在 IIS 上的服务获得的空白 xml 响应:

<?xml version="1.0"?>
<ArrayOfEmployeeDump xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MasterDumpService"/>

下面是正确的 xml 响应(托管服务实际上应该 return),当我在浏览器上 运行 来自 visual studio 的服务时:

<?xml version="1.0"?>

-<ArrayOfEmployeeDump xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MasterDumpService">


-<EmployeeDump>

<BusinessUnit/>

<Department>E commerce</Department>

<DisplayName>Gayathri</DisplayName>

<Mail>gayathri@xyz.com</Mail>

</EmployeeDump>


-<EmployeeDump>

<BusinessUnit/>

<Department/>

<DisplayName>kiran</DisplayName>

<Mail>kiran@domain.com</Mail>

</EmployeeDump>


-<EmployeeDump>

<BusinessUnit/>

<Department/>

<DisplayName>xyz</DisplayName>

<Mail>xyz@domian.com</Mail>

</EmployeeDump>

任何帮助将不胜感激。

问题的关键是数据库连接。我们可以通过在调试时加断点来查看EF实体返回的结果。
当我们在IIS中部署服务时,到数据库的连接字符串最好使用username/password而不是集成安全。因为 IIS 应用程序池标识可能不同于当前用户 运行 VS.
此外,我们在IIS中部署服务时,应该在IIS站点绑定模块中指定基地址,而不是在webconfig文件中。
如果有什么我可以帮忙的,请随时告诉我。