【问题标题】:HTTP 400 Bad Request on method GET request for WCFService对 WCF 服务的方法 GET 请求的 HTTP 400 错误请求
【发布时间】:2018-11-22 03:20:28
【问题描述】:

我正在使用 fiddler 来测试我的网络服务。它连接到 SQL 服务器数据库(已连接并正在运行) get 在本地主机上工作,并通过 VS 2017 中的 IIS Express 工作。在 IIS 上,除了通过提琴手的 GET 请求外,服务器工作。如果您需要查看我的Service1.svc 文件,请告诉我(代码过多,文本不足错误)我已尝试所有其他答案

web.config

<appSettings>
  <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
  <compilation debug="true" targetFramework="4.6.1" />
  <httpRuntime targetFramework="4.6.1"/>
</system.web>
<system.serviceModel>

      <!--<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="false"/>
      </behavior> -->
  <bindings>
    <webHttpBinding>
      <binding name="restLargeBinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" transferMode="Streamed">
        <readerQuotas maxStringContentLength="2147483647"/>
      </binding>
    </webHttpBinding>
  </bindings>
  <behaviors>
    <endpointBehaviors>
      <behavior name="myWebEndPointBehaviour">
        <webHttp automaticFormatSelectionEnabled="true" defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Json" helpEnabled="true"/>
        <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
      </behavior>
    </endpointBehaviors> 

    <serviceBehaviors>
      <behavior name="myServiceBehaviour">
        <serviceMetadata httpGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
        <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>

  <services>
    <service name="TestWcfService.Service1" behaviorConfiguration="myServiceBehaviour">
      <endpoint address="" contract="TestWcfService.IService1" binding="webHttpBinding" bindingConfiguration="restLargeBinding" behaviorConfiguration="myWebEndPointBehaviour"></endpoint>
      <endpoint address="mex" contract="TestWcfService.IService1" binding="mexHttpBinding" />
    </service>
  </services>

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

IService1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace TestWcfService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {

        // TODO: Add your service opperations here

        // Get Method
        [OperationContract]
        [WebGet(UriTemplate = "GetUserName")]
        List<UserName> GetUserName();

        // Post Method
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "AddUser", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        int AddUser(string user);

    }

    [DataContract]
    public class UserName
    {
        string user;

        [DataMember]
        public string User
        {
            get { return user; }
            set { user = value; }
        }
    }
}

【问题讨论】:

  • 可以调试wcf服务吗?
  • 我可以,但我在 Visual Studio 中没有收到错误,仅在提琴手上
  • 我建议在您的代码中编写适当的异常处理,并在一些日志文件中写入错误和异常详细信息。这样你就可以知道你得到了什么确切的错误。您共享的 Fiddler 屏幕截图没有说明错误消息。所以不确定可能是什么问题。我假设 Visual Studio、IIS 和提琴手都在同一台机器上。
  • 你在浏览器(邮递员)中测试过并正确返回结果吗?您用于连接 SQL 服务器的身份验证是什么?尝试使用 SQL Server 身份验证。
  • 我发现了异常,它是Login failed for user 'IIS APPPOOL\WCFService 搜索引导我找到解决问题的已接受答案stackoverflow.com/questions/7698286/… 接受我添加了IIS APPPOOL\WCFService 的登录名而不是@ 987654331@回答。

标签: c# wcf iis get fiddler


【解决方案1】:

我需要找到正确的异常消息。有几种不同的方法可以做到这一点,但在这种情况下最简单的方法是使用 fiddler(因为错误是在使用它时引起的)。我需要做的就是粘贴用于 GET 请求的 Uri 并将其粘贴到浏览器中。这给了我异常和跟踪详细信息。然后我使用异常消息Login failed for user 'IIS APPPOOL\WCFService 来搜索答案。这个问题的公认答案Login failed for user 'IIS APPPOOL\ASP.NET v4.0' 是解决方案(登录可能会或不会略有不同,具体取决于您使用的确切错误代码和服务)

希望这对其他人有帮助。

【讨论】:

  • 是的,感谢您分享进度。我开始认为这是数据库连接的身份验证问题。如果你尝试使用SQL server认证指定连接字符串,我认为不会有这样的问题。
猜你喜欢
  • 2012-04-27
  • 2015-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-21
  • 2011-09-29
  • 2014-01-11
  • 1970-01-01
相关资源
最近更新 更多