简体   繁体   English

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

[英]wcf - 400 bad request when host in IIS

I found so many questions with similar problem, but couldn't find solution for this. 我发现了很多类似问题的问题,但是找不到解决方案。 in my case i wrote wcf rest service and then created client application and connected. 就我而言,我编写了wcf rest服务,然后创建了客户端应用程序并进行了连接。 (in local host) and then it connected and working properly. (在本地主机中),然后它已连接并正常工作。 After that i host my wcf service in IIS and change my client application to IIS link. 之后,我将我的wcf服务托管在IIS中,并将客户端应用程序更改为IIS链接。 then it gives me following error. 然后它给了我以下错误。

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)

My web config as follows: 我的网络配置如下:

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

What i miss in here? 我在这里想念什么?

400 bad request refers that the request send does not respect the HTTP protocol and is not well formed. 400错误的请求是指请求发送不遵守HTTP协议并且格式不正确。 As you said it was working fine before you host it on IIS. 正如您所说,在将其托管在IIS上之前,它运行良好。

  1. if its rest GET api just paste the url on browser and check is it working fine ? 如果它的其余GET API只是将URL粘贴到浏览器上并检查是否正常?
  2. If yes mean you have a problem in client application 如果是,则表示您的客户端应用程序有问题
  3. If No then i would suggest you to check the service contract / or you did not host the service properly on IIS. 如果否,那么我建议您检查服务合同/或者您没有在IIS上正确托管服务。

I found this problem occurs to me because of database connection problem. 我发现此问题是由于数据库连接问题而发生的。 When I'm host my web service in IIS its need to give access rights to database for IIS user. 当我将Web服务托管在IIS中时,它需要为IIS用户授予对数据库的访问权限。 i configure database settings as mentioned in this answer 我按照此答案中所述配置数据库设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM