wcf - IIS中的主机400错误请求

时间:2013-12-24 07:41:33

标签: c# wcf rest iis wcf-binding

我发现了很多类似问题的问题,但找不到解决方法。在我的情况下,我写了wcf休息服务,然后创建客户端应用程序并连接。 (在本地主机中)然后连接并正常工作。 之后,我在IIS中托管我的wcf服务,并将我的客户端应用程序更改为IIS链接。然后它给了我以下错误。

The remote server returned an unexpected response: (400) Bad Request.

Server stack trace: 
   at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory`1 factory, WebException responseException, ChannelBinding channelBinding)
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)

我的网络配置如下:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <clear />
    <add name="LibraryMgtSysEntities" connectionString="metadata=res://*/DataLibraryMgtSys.csdl|res://*/DataLibraryMgtSys.ssdl|res://*/DataLibraryMgtSys.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=tt\SQLEXPRESS;initial catalog=LibraryMgtSys;Persist Security Info=True;user id=sa;password=testing;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
    <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=tt\SQLEXPRESS;          Initial Catalog=LibraryMgtSys;          Integrated Security=SSPI;          user id=sa;password=testing;" />
  </connectionStrings>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <membership defaultProvider="SqlMembershipProvider" userIsOnlineTimeWindow="15">
      <providers>
        <clear />
        <add name="SqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="DefaultConnection" applicationName="LibraryMgtSys" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" passwordFormat="Hashed" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" />
      </providers>
    </membership>
    <sessionState mode="InProc" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
      </providers>
    </sessionState>
    <roleManager enabled="true" defaultProvider="SqlRoleProvider">
      <providers>
        <clear />
        <add connectionStringName="DefaultConnection" applicationName="LibraryMgtSys" name="SqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
      </providers>
    </roleManager>
    <profile enabled="true">
      <providers>
        <clear />
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="DefaultConnection" applicationName="LibraryMgtSys" />
      </providers>
    </profile>
  </system.web>

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="ServiceBehaviour" name="LibraryManagementService.MobileService">
        <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding"
        contract="LibraryManagementService.IMobileService" />
      </service>

    </services>

    <bindings>
    </bindings>

    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>

      </serviceBehaviors>
    </behaviors>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>  
    <directoryBrowse enabled="true"/>
  </system.webServer>

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

我想念的是什么?

2 个答案:

答案 0 :(得分:1)

400错误请求指的是请求发送不遵守HTTP协议并且格式不正确。 正如您所说,在IIS上托管它之前它工作正常。

  1. 如果其余的GET api只是将网址粘贴到浏览器上并检查它是否正常工作?
  2. 如果是,则表示您在客户端应用程序中遇到问题
  3. 如果否,那么我建议您查看服务合同/或者您没有在IIS上正确托管服务。

答案 1 :(得分:0)

我发现由于数据库连接问题,我发生了这个问题。当我在IIS中托管我的Web服务时,需要为IIS用户提供数据库的访问权限。我按照answer

中的说明配置数据库设置