【问题标题】:WCF webservice hosted on IIS returns empty xml responseIIS 上托管的 WCF Web 服务返回空的 xml 响应
【发布时间】:2019-11-18 00:23:00
【问题描述】:

我创建了一个托管在 IIS 窗口服务器环境中的 rest wcf 服务。当我尝试从 IIS 访问服务(http://localhost:8081/Service1.svc/EmployeeDump)时,它返回空白 xml 响应。它没有给出任何错误,只是返回一个空白 xml。但是,可以从浏览器访问服务 wsdl(metadata) 文件。当我在 Visual Studio 上运行该服务并且端点方法以 xml 格式返回数据响应时,该服务运行良好。但是当我在 IIS 服务器中托管相同的服务时,它返回空白响应,我不知道为什么。

我尝试从服务器管理器安装 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"/>

当我在浏览器上从 Visual Studio 运行服务时,以下是正确的 xml 响应(托管服务实际上应该返回):

<?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>

我们将不胜感激。

【问题讨论】:

标签: .net xml web-services wcf iis


【解决方案1】:

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

【讨论】:

  • 嗨,亚伯拉罕,你说得对,集成安全在 IIS 中不起作用,我们必须指定用户名和密码。在一位高级开发人员的帮助下,我能够通过将我的服务的应用程序池下的用户帐户从 applicationpoolidentity 更改为我运行 VS 并且拥有权限的用户帐户来解决此问题在数据库上。该错误未显示在浏览器中,因此我们通过创建一个 commonhelper 类来启用错误日志,该类将服务的错误日志写入一个文件中,这有助于我们追踪问题。
猜你喜欢
  • 2012-04-08
  • 2018-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多