对WCFService的方法GET请求的HTTP 400错误请求

时间:2018-11-22 03:20:28

标签: c# wcf iis get fiddler

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

Fiddler Example

Fiddler Example

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

1 个答案:

答案 0 :(得分:1)

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

希望这可以帮助其他人。